# read the things
jolts <- readLines("data-2020-10.txt")Warning in readLines("data-2020-10.txt"): incomplete final line found on
'data-2020-10.txt'
jolts = c(0, sort(as.numeric(jolts)))
jolts = c(jolts, max(jolts)+3)Nathan Moore
Jolts, and stuff?
Find a chain that uses all of your adapters to connect the charging outlet to your device’s built-in adapter and count the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt differences multiplied by the number of 3-jolt differences?
Warning in readLines("data-2020-10.txt"): incomplete final line found on
'data-2020-10.txt'
Sort and then add up the differences
We need to join all of these together.
What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device?
# to connect 0 to 1, is 1 way (and 1 for 0 because I did this manually)
ways = 1
ways_1 = 1
ways_2 = 1
ways_3 = 0
# to connect 1 to 2, or 0 to 2, is 2 ways
ways = 2
ways_1 = 1
ways_2 = 1
ways_3 = 1
# now do the rest for 3 etc
for (j in 4:length(jolts)) {
# print(c(jolts[j], jolts[j-1], jolts[j-2], jolts[j-3]))
# print(c(ways, ways_1, ways_2, ways_3))
if ((jolts[j] - jolts[j-3]) <= 3) {
ways = ways + ways_3
}
if ((jolts[j] - jolts[j-2]) <= 3) {
ways = ways + ways_2
}
# if ((jolts[j] - jolts[j-1]) <= 3) {
# ways = ways + ways_1
# }
ways_3 = ways_2
ways_2 = ways_1
ways_1 = ways
# print(ways)
}
print(ways, digits=16)[1] 3947645370368