2022-01: Calorie Counting

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(data.table)

Attaching package: 'data.table'

The following objects are masked from 'package:dplyr':

    between, first, last

The following object is masked from 'package:purrr':

    transpose

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.

dt <- readLines("input.txt")

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.

sm <- 0
maxsm <- 0
for (x in dt) {
  x <- as.numeric(x)
  if (!is.na(x)) {
    sm <<- sm + x
  } else if (sm > maxsm) {
    maxsm <<- sm
    sm <<- 0
  } else {
    sm <<- 0
  }
}
maxsm
[1] 68923

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.

sm <- 0
maxsm <- c()
for (x in dt) {
  x <- as.numeric(x)
  if (!is.na(x)) {
    sm <<- sm + x
  } else  {
    maxsm <<- c(maxsm, sm)
    sm <<- 0
  }
}
sum(sort(maxsm, decreasing = TRUE)[1:3])
[1] 200044