2020 Day 08, fix typo in test of 07

This commit is contained in:
Akumatic 2020-12-08 11:20:43 +01:00
parent 97125d8b50
commit ab66e38e47
7 changed files with 775 additions and 2 deletions

View File

@ -27,5 +27,5 @@ def test():
rules, amount = parseRules(input)
assert part2(rules, amount) == 126
if __name__ == "__name__":
if __name__ == "__main__":
test()

95
2020/08/README.md Normal file
View File

@ -0,0 +1,95 @@
# 2020 Day 1: Handheld Halting
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2020/day/8)
## Part 1
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](https://en.wikipedia.org/wiki/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?**
## Part 2
After some careful analysis, you believe that **exactly one instruction is corrupted**.
Somewhere in the program, **either** a `jmp` is supposed to be a `nop`, **or** a `nop` is supposed to be a `jmp`. (No `acc` instructions were harmed in the corruption of this boot code.)
The program is supposed to terminate by **attempting to execute an instruction immediately after the last instruction** in the file. By changing exactly one `jmp` or `nop`, you can repair the boot code and make it terminate correctly.
For example, consider the same program from above:
```
nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6
```
If you change the first instruction from `nop +0` to `jmp +0`, it would create a single-instruction infinite loop, never leaving that instruction. If you change almost any of the `jmp` instructions, the program will still eventually find another `jmp` instruction and loop forever.
However, if you change the second-to-last instruction (from `jmp -4` to `nop -4`), the program terminates! The instructions are visited in this order:
```
nop +0 | 1
acc +1 | 2
jmp +4 | 3
acc +3 |
jmp -3 |
acc -99 |
acc +1 | 4
nop -4 | 5
acc +6 | 6
```
After the last instruction (`acc +6`), the program terminates by attempting to run the instruction below the last instruction in the file. With this change, after the program terminates, the accumulator contains the value **`8`** (`acc +1`, `acc +1`, `acc +6`).
Fix the program so that it terminates normally by changing exactly one `jmp` (to `nop`) or `nop` (to `jmp`). **What is the value of the accumulator after the program terminates?**

52
2020/08/code.py Normal file
View File

@ -0,0 +1,52 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2020 Akumatic
#
#https://adventofcode.com/2020/day/8
def readFile() -> list:
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
return [line.split() for line in f.read().strip().split("\n")]
def run(instructions: list) -> tuple:
accumulator = 0
pointer = 0
visited = []
size = len(instructions)
while pointer not in visited and pointer != size:
visited.append(pointer)
if instructions[pointer][0] == "acc":
accumulator += int(instructions[pointer][1])
pointer += 1
elif instructions[pointer][0] == "jmp":
pointer += int(instructions[pointer][1])
else: # instructions[pointer][0] == "nop":
pointer += 1
return pointer, accumulator
def part1(instructions: list) -> int:
return run(instructions)[1]
def part2(instructions: list) -> int:
size = len(instructions)
for i in range(size):
if instructions[i][0] == "jmp":
instructions[i][0] = "nop"
pointer, accumulator = run(instructions)
instructions[i][0] = "jmp"
elif instructions[i][0] == "nop":
instructions[i][0] = "jmp"
pointer, accumulator = run(instructions)
instructions[i][0] = "nop"
else: # instructions[i][0] == "acc"
continue
if pointer == size:
return accumulator
if __name__ == "__main__":
instructions = readFile()
print(f"Part 1: {part1(instructions)}")
print(f"Part 2: {part2(instructions)}")

611
2020/08/input.txt Normal file
View File

@ -0,0 +1,611 @@
acc +22
acc +42
nop +456
jmp +5
acc +31
acc +49
acc +10
jmp +519
nop +390
jmp +418
nop +29
acc -4
jmp +156
jmp +85
acc +5
acc +26
jmp +497
acc -6
acc -18
acc +20
acc +4
jmp -8
jmp +372
jmp +371
jmp -1
jmp +1
nop +378
acc +18
jmp +388
jmp +1
acc +29
acc +37
jmp +1
jmp +425
acc +19
acc +13
jmp +477
acc +7
jmp +469
nop +495
nop +141
acc +22
jmp +517
jmp +125
nop +30
acc +37
acc +23
nop +238
jmp +110
jmp +411
acc +2
acc -19
acc -19
jmp +296
acc +0
acc +14
acc +20
jmp +75
nop +88
acc -16
acc +40
acc +27
jmp +131
acc +33
nop +252
acc +5
acc +0
jmp +101
nop +219
acc +50
acc +40
jmp +49
nop +74
jmp +327
acc +47
jmp +206
acc -15
jmp +449
acc -17
acc -13
acc +46
jmp +417
jmp +160
acc -7
acc -11
acc +16
acc +14
jmp -37
acc -12
acc +15
acc -14
nop +110
jmp +1
acc -4
nop +287
nop -82
jmp +30
jmp +490
acc +34
jmp +305
nop +90
jmp +1
nop -4
nop -95
jmp -46
acc +26
acc +13
acc +47
jmp +350
acc +11
jmp -102
acc -2
jmp +489
acc +28
acc +24
nop +486
jmp +485
nop +170
jmp +66
jmp +411
acc +30
acc +48
acc +48
jmp -6
acc +11
jmp -51
jmp +1
jmp -10
nop +411
acc -17
acc +32
jmp +9
jmp +398
nop +82
jmp +6
acc +45
acc +34
jmp -44
acc -13
jmp -122
acc +25
nop +286
acc +5
jmp +144
acc +0
jmp -122
acc -11
acc -6
jmp -123
acc +16
acc +1
jmp -58
nop +242
acc -11
jmp +257
nop +231
acc +46
jmp +301
acc -6
acc +20
acc -7
jmp +365
acc +32
acc +0
jmp -66
jmp +110
acc -18
jmp +118
acc +33
nop -125
acc +49
acc +36
jmp +188
acc +9
acc -11
jmp +100
acc +35
jmp +55
acc +38
acc -1
jmp +312
jmp +157
acc +17
jmp +177
nop -126
acc +30
acc -3
jmp +211
acc -3
jmp -164
jmp -112
acc +50
jmp +268
nop +290
acc -8
acc +35
jmp -44
acc -6
acc +11
nop +327
jmp +155
acc +10
acc +35
nop +233
jmp +330
acc +31
acc +8
jmp +124
acc -5
jmp +300
nop +171
nop +4
acc +19
acc +41
jmp -156
nop +179
acc +12
jmp +160
jmp -92
acc -11
acc -10
jmp +95
nop +94
acc -8
jmp -199
acc +16
acc +30
nop +73
acc +36
jmp -53
jmp +1
jmp -6
nop +369
acc +29
acc +47
jmp +32
acc +35
jmp -61
acc +41
jmp +352
acc -1
jmp +75
acc -10
acc +28
acc -15
jmp -187
acc +6
jmp +1
nop +112
jmp +273
nop +186
acc +11
acc +40
jmp +128
acc +17
acc +23
acc -8
nop +277
jmp +42
acc +11
nop -237
acc +36
acc +32
jmp +287
acc +16
acc -19
jmp +115
acc -6
acc +16
nop -2
acc +23
jmp -160
acc -10
acc -10
jmp +26
acc -7
jmp -95
nop -160
acc -2
acc +44
jmp -236
jmp -198
jmp +1
acc +1
jmp -9
jmp -95
jmp +273
acc -19
jmp -46
acc +12
acc +2
jmp -145
acc -14
acc +3
acc +3
jmp +250
acc +4
acc +40
jmp +1
jmp +17
acc +6
acc +47
jmp -77
nop -192
acc +11
jmp +296
acc -14
jmp +64
acc +35
jmp +134
acc -8
nop +228
acc +24
acc +15
jmp -64
jmp -241
acc +19
acc +22
acc +49
nop -193
jmp +219
acc -1
acc -11
nop +211
acc +0
jmp -106
nop +101
jmp -222
acc +20
acc +45
jmp +70
acc +19
acc +21
jmp -23
acc +8
nop +92
acc +47
jmp -144
acc +0
acc -1
jmp -81
acc +23
jmp -274
acc +14
acc +26
acc +9
jmp +79
acc +22
jmp -331
acc -10
jmp -311
acc +16
acc +30
acc -8
jmp +176
acc -19
acc +43
jmp -222
nop -116
jmp +18
acc +26
acc +23
acc +6
jmp -162
acc +34
jmp +95
acc +27
acc +40
acc +9
jmp -77
jmp +137
acc -13
acc +21
acc +17
acc -5
jmp +91
jmp -95
acc +18
acc -1
jmp +70
jmp -355
nop -166
acc -19
acc +16
jmp -146
jmp -135
jmp +57
acc +45
jmp -62
acc -14
jmp -382
nop -172
acc +45
jmp -77
acc +13
jmp +65
acc -4
jmp +112
jmp +107
jmp +26
jmp -326
acc +25
jmp +1
jmp +179
acc +33
acc +2
jmp -222
nop +36
acc +25
nop -244
jmp -376
jmp -203
acc +26
nop +109
acc +38
jmp +135
acc +7
acc +40
acc -18
jmp -113
nop -294
acc +0
acc +40
nop -265
jmp +81
jmp -99
jmp +32
acc -17
acc +25
acc -12
acc +26
jmp -125
acc -3
acc -7
acc +25
jmp -410
acc +47
acc +36
jmp +35
acc +2
acc +18
acc -3
jmp -38
acc +29
acc +49
jmp -299
acc -4
nop -422
jmp +50
acc +11
acc +2
acc +49
jmp -233
acc +12
acc +43
acc -19
acc +11
jmp -264
jmp +124
jmp -361
acc +35
jmp -118
acc +23
acc -16
acc -14
jmp -22
jmp -135
jmp -309
acc +6
jmp -44
acc -12
acc +0
jmp -23
acc +29
acc -8
acc +18
acc +35
jmp -111
acc +22
acc +23
acc +0
acc -8
jmp -55
acc +14
jmp +1
acc +44
acc +17
jmp -272
acc +39
nop +37
acc -19
jmp -323
acc +24
acc +28
acc +29
acc +37
jmp +110
jmp -386
nop -352
acc +23
acc +38
jmp -369
acc -5
acc -14
jmp +83
jmp +17
jmp -151
jmp -118
jmp -104
jmp -341
acc +32
acc +43
jmp -52
acc -4
acc +42
acc +5
jmp -116
acc +13
jmp +1
nop -361
acc +41
jmp -386
jmp -241
nop -449
acc +46
jmp -176
acc +6
jmp +60
jmp +1
jmp -3
jmp -62
acc -14
acc +17
jmp -340
acc +31
acc -13
acc +7
jmp -54
jmp -80
acc +14
acc +49
acc +34
jmp +24
acc +11
jmp -158
acc -13
jmp -261
acc +33
nop -171
jmp -106
acc +0
acc +9
acc +16
acc +34
jmp +18
acc -2
acc +47
acc +39
jmp -232
acc +23
nop -229
acc +30
acc +32
jmp -147
acc -8
jmp -460
jmp -498
nop -218
acc +31
acc +44
acc +30
jmp -105
acc +8
acc -19
acc +45
nop -49
jmp -140
nop -43
acc +42
jmp +1
acc -14
jmp -42
jmp -389
acc +39
acc +26
acc +38
jmp -77
acc +48
jmp -83
acc +5
jmp -81
nop -242
acc +35
acc +0
acc +19
jmp -430
acc +11
nop -226
acc +13
acc +23
jmp -575
acc +44
acc +50
nop -303
jmp -112
jmp -305
acc +23
acc -11
nop -376
acc +50
jmp +1

2
2020/08/solution.txt Normal file
View File

@ -0,0 +1,2 @@
Part 1: 1782
Part 2: 797

13
2020/08/test_code.py Normal file
View File

@ -0,0 +1,13 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2020 Akumatic
from code import part1, part2
def test():
instructions = [["nop", "+0"], ["acc", "+1"], ["jmp", "+4"],
["acc", "+3"], ["jmp", "-3"], ["acc", "-99"], ["acc", "+1"], ["jmp", "-4"], ["acc", "+6"]]
assert(part1(instructions) == 5)
assert part2(instructions) == 8
if __name__ == "__main__":
test()

View File

@ -18,7 +18,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
| 01 | :white_check_mark: | :white_check_mark: || 02 | :white_check_mark: | :white_check_mark: |
| 03 | :white_check_mark: | :white_check_mark: || 04 | :white_check_mark: | :white_check_mark: |
| 05 | :white_check_mark: | :white_check_mark: || 06 | :white_check_mark: | :white_check_mark: |
| 07 | :white_check_mark: | :white_check_mark: || 08 | | |
| 07 | :white_check_mark: | :white_check_mark: || 08 | :white_check_mark: | :white_check_mark: |
| 09 | | || 10 | | |
| 11 | | || 12 | | |
| 13 | | || 14 | | |