@ -0,0 +1,162 @@ | |||
# --- Day 8: Handheld Halting --- | |||
# | |||
# Your flight to the major airline hub reaches cruising altitude without | |||
# incident. While you consider checking the in-flight menu for one of those | |||
# drinks that come with a little umbrella, you are interrupted by the kid | |||
# sitting next to you. | |||
# | |||
# Their handheld game console won't turn on! They ask if you can take a look. | |||
# | |||
# You narrow the problem down to a strange infinite loop in the boot code | |||
# (your puzzle input) of the device. You should be able to fix it, but first | |||
# you need to be able to run the code in isolation. | |||
# | |||
# The boot code is represented as a text file with one instruction per line of | |||
# text. Each instruction consists of an operation (acc, jmp, or nop) and an | |||
# argument (a signed number like +4 or -20). | |||
# | |||
# acc increases or decreases a single global value called the accumulator by | |||
# the value given in the argument. For example, acc +7 would increase the | |||
# accumulator by 7. The accumulator starts at 0. After an acc instruction, | |||
# the instruction immediately below it is executed next. | |||
# jmp jumps to a new instruction relative to itself. The next instruction to | |||
# execute is found using the argument as an offset from the jmp instruction; | |||
# for example, jmp +2 would skip the next instruction, jmp +1 would | |||
# continue to the instruction immediately below it, and jmp -20 would | |||
# cause the instruction 20 lines above to be executed next. | |||
# nop stands for No OPeration - it does nothing. The instruction immediately | |||
# below it is executed next. | |||
# | |||
# For example, consider the following program: | |||
# | |||
# nop +0 | |||
# acc +1 | |||
# jmp +4 | |||
# acc +3 | |||
# jmp -3 | |||
# acc -99 | |||
# acc +1 | |||
# jmp -4 | |||
# acc +6 | |||
# | |||
# These instructions are visited in this order: | |||
# | |||
# nop +0 | 1 | |||
# acc +1 | 2, 8(!) | |||
# jmp +4 | 3 | |||
# acc +3 | 6 | |||
# jmp -3 | 7 | |||
# acc -99 | | |||
# acc +1 | 4 | |||
# jmp -4 | 5 | |||
# acc +6 | | |||
# | |||
# First, the nop +0 does nothing. Then, the accumulator is increased from | |||
# 0 to 1 (acc +1) and jmp +4 sets the next instruction to the other acc +1 | |||
# near the bottom. After it increases the accumulator from 1 to 2, jmp -4 | |||
# executes, setting the next instruction to the only acc +3. It sets the | |||
# accumulator to 5, and jmp -3 causes the program to continue back at the | |||
# first acc +1. | |||
# | |||
# This is an infinite loop: with this sequence of jumps, the program will | |||
# run forever. The moment the program tries to run any instruction a second | |||
# time, you know it will never terminate. | |||
# | |||
# Immediately before the program would run an instruction a second time, the | |||
# value in the accumulator is 5. | |||
# | |||
# Run your copy of the boot code. Immediately before any instruction is | |||
# executed a second time, what value is in the accumulator? | |||
library(tidyverse) | |||
input <- "../input/08-01.txt" | |||
read_delim( | |||
file = input, | |||
delim = " ", | |||
col_names = c("instr", "value"), | |||
col_types = "cd" | |||
) %>% | |||
mutate( | |||
visits = 0 | |||
) -> asm | |||
curr <- 1 | |||
accumulator <- 0 | |||
while (all(asm$visits < 2)) { | |||
asm$visits[curr] <- asm$visits[curr] + 1 | |||
switch( | |||
asm$instr[curr], | |||
"nop" = curr + 1, | |||
"acc" = curr + 1, | |||
"jmp" = curr + asm[curr,]$value | |||
) -> nxt | |||
if (nxt > nrow(asm)) break | |||
if ((asm$visits[curr] < 2) && (asm$instr[curr] == "acc")) { | |||
accumulator <- accumulator + asm$value[curr] | |||
} | |||
curr <- nxt | |||
} | |||
# 08-02 ------------------------------------------------------------------- | |||
read_delim( | |||
file = input, | |||
delim = " ", | |||
col_names = c("instr", "value"), | |||
col_types = "cd" | |||
) %>% | |||
mutate( | |||
visits = 0 | |||
) -> asm | |||
orig_program <- asm | |||
for (run in seq_len(nrow(asm))) { | |||
asm <- orig_program | |||
curr <- 1 | |||
accumulator <- 0 | |||
switch( | |||
asm$instr[run], | |||
"acc" = "acc", | |||
"nop" = "jmp", | |||
"jmp" = "nop" | |||
) -> asm$instr[run] | |||
while (all(asm$visits < 2)) { | |||
asm$visits[curr] <- asm$visits[curr] + 1 | |||
switch( | |||
asm$instr[curr], | |||
"acc" = curr + 1, | |||
"nop" = curr + 1, | |||
"jmp" = curr + asm[curr,]$value | |||
) -> nxt | |||
if (nxt > nrow(asm)) break | |||
if ((asm$visits[curr] < 2) && (asm$instr[curr] == "acc")) { | |||
accumulator <- accumulator + asm$value[curr] | |||
} | |||
curr <- nxt | |||
} | |||
if (all(asm$visits < 2)) break | |||
} | |||
accumulator |
@ -0,0 +1,149 @@ | |||
# --- Day 9: Encoding Error --- | |||
# | |||
# With your neighbor happily enjoying their video game, you turn your attention | |||
# to an open data port on the little screen in the seat in front of you. | |||
# | |||
# Though the port is non-standard, you manage to connect it to your computer | |||
# through the clever use of several paperclips. Upon connection, the port | |||
# outputs a series of numbers (your puzzle input). | |||
# | |||
# The data appears to be encrypted with the eXchange-Masking Addition System | |||
# (XMAS) which, conveniently for you, is an old cypher with an important weakness. | |||
# | |||
# XMAS starts by transmitting a preamble of 25 numbers. After that, each number | |||
# you receive should be the sum of any two of the 25 immediately previous | |||
# numbers. The two numbers will have different values, and there might be more | |||
# than one such pair. | |||
# | |||
# For example, suppose your preamble consists of the numbers 1 through 25 in a | |||
# random order. To be valid, the next number must be the sum of two of those numbers: | |||
# | |||
# 26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24). | |||
# 49 would be a valid next number, as it is the sum of 24 and 25. | |||
# 100 would not be valid; no two of the previous 25 numbers sum to 100. | |||
# 50 would also not be valid; although 25 appears in the previous 25 numbers, the two numbers in the pair must be different. | |||
# | |||
# Suppose the 26th number is 45, and the first number (no longer an option, | |||
# as it is more than 25 numbers ago) was 20. Now, for the next number to be | |||
# valid, there needs to be some pair of numbers among 1-19, 21-25, or 45 that | |||
# add up to it: | |||
# | |||
# 26 would still be a valid next number, as 1 and 25 are still within the previous 25 numbers. | |||
# 65 would not be valid, as no two of the available numbers sum to it. | |||
# 64 and 66 would both be valid, as they are the result of 19+45 and 21+45 respectively. | |||
# | |||
# Here is a larger example which only considers the previous 5 numbers (and has a preamble of length 5): | |||
# | |||
# 35 | |||
# 20 | |||
# 15 | |||
# 25 | |||
# 47 | |||
# 40 | |||
# 62 | |||
# 55 | |||
# 65 | |||
# 95 | |||
# 102 | |||
# 117 | |||
# 150 | |||
# 182 | |||
# 127 | |||
# 219 | |||
# 299 | |||
# 277 | |||
# 309 | |||
# 576 | |||
# | |||
# In this example, after the 5-number preamble, almost every number is the sum | |||
# of two of the previous 5 numbers; the only number that does not follow this | |||
# rule is 127. | |||
# | |||
# The first step of attacking the weakness in the XMAS data is to find the | |||
# first number in the list (after the preamble) which is not the sum of two of | |||
# the 25 numbers before it. What is the first number that does not have this | |||
# property? | |||
library(tidyverse) | |||
test_input <- c(35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576) | |||
# input <- test_input | |||
input <- as.numeric(read_lines("../input/09-01.txt")) | |||
# preamble_length <- 5 | |||
preamble_length <- 25 | |||
window <- 1:preamble_length | |||
input_start <- preamble_length + 1 | |||
no_sum_found <- TRUE | |||
while(no_sum_found && (length(input)>=(preamble_length + 1))) { | |||
as.data.frame(t(combn(window, 2))) %>% | |||
mutate( | |||
val1 = input[V1], | |||
val2 = input[V2], | |||
sum = input[V1] + input[V2] | |||
) -> pair_sums | |||
no_sum_found <- any(pair_sums$sum == input[input_start]) | |||
if (no_sum_found) input <- tail(input, -1) | |||
} | |||
(invalid <- input[input_start]) | |||
# --- Part Two --- | |||
# | |||
# The final step in breaking the XMAS encryption relies on the invalid number | |||
# you just found: you must find a contiguous set of at least two numbers in | |||
# your list which sum to the invalid number from step 1. | |||
# | |||
# Again consider the above example: | |||
# | |||
# 35 | |||
# 20 | |||
# 15 | |||
# 25 | |||
# 47 | |||
# 40 | |||
# 62 | |||
# 55 | |||
# 65 | |||
# 95 | |||
# 102 | |||
# 117 | |||
# 150 | |||
# 182 | |||
# 127 | |||
# 219 | |||
# 299 | |||
# 277 | |||
# 309 | |||
# 576 | |||
# | |||
# In this list, adding up all of the numbers from 15 through 40 produces the | |||
# invalid number from step 1, 127. (Of course, the contiguous set of numbers | |||
# in your actual list might be much longer.) | |||
# | |||
# To find the encryption weakness, add together the smallest and largest | |||
# number in this contiguous range; in this example, these are 15 and 47, | |||
# producing 62. | |||
# | |||
# What is the encryption weakness in your XMAS-encrypted list of numbers? | |||
# s | |||
# 09-02 ------------------------------------------------------------------- | |||
# input <- test_input | |||
input <- as.numeric(read_lines("../input/09-01.txt")) | |||
for (i in seq_along(input)) { | |||
csum <- cumsum(input[i:length(input)]) | |||
if (any(csum == invalid)) break | |||
} | |||
sum(range(input[i:(i+which(csum == invalid)-1)])) |
@ -0,0 +1,610 @@ | |||
acc +0 | |||
jmp +516 | |||
acc -16 | |||
nop +326 | |||
jmp +366 | |||
jmp +400 | |||
jmp +494 | |||
acc -5 | |||
jmp +475 | |||
acc +29 | |||
nop +24 | |||
jmp +186 | |||
jmp -5 | |||
acc +50 | |||
acc +49 | |||
acc +49 | |||
nop +507 | |||
jmp +236 | |||
acc -4 | |||
acc +50 | |||
acc +48 | |||
jmp +293 | |||
jmp +461 | |||
acc +48 | |||
acc +2 | |||
jmp +241 | |||
acc +38 | |||
jmp +544 | |||
jmp +1 | |||
jmp +382 | |||
acc -4 | |||
acc -10 | |||
acc +44 | |||
jmp +444 | |||
jmp +1 | |||
acc +20 | |||
acc -1 | |||
acc +25 | |||
jmp +332 | |||
acc +18 | |||
jmp +184 | |||
acc +1 | |||
acc +20 | |||
acc -7 | |||
acc -12 | |||
jmp +84 | |||
acc +30 | |||
jmp -19 | |||
nop +491 | |||
acc +37 | |||
acc +44 | |||
jmp +220 | |||
acc +3 | |||
acc +19 | |||
jmp +485 | |||
jmp +222 | |||
nop +264 | |||
acc +4 | |||
jmp +151 | |||
nop -36 | |||
acc +48 | |||
jmp +356 | |||
acc +28 | |||
acc +29 | |||
nop +397 | |||
jmp +168 | |||
nop +115 | |||
acc -13 | |||
acc -11 | |||
jmp +470 | |||
jmp +201 | |||
acc +48 | |||
acc -6 | |||
acc -18 | |||
acc -13 | |||
jmp +442 | |||
jmp +266 | |||
acc +10 | |||
acc +30 | |||
acc -7 | |||
acc -8 | |||
jmp +380 | |||
acc +1 | |||
nop +106 | |||
acc +9 | |||
acc -11 | |||
jmp -79 | |||
jmp +166 | |||
acc -9 | |||
jmp +300 | |||
acc +39 | |||
acc +1 | |||
acc +27 | |||
acc +26 | |||
jmp +367 | |||
jmp +291 | |||
acc +18 | |||
jmp +100 | |||
acc +25 | |||
acc +43 | |||
acc -8 | |||
acc -12 | |||
jmp +475 | |||
acc +15 | |||
jmp +492 | |||
acc -5 | |||
acc -12 | |||
acc +35 | |||
acc +25 | |||
jmp +360 | |||
nop +301 | |||
acc +32 | |||
acc -1 | |||
jmp +436 | |||
acc +21 | |||
jmp +109 | |||
acc +1 | |||
nop +230 | |||
jmp -113 | |||
acc +24 | |||
acc +12 | |||
acc +13 | |||
acc +27 | |||
jmp -52 | |||
jmp +476 | |||
acc +15 | |||
acc -18 | |||
acc -13 | |||
jmp +323 | |||
acc -10 | |||
acc +35 | |||
acc -6 | |||
nop +377 | |||
jmp +8 | |||
jmp +430 | |||
nop +50 | |||
acc +33 | |||
acc -1 | |||
jmp -72 | |||
acc -19 | |||
jmp -92 | |||
jmp +1 | |||
jmp -87 | |||
nop -120 | |||
nop -34 | |||
jmp +159 | |||
jmp +183 | |||
jmp +1 | |||
acc -4 | |||
acc +49 | |||
acc -4 | |||
jmp +73 | |||
acc -18 | |||
acc +34 | |||
acc -8 | |||
jmp +403 | |||
acc +32 | |||
acc +0 | |||
acc +0 | |||
jmp +390 | |||
acc +19 | |||
jmp +305 | |||
acc -15 | |||
jmp +176 | |||
acc +3 | |||
jmp +36 | |||
acc +29 | |||
acc +21 | |||
jmp +1 | |||
acc -17 | |||
jmp +144 | |||
acc +16 | |||
acc +4 | |||
acc +49 | |||
acc -7 | |||
jmp -65 | |||
acc +28 | |||
acc +2 | |||
acc +23 | |||
acc +46 | |||
jmp +278 | |||
jmp +424 | |||
acc +45 | |||
acc +7 | |||
jmp -49 | |||
jmp -50 | |||
acc -11 | |||
nop -121 | |||
jmp -140 | |||
jmp +1 | |||
acc -8 | |||
jmp +206 | |||
acc -7 | |||
jmp +1 | |||
acc +20 | |||
acc +30 | |||
jmp +375 | |||
nop +312 | |||
jmp +111 | |||
acc +23 | |||
jmp -15 | |||
acc +27 | |||
acc +46 | |||
acc +36 | |||
jmp +360 | |||
acc +1 | |||
acc +25 | |||
acc +29 | |||
jmp -137 | |||
jmp +177 | |||
acc +15 | |||
acc -13 | |||
acc +48 | |||
acc +29 | |||
jmp +76 | |||
nop -174 | |||
acc +27 | |||
jmp +214 | |||
acc +4 | |||
jmp +120 | |||
nop -165 | |||
acc -16 | |||
jmp +2 | |||
jmp +238 | |||
jmp +224 | |||
jmp -154 | |||
acc +12 | |||
jmp +162 | |||
acc +31 | |||
acc +26 | |||
acc -13 | |||
acc +18 | |||
jmp +173 | |||
acc +38 | |||
jmp +1 | |||
jmp +199 | |||
jmp -202 | |||
jmp +127 | |||
acc -12 | |||
jmp -42 | |||
acc -12 | |||
acc +36 | |||
acc -4 | |||
acc +24 | |||
jmp +343 | |||
acc -17 | |||
nop +277 | |||
acc +13 | |||
jmp +56 | |||
jmp +41 | |||
nop +92 | |||
acc +32 | |||
jmp +182 | |||
nop +111 | |||
acc +0 | |||
acc +29 | |||
jmp +219 | |||
nop -234 | |||
acc +0 | |||
acc +31 | |||
acc +49 | |||
jmp +216 | |||
jmp +247 | |||
acc +24 | |||
acc -10 | |||
jmp +218 | |||
acc +14 | |||
acc -19 | |||
acc +14 | |||
jmp +11 | |||
jmp +82 | |||
acc +5 | |||
acc +28 | |||
acc +35 | |||
jmp -93 | |||
acc +24 | |||
jmp -210 | |||
acc +39 | |||
jmp -107 | |||
jmp -150 | |||
acc -7 | |||
jmp -185 | |||
acc +25 | |||
acc -15 | |||
jmp +63 | |||
acc +14 | |||
acc +50 | |||
jmp -7 | |||
acc +13 | |||
jmp +298 | |||
nop +69 | |||
jmp -268 | |||
acc -5 | |||
jmp +66 | |||
jmp +48 | |||
nop -1 | |||
acc +38 | |||
acc +3 | |||
acc +46 | |||
jmp +259 | |||
nop +235 | |||
acc +4 | |||
jmp -186 | |||
jmp +94 | |||
acc -16 | |||
acc +6 | |||
jmp +287 | |||
acc -19 | |||
jmp -221 | |||
jmp -69 | |||
acc +8 | |||
nop -102 | |||
acc +31 | |||
jmp +204 | |||
jmp -81 | |||
acc +12 | |||
acc +21 | |||
acc -8 | |||
nop +151 | |||
jmp -155 | |||
acc +50 | |||
nop +145 | |||
acc -6 | |||
acc +0 | |||
jmp -296 | |||
acc +16 | |||
acc +30 | |||
acc +11 | |||
jmp +254 | |||
acc +0 | |||
acc -8 | |||
nop +162 | |||
acc -17 | |||
jmp -144 | |||
acc +6 | |||
acc +2 | |||
acc +32 | |||
acc -5 | |||
jmp +21 | |||
jmp -77 | |||
acc +38 | |||
jmp +117 | |||
acc +27 | |||
acc -11 | |||
nop -203 | |||
jmp +201 | |||
jmp -305 | |||
acc +40 | |||
nop -330 | |||
acc -7 | |||
jmp -279 | |||
jmp +60 | |||
nop -163 | |||
jmp -53 | |||
nop +115 | |||
acc +24 | |||
jmp +1 | |||
acc +45 | |||
jmp -92 | |||
acc +0 | |||
acc +13 | |||
jmp -302 | |||
acc +25 | |||
jmp +208 | |||
nop -293 | |||
jmp +5 | |||
jmp +1 | |||
acc -13 | |||
nop -272 | |||
jmp +62 | |||
acc -4 | |||
acc +32 | |||
acc +49 | |||
jmp -124 | |||
acc -19 | |||
nop -246 | |||
jmp -353 | |||
nop -349 | |||
acc +13 | |||
acc +21 | |||
jmp -165 | |||
acc +14 | |||
acc +37 | |||
nop +28 | |||
acc +7 | |||
jmp -308 | |||
acc +11 | |||
jmp +71 | |||
jmp -139 | |||
nop -19 | |||
acc -7 | |||
acc -3 | |||
jmp -302 | |||
acc -2 | |||
acc +1 | |||
nop -43 | |||
jmp -280 | |||
acc +10 | |||
acc +9 | |||
jmp -358 | |||
acc +39 | |||
acc +16 | |||
acc -11 | |||
jmp +1 | |||
jmp +142 | |||
acc -15 | |||
acc -17 | |||
acc +9 | |||
acc +11 | |||
jmp -172 | |||
jmp -90 | |||
nop +135 | |||
acc -1 | |||
jmp -379 | |||
acc +39 | |||
acc -13 | |||
jmp -57 | |||
acc -3 | |||
acc -7 | |||
jmp +1 | |||
jmp +80 | |||
acc +7 | |||
acc +11 | |||
acc +30 | |||
nop +69 | |||
jmp -140 | |||
acc +11 | |||
acc +7 | |||
acc +3 | |||
jmp -417 | |||
jmp -314 | |||
acc -6 | |||
jmp -6 | |||
jmp -224 | |||
acc +0 | |||
jmp -131 | |||
acc +7 | |||
acc +33 | |||
jmp +20 | |||
acc +32 | |||
acc -14 | |||
nop -375 | |||
jmp -394 | |||
acc +8 | |||
acc +47 | |||
acc +29 | |||
acc -6 | |||
jmp -381 | |||
acc -8 | |||
jmp -353 | |||
jmp -108 | |||
acc -16 | |||
acc +21 | |||
jmp -329 | |||
acc +13 | |||
acc +0 | |||
acc +0 | |||
jmp -402 | |||
acc -8 | |||
jmp -307 | |||
jmp -308 | |||
jmp -358 | |||
acc +38 | |||
acc -19 | |||
acc +33 | |||
jmp -276 | |||
jmp -114 | |||
acc -6 | |||
jmp +55 | |||
acc +16 | |||
jmp -244 | |||
nop +22 | |||
acc -7 | |||
acc +19 | |||
jmp -154 | |||
jmp +18 | |||
jmp -50 | |||
acc +31 | |||
acc +50 | |||
acc +2 | |||
jmp -260 | |||
acc -16 | |||
jmp -135 | |||
acc +50 | |||
acc -9 | |||
acc +50 | |||
acc +27 | |||
jmp -323 | |||
acc +26 | |||
nop -249 | |||
acc +46 | |||
acc +3 | |||
jmp -474 | |||
acc +50 | |||
acc -7 | |||
jmp -78 | |||
jmp +1 | |||
acc -6 | |||
acc +6 | |||
jmp +94 | |||
acc +47 | |||
acc -3 | |||
acc +13 | |||
jmp -485 | |||
nop -499 | |||
acc +35 | |||
acc +35 | |||
acc -1 | |||
jmp -259 | |||
nop -486 | |||
acc +48 | |||
acc -4 | |||
jmp +23 | |||
acc +33 | |||
acc +37 | |||
acc +10 | |||
jmp +61 | |||
acc -1 | |||
jmp +1 | |||
jmp +1 | |||
acc -8 | |||
jmp +61 | |||
jmp -237 | |||
jmp -164 | |||
acc +39 | |||
acc +38 | |||
nop -302 | |||
jmp -468 | |||
acc -15 | |||
jmp -6 | |||
acc -9 | |||
acc +1 | |||
acc +41 | |||
acc +48 | |||
jmp -285 | |||
acc +1 | |||
acc +24 | |||
jmp -111 | |||
jmp -90 | |||
acc +46 | |||
acc +21 | |||
acc +43 | |||
jmp -103 | |||
acc -9 | |||
nop -5 | |||
jmp -274 | |||
acc -13 | |||
jmp -262 | |||
jmp -39 | |||
acc +43 | |||
nop -463 | |||
acc +15 | |||
acc +23 | |||
jmp -176 | |||
nop -305 | |||
acc +2 | |||
acc +28 | |||
jmp -198 | |||
acc +15 | |||
acc -18 | |||
jmp -473 | |||
acc -13 | |||
acc +9 | |||
jmp -46 | |||
acc +1 | |||
acc +43 | |||
acc +6 | |||
jmp +10 | |||
acc +24 | |||
acc -3 | |||
jmp -424 | |||
acc -1 | |||
acc +47 | |||
jmp +1 | |||
acc +20 | |||
jmp -17 | |||
jmp -59 | |||
nop -19 | |||
acc +42 | |||
acc +47 | |||
jmp -9 | |||
jmp -576 | |||
nop -148 | |||
acc +0 | |||
jmp +12 | |||
acc -15 | |||
jmp -209 | |||
acc +13 | |||
jmp -442 | |||
nop -548 | |||
nop -62 | |||
acc +22 | |||
jmp -92 | |||
jmp -516 | |||
acc +8 | |||
jmp -301 | |||
acc +1 | |||
jmp -146 | |||
acc -15 | |||
jmp -366 | |||
jmp -440 | |||
acc +17 | |||
acc +25 | |||
acc +28 | |||
jmp -240 | |||
acc -2 | |||
acc +25 | |||
nop -68 | |||
acc +14 | |||
jmp +1 |