01: Calorie Counting
My initial thought was to use data.table::fread
but that skipped the spaces in the input by default. readLines
keeps the spaces so I switched to that right away.
Part 1
This was just a matter of getting a loop with a maximum value counter going. I knew from the get-go that I would need to use the global assignment operator inside the loop. For reasons I thought that naming my variable sum
would work but got an error since that’s a reserved word in R (duh!). Find+replace helped get that sorted but my inner loop logic was still wrong. I realized that I was missing an else
case to catch any time the maximum hadn’t changed, but the sum needed to be reset.
Part 2
Coming out of Part 1 was really strong, especially on Day 1. Instead of saving just the max sum, I made it a vector to record all sums. Then I just need to sort it, get the top three values, and sum those.