library(tidyverse)
my_file <- here::here("2021", "data-2021-07.txt")
x <- readLines(my_file)2021 Day 7
— Day 7: The Treachery of Whales —
Watch out for the whale that wants to eat you! We need to align some crabs.
Determine the horizontal position that the crabs can align to using the least fuel possible. How much fuel must they spend to align to that position?
brute force to calculate everything, using vector differences.
# positions of crabs
crabs = as.numeric(unlist(strsplit(x[1], ",")))
# brutish
ss = sum(abs(crabs - 1)) + 1
for (i in seq(floor(mean(crabs)))) {
tt = sum(abs(crabs - i))
if (tt < ss) {
ss = tt
} else {
break
}
}
ss[1] 354129
Alternative approach: align with the median.
# minimise the sum of differences
crabs = as.numeric(unlist(strsplit(x[1], ",")))
sum(abs(crabs - median(crabs)))[1] 354129
— Part Two —
Actually, the calculations are different.
Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! How much fuel must they spend to align to that position?
# minimise the sum of differences - this time from the mean
# with a triangular number function
crabs = as.numeric(unlist(strsplit(x[1], ",")))
diffs = abs(crabs - floor(mean(crabs)))
# print(diffs)
sum(diffs * (diffs + 1) / 2)[1] 98905973