rename repo, subdir for yearly challenges
This commit is contained in:
656
2020/day08/INPUT.txt
Normal file
656
2020/day08/INPUT.txt
Normal file
@@ -0,0 +1,656 @@
|
||||
acc -7
|
||||
acc +2
|
||||
acc +20
|
||||
acc +14
|
||||
jmp +191
|
||||
acc +47
|
||||
nop +339
|
||||
acc +49
|
||||
jmp +104
|
||||
jmp +629
|
||||
jmp +374
|
||||
acc +24
|
||||
jmp +220
|
||||
nop +474
|
||||
acc +25
|
||||
jmp +340
|
||||
acc +16
|
||||
acc +3
|
||||
acc +41
|
||||
jmp +566
|
||||
jmp +296
|
||||
acc +15
|
||||
jmp +452
|
||||
acc +21
|
||||
jmp +129
|
||||
acc +10
|
||||
acc -8
|
||||
acc +39
|
||||
jmp +396
|
||||
acc +5
|
||||
acc -4
|
||||
acc +0
|
||||
jmp +496
|
||||
nop +181
|
||||
acc +48
|
||||
acc +7
|
||||
jmp +1
|
||||
jmp +370
|
||||
acc +16
|
||||
acc -18
|
||||
acc +47
|
||||
acc +48
|
||||
jmp +99
|
||||
nop +17
|
||||
acc +25
|
||||
acc -15
|
||||
jmp +285
|
||||
nop +545
|
||||
nop +147
|
||||
nop +479
|
||||
acc -4
|
||||
jmp +386
|
||||
acc +36
|
||||
acc -12
|
||||
jmp +50
|
||||
acc +37
|
||||
nop +133
|
||||
acc +11
|
||||
acc +20
|
||||
jmp +32
|
||||
jmp +1
|
||||
nop +210
|
||||
acc -15
|
||||
acc -6
|
||||
jmp +446
|
||||
acc +25
|
||||
acc +1
|
||||
acc +17
|
||||
acc -4
|
||||
jmp +355
|
||||
acc -4
|
||||
jmp +292
|
||||
acc +16
|
||||
acc +44
|
||||
acc +26
|
||||
jmp +157
|
||||
acc -18
|
||||
acc +15
|
||||
acc -8
|
||||
acc -3
|
||||
jmp +46
|
||||
acc +30
|
||||
acc +16
|
||||
jmp -7
|
||||
acc +34
|
||||
jmp +515
|
||||
acc +11
|
||||
acc -8
|
||||
acc -9
|
||||
acc -3
|
||||
jmp +548
|
||||
jmp +278
|
||||
nop +332
|
||||
acc -19
|
||||
acc +49
|
||||
jmp +536
|
||||
acc -9
|
||||
acc +46
|
||||
jmp +124
|
||||
acc +41
|
||||
acc +47
|
||||
acc -5
|
||||
acc -13
|
||||
jmp +41
|
||||
nop +178
|
||||
acc +12
|
||||
acc +45
|
||||
jmp +461
|
||||
acc +37
|
||||
acc +12
|
||||
acc +38
|
||||
jmp -68
|
||||
acc -6
|
||||
nop +494
|
||||
acc -9
|
||||
jmp -63
|
||||
acc +42
|
||||
acc +16
|
||||
acc +30
|
||||
jmp +70
|
||||
acc +13
|
||||
jmp +1
|
||||
acc -18
|
||||
jmp +528
|
||||
acc +48
|
||||
jmp +493
|
||||
nop +402
|
||||
jmp +381
|
||||
acc -8
|
||||
jmp +372
|
||||
acc +20
|
||||
acc +25
|
||||
jmp +425
|
||||
acc -10
|
||||
jmp +510
|
||||
jmp +439
|
||||
nop +78
|
||||
acc +36
|
||||
acc +7
|
||||
nop +281
|
||||
jmp +504
|
||||
jmp -108
|
||||
acc +40
|
||||
jmp -122
|
||||
acc +23
|
||||
acc -2
|
||||
acc +7
|
||||
jmp +370
|
||||
acc +25
|
||||
nop -5
|
||||
acc +33
|
||||
acc +37
|
||||
jmp +70
|
||||
acc -6
|
||||
nop +336
|
||||
jmp +34
|
||||
jmp +1
|
||||
acc -18
|
||||
jmp +473
|
||||
jmp +1
|
||||
acc +20
|
||||
acc +4
|
||||
acc +25
|
||||
jmp -87
|
||||
acc -12
|
||||
acc +47
|
||||
acc +49
|
||||
jmp +323
|
||||
jmp +1
|
||||
jmp +1
|
||||
jmp +167
|
||||
acc -10
|
||||
acc +45
|
||||
jmp +355
|
||||
acc +32
|
||||
acc +38
|
||||
acc +2
|
||||
jmp -93
|
||||
acc +8
|
||||
acc +20
|
||||
acc +4
|
||||
acc -1
|
||||
jmp +108
|
||||
nop +164
|
||||
acc +41
|
||||
jmp +440
|
||||
acc -16
|
||||
acc +47
|
||||
jmp +355
|
||||
acc -13
|
||||
acc +29
|
||||
acc +50
|
||||
jmp -101
|
||||
acc -8
|
||||
jmp +316
|
||||
acc +27
|
||||
acc +31
|
||||
nop -29
|
||||
jmp +1
|
||||
jmp +250
|
||||
acc +12
|
||||
acc -13
|
||||
jmp +73
|
||||
jmp +72
|
||||
acc +36
|
||||
acc +44
|
||||
jmp +1
|
||||
jmp -33
|
||||
acc -18
|
||||
acc +16
|
||||
acc -8
|
||||
acc +6
|
||||
jmp +104
|
||||
jmp +295
|
||||
acc +10
|
||||
nop -80
|
||||
jmp +74
|
||||
acc -13
|
||||
jmp +1
|
||||
acc +22
|
||||
acc +50
|
||||
jmp +280
|
||||
jmp +265
|
||||
jmp +278
|
||||
acc +46
|
||||
acc -14
|
||||
acc -17
|
||||
jmp -19
|
||||
acc +39
|
||||
acc +31
|
||||
acc -11
|
||||
jmp +400
|
||||
jmp +80
|
||||
acc +0
|
||||
acc +27
|
||||
nop +209
|
||||
jmp -184
|
||||
acc +12
|
||||
acc +21
|
||||
acc +23
|
||||
jmp +352
|
||||
acc +29
|
||||
jmp -5
|
||||
acc +15
|
||||
acc +7
|
||||
jmp +6
|
||||
acc +31
|
||||
acc -5
|
||||
nop +83
|
||||
acc +31
|
||||
jmp -239
|
||||
acc +8
|
||||
acc -2
|
||||
acc +49
|
||||
acc -12
|
||||
jmp -52
|
||||
acc -15
|
||||
acc -14
|
||||
jmp +126
|
||||
jmp +385
|
||||
acc +30
|
||||
acc -5
|
||||
acc +6
|
||||
jmp -187
|
||||
acc +39
|
||||
acc +40
|
||||
acc +0
|
||||
acc +6
|
||||
jmp +24
|
||||
acc +20
|
||||
jmp +131
|
||||
jmp -127
|
||||
acc +8
|
||||
acc +30
|
||||
jmp -265
|
||||
acc -2
|
||||
jmp -265
|
||||
acc +22
|
||||
acc -19
|
||||
acc -9
|
||||
nop +10
|
||||
jmp +148
|
||||
acc -14
|
||||
acc +38
|
||||
acc +50
|
||||
acc -7
|
||||
jmp +197
|
||||
acc +11
|
||||
acc +22
|
||||
jmp +201
|
||||
jmp -155
|
||||
jmp -32
|
||||
acc +48
|
||||
nop -50
|
||||
jmp -99
|
||||
jmp -5
|
||||
acc +11
|
||||
acc -18
|
||||
jmp -186
|
||||
acc +6
|
||||
acc +43
|
||||
jmp +159
|
||||
jmp +249
|
||||
acc +44
|
||||
acc +29
|
||||
nop +313
|
||||
acc +23
|
||||
jmp +311
|
||||
jmp +152
|
||||
acc +0
|
||||
acc +41
|
||||
jmp -251
|
||||
jmp +102
|
||||
nop -17
|
||||
nop +176
|
||||
jmp +40
|
||||
acc +28
|
||||
jmp -21
|
||||
acc -4
|
||||
acc -10
|
||||
acc -19
|
||||
acc -15
|
||||
jmp +23
|
||||
nop +144
|
||||
acc +9
|
||||
acc +18
|
||||
jmp +141
|
||||
acc -19
|
||||
acc -10
|
||||
acc +48
|
||||
jmp -7
|
||||
acc +46
|
||||
acc -9
|
||||
jmp -174
|
||||
acc +30
|
||||
acc +30
|
||||
jmp -201
|
||||
acc +34
|
||||
acc +24
|
||||
acc +37
|
||||
acc +44
|
||||
jmp -158
|
||||
acc +4
|
||||
acc +39
|
||||
jmp -52
|
||||
jmp -329
|
||||
jmp +68
|
||||
acc +25
|
||||
nop -105
|
||||
acc -15
|
||||
acc +34
|
||||
jmp -6
|
||||
jmp +1
|
||||
acc +1
|
||||
jmp +163
|
||||
nop -285
|
||||
acc +8
|
||||
acc +48
|
||||
jmp +143
|
||||
acc -3
|
||||
nop -269
|
||||
acc -16
|
||||
jmp -310
|
||||
acc -5
|
||||
jmp -304
|
||||
acc +45
|
||||
nop -231
|
||||
jmp +1
|
||||
jmp +245
|
||||
nop -243
|
||||
jmp +187
|
||||
acc -6
|
||||
acc +7
|
||||
acc +17
|
||||
acc +6
|
||||
jmp -111
|
||||
acc +24
|
||||
acc -10
|
||||
acc +21
|
||||
jmp -97
|
||||
jmp +1
|
||||
acc -12
|
||||
acc +10
|
||||
jmp +127
|
||||
acc +0
|
||||
jmp -211
|
||||
acc -11
|
||||
acc +36
|
||||
acc +45
|
||||
acc -19
|
||||
jmp -182
|
||||
jmp -366
|
||||
acc +38
|
||||
acc -11
|
||||
acc +32
|
||||
jmp -260
|
||||
acc +6
|
||||
acc +31
|
||||
jmp +3
|
||||
acc +5
|
||||
jmp +101
|
||||
jmp -64
|
||||
acc +48
|
||||
acc +5
|
||||
nop +40
|
||||
acc -13
|
||||
jmp +95
|
||||
nop +76
|
||||
acc +44
|
||||
acc +43
|
||||
acc +43
|
||||
jmp +196
|
||||
acc +34
|
||||
jmp +161
|
||||
acc +5
|
||||
acc +45
|
||||
acc +7
|
||||
jmp +20
|
||||
acc +13
|
||||
jmp -127
|
||||
acc +5
|
||||
acc +18
|
||||
jmp -239
|
||||
jmp -76
|
||||
nop +214
|
||||
jmp -284
|
||||
acc +10
|
||||
acc -8
|
||||
jmp -81
|
||||
acc +48
|
||||
acc -3
|
||||
jmp -55
|
||||
nop -288
|
||||
acc +37
|
||||
acc +1
|
||||
acc -12
|
||||
jmp +1
|
||||
nop +91
|
||||
acc +20
|
||||
acc +18
|
||||
jmp +4
|
||||
acc -7
|
||||
acc -10
|
||||
jmp -229
|
||||
nop -230
|
||||
nop +45
|
||||
acc +37
|
||||
jmp +127
|
||||
jmp +69
|
||||
jmp -153
|
||||
acc -15
|
||||
acc -19
|
||||
acc +32
|
||||
jmp -33
|
||||
nop +164
|
||||
acc +32
|
||||
jmp -133
|
||||
acc +20
|
||||
acc -8
|
||||
jmp +8
|
||||
acc -11
|
||||
nop +82
|
||||
acc +7
|
||||
acc +40
|
||||
jmp +79
|
||||
acc +0
|
||||
jmp +159
|
||||
acc +4
|
||||
acc -8
|
||||
acc +20
|
||||
nop +143
|
||||
jmp -351
|
||||
acc -7
|
||||
jmp +78
|
||||
acc +0
|
||||
acc +4
|
||||
jmp +20
|
||||
jmp -3
|
||||
acc +2
|
||||
acc +23
|
||||
jmp -256
|
||||
acc +33
|
||||
jmp -473
|
||||
acc +29
|
||||
acc -13
|
||||
jmp +77
|
||||
jmp +158
|
||||
acc -16
|
||||
jmp -10
|
||||
jmp -181
|
||||
jmp -135
|
||||
nop -95
|
||||
acc +46
|
||||
acc +39
|
||||
acc -3
|
||||
jmp -94
|
||||
jmp -67
|
||||
acc +49
|
||||
nop -78
|
||||
nop -9
|
||||
jmp +107
|
||||
acc -19
|
||||
acc -1
|
||||
acc +0
|
||||
acc -4
|
||||
jmp -189
|
||||
acc +11
|
||||
jmp -106
|
||||
jmp -200
|
||||
jmp +122
|
||||
acc +8
|
||||
acc +48
|
||||
acc +15
|
||||
acc +0
|
||||
jmp -493
|
||||
acc +13
|
||||
jmp -8
|
||||
acc +36
|
||||
acc -10
|
||||
jmp +1
|
||||
acc +9
|
||||
jmp +7
|
||||
jmp +85
|
||||
acc +22
|
||||
acc -8
|
||||
nop -124
|
||||
jmp -517
|
||||
jmp -338
|
||||
acc +39
|
||||
nop -438
|
||||
acc -11
|
||||
jmp +69
|
||||
acc +8
|
||||
acc +34
|
||||
acc +34
|
||||
acc -9
|
||||
jmp -205
|
||||
nop -528
|
||||
jmp -495
|
||||
acc +47
|
||||
acc +40
|
||||
acc +30
|
||||
jmp -328
|
||||
acc -2
|
||||
acc +41
|
||||
jmp -475
|
||||
acc +42
|
||||
acc +48
|
||||
acc +2
|
||||
acc +7
|
||||
jmp -415
|
||||
nop -249
|
||||
acc -3
|
||||
jmp +65
|
||||
acc +23
|
||||
nop -4
|
||||
jmp -254
|
||||
acc -12
|
||||
acc +22
|
||||
acc +27
|
||||
jmp -176
|
||||
jmp -408
|
||||
acc -15
|
||||
acc +14
|
||||
acc +30
|
||||
acc +0
|
||||
jmp -363
|
||||
jmp -426
|
||||
acc +38
|
||||
nop -425
|
||||
jmp -440
|
||||
jmp +1
|
||||
acc +22
|
||||
jmp -63
|
||||
jmp -406
|
||||
nop -445
|
||||
acc -5
|
||||
acc +34
|
||||
nop -425
|
||||
jmp +65
|
||||
acc +33
|
||||
jmp -91
|
||||
acc -12
|
||||
jmp +1
|
||||
jmp -541
|
||||
nop -489
|
||||
jmp -490
|
||||
acc +20
|
||||
acc +20
|
||||
acc +38
|
||||
acc -18
|
||||
jmp -548
|
||||
acc +43
|
||||
acc -7
|
||||
jmp -351
|
||||
acc -9
|
||||
acc +50
|
||||
acc +1
|
||||
nop -587
|
||||
jmp -230
|
||||
jmp +1
|
||||
nop +43
|
||||
jmp -65
|
||||
acc +31
|
||||
acc +5
|
||||
acc +1
|
||||
jmp -105
|
||||
nop -477
|
||||
acc +21
|
||||
nop -92
|
||||
jmp -263
|
||||
acc +28
|
||||
jmp -265
|
||||
jmp -311
|
||||
acc +2
|
||||
acc +23
|
||||
acc +50
|
||||
jmp -4
|
||||
acc +42
|
||||
acc +42
|
||||
acc +31
|
||||
jmp -167
|
||||
acc +49
|
||||
acc +46
|
||||
jmp -73
|
||||
nop -135
|
||||
acc +43
|
||||
jmp -236
|
||||
acc -14
|
||||
acc -3
|
||||
jmp -406
|
||||
acc +2
|
||||
acc -3
|
||||
acc +47
|
||||
jmp -420
|
||||
acc -8
|
||||
acc +18
|
||||
jmp -604
|
||||
jmp -218
|
||||
acc +37
|
||||
acc -16
|
||||
nop -278
|
||||
acc -15
|
||||
jmp -214
|
||||
acc -6
|
||||
acc +18
|
||||
acc +7
|
||||
acc +0
|
||||
jmp -252
|
||||
acc +14
|
||||
jmp -266
|
||||
acc +27
|
||||
acc -16
|
||||
nop -533
|
||||
nop -534
|
||||
jmp +1
|
28
2020/day08/Makefile
Normal file
28
2020/day08/Makefile
Normal file
@@ -0,0 +1,28 @@
|
||||
INPUT := INPUT.txt
|
||||
SHELL := /bin/bash
|
||||
CFLAGS := -w
|
||||
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
|
||||
export PATH := .:$(PATH)
|
||||
|
||||
.PHONY: clean all compile deploy ex1 ex2
|
||||
|
||||
all: ex1 ex2
|
||||
|
||||
output:
|
||||
@$(MAKE) --no-print-directory all 2>&1 > OUTPUT
|
||||
|
||||
compile: ex1-c ex2-c
|
||||
|
||||
ex1: ex1-c
|
||||
@$(TIME) ex1.bash < $(INPUT) 2>&1
|
||||
@$(TIME) ex1-c shinygold < $(INPUT) 2>&1
|
||||
|
||||
ex2: ex2-c
|
||||
@$(TIME) ex2.bash < $(INPUT) 2>&1
|
||||
@$(TIME) ex2-c shinygold < $(INPUT) 2>&1
|
||||
|
||||
clean:
|
||||
@rm -f ex1-c ex2-c core
|
||||
|
||||
deploy:
|
||||
@$(MAKE) -C .. deploy
|
16
2020/day08/OUTPUT
Normal file
16
2020/day08/OUTPUT
Normal file
@@ -0,0 +1,16 @@
|
||||
ex1.bash : res=1594
|
||||
time: 0:00.01 real, 0.01 user, 0.00 sys
|
||||
context-switch: 0+4, page-faults: 0+303
|
||||
|
||||
ex1-c : res=1594
|
||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||
context-switch: 1+0, page-faults: 0+79
|
||||
|
||||
ex2.bash : res:758
|
||||
time: 0:02.26 real, 2.12 user, 0.16 sys
|
||||
context-switch: 212+1084, page-faults: 0+43039
|
||||
|
||||
ex2-c : res=758
|
||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||
context-switch: 1+1, page-faults: 0+78
|
||||
|
94
2020/day08/README
Normal file
94
2020/day08/README
Normal file
@@ -0,0 +1,94 @@
|
||||
--- 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?
|
||||
|
||||
Your puzzle answer was 1594.
|
||||
--- Part Two ---
|
||||
|
||||
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?
|
||||
|
||||
Your puzzle answer was 758.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your Advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
||||
|
||||
You can also [Share] this puzzle.
|
9
2020/day08/TEST.txt
Normal file
9
2020/day08/TEST.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
nop +0
|
||||
acc +1
|
||||
jmp +4
|
||||
acc +3
|
||||
jmp -3
|
||||
acc -99
|
||||
acc +1
|
||||
jmp -4
|
||||
acc +6
|
143
2020/day08/ex1-c.c
Normal file
143
2020/day08/ex1-c.c
Normal file
@@ -0,0 +1,143 @@
|
||||
/* ex1-c: Advent2020 game, day 8/game 1
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct instr {
|
||||
struct instr *next;
|
||||
int instr;
|
||||
int val;
|
||||
int visited;
|
||||
};
|
||||
|
||||
struct program {
|
||||
unsigned length;
|
||||
unsigned total;
|
||||
struct instr *prg;
|
||||
};
|
||||
|
||||
#define BLOCKSIZE 1024 /* number of elements for realloc() */
|
||||
#define NOP 0
|
||||
#define ACC 1
|
||||
#define JMP 2
|
||||
|
||||
void print_instr(prg, step, cur)
|
||||
struct program *prg;
|
||||
int step;
|
||||
int cur;
|
||||
{
|
||||
struct instr *instr=prg->prg+step;
|
||||
char *op="unknown";
|
||||
|
||||
switch (instr->instr) {
|
||||
case NOP:
|
||||
op="nop";
|
||||
break;
|
||||
case JMP:
|
||||
op="jmp";
|
||||
break;
|
||||
case ACC:
|
||||
op="acc";
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "%03d: [%s][%4d] -> %4d\n", step, op, instr->val, cur);
|
||||
}
|
||||
|
||||
void print_prg(prg)
|
||||
struct program *prg;
|
||||
{
|
||||
unsigned i, psize=prg->length;
|
||||
struct instr *pinstr=prg->prg;
|
||||
|
||||
fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", prg, pinstr, psize);
|
||||
for (i=0; i<psize; ++i)
|
||||
print_instr(prg, i, 0);
|
||||
}
|
||||
|
||||
static struct program *add_line(prg, line)
|
||||
struct program *prg;
|
||||
char *line;
|
||||
{
|
||||
char sinstr[80];
|
||||
int val;
|
||||
unsigned cur, total;
|
||||
struct instr *pinstr;
|
||||
|
||||
if (!prg) {
|
||||
prg=malloc(sizeof (struct program));
|
||||
prg->prg=malloc(sizeof(struct instr)*BLOCKSIZE);
|
||||
|
||||
//fprintf(stderr, "alloc buf: prg=%p prog=%p\n", prg, prg->prg);
|
||||
prg->length=0;
|
||||
prg->total=BLOCKSIZE;
|
||||
}
|
||||
cur=prg->length;
|
||||
total=prg->total;
|
||||
|
||||
if (cur == total) {
|
||||
total+=BLOCKSIZE;
|
||||
prg->total=total;
|
||||
prg->prg=realloc(prg->prg, sizeof(struct instr)*total);
|
||||
//fprintf(stderr, "realloc buf: cur=%d total=%d prog=%p\n", cur, total, prg->prg);
|
||||
}
|
||||
pinstr=prg->prg+cur;
|
||||
|
||||
sscanf(line, "%s %d", sinstr, &val);
|
||||
pinstr->visited=0;
|
||||
pinstr->val=val;
|
||||
if (!strcmp(sinstr, "jmp"))
|
||||
pinstr->instr=JMP;
|
||||
else if (!strcmp(sinstr, "acc"))
|
||||
pinstr->instr=ACC;
|
||||
else pinstr->instr=NOP;
|
||||
prg->length++;
|
||||
|
||||
return prg;
|
||||
}
|
||||
|
||||
int run(prg)
|
||||
struct program *prg;
|
||||
{
|
||||
int cur=0, length=prg->length;
|
||||
struct instr *instr=prg->prg;
|
||||
int res=0;
|
||||
|
||||
//fprintf(stderr, "RUN: address=%p prg=%p size=%d\n", prg, instr, length);
|
||||
while (cur>=0 && cur<length) {
|
||||
instr=prg->prg+cur;
|
||||
//print_instr(prg, cur, res);
|
||||
if (instr->visited) {
|
||||
break;
|
||||
}
|
||||
instr->visited=1;
|
||||
switch (instr->instr) {
|
||||
case NOP:
|
||||
cur++;
|
||||
break;
|
||||
case JMP:
|
||||
cur+=instr->val;
|
||||
break;
|
||||
case ACC:
|
||||
res+=instr->val;
|
||||
cur++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(ac, av)
|
||||
char **av;
|
||||
{
|
||||
char line[512];
|
||||
struct program *prg=NULL;
|
||||
|
||||
while (fgets(line, sizeof line, stdin)) {
|
||||
prg=add_line(prg, line);
|
||||
}
|
||||
//print_prg(prg);
|
||||
printf("%s : res=%d\n", *av, run(prg));
|
||||
exit(0);
|
||||
}
|
58
2020/day08/ex1.bash
Executable file
58
2020/day08/ex1.bash
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex1.bash: Advent2020 game, day 8/game 1.
|
||||
|
||||
CMD=${0##*/}
|
||||
shopt -s extglob
|
||||
|
||||
#declare -i ACCUMULATOR=0
|
||||
|
||||
declare -a PRG=()
|
||||
declare -i ARG=()
|
||||
declare -i PRGSIZE=0
|
||||
|
||||
function run() {
|
||||
local -i arg cur=0 res=0
|
||||
local -a steps=()
|
||||
local instr
|
||||
|
||||
while [[ ${steps[$cur]} != 1 ]]; do
|
||||
instr=${PRG[$cur]}
|
||||
arg=${ARG[$cur]}
|
||||
steps[$cur]=1
|
||||
case "$instr" in
|
||||
acc)
|
||||
((res += arg))
|
||||
((cur++))
|
||||
;;
|
||||
nop)
|
||||
((cur++))
|
||||
;;
|
||||
jmp)
|
||||
((cur += arg))
|
||||
;;
|
||||
esac
|
||||
done
|
||||
echo "$res"
|
||||
}
|
||||
|
||||
function print_prog()
|
||||
{
|
||||
local -i i
|
||||
for ((i=0; i<PRGSIZE; ++i)); do
|
||||
printf "%03d : [%s][%3d]\n" "$i" "${PRG[$i]}" "${ARG[$i]}"
|
||||
done
|
||||
}
|
||||
|
||||
while read -r instr arg; do
|
||||
PRG[$PRGSIZE]=$instr
|
||||
ARG[$PRGSIZE]=$arg
|
||||
((PRGSIZE++))
|
||||
done
|
||||
|
||||
#print_prog
|
||||
|
||||
res=$(run)
|
||||
printf "%s : res=%d\n" "${CMD}" "$res"
|
||||
|
||||
exit 0
|
184
2020/day08/ex2-c.c
Normal file
184
2020/day08/ex2-c.c
Normal file
@@ -0,0 +1,184 @@
|
||||
/* ex2-c: Advent2020 game, day 8/game 2
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct instr {
|
||||
struct instr *next;
|
||||
int instr;
|
||||
int val;
|
||||
int visited;
|
||||
};
|
||||
|
||||
struct program {
|
||||
unsigned length;
|
||||
unsigned total;
|
||||
struct instr *prg;
|
||||
};
|
||||
|
||||
#define BLOCKSIZE 1024 /* number of elements for realloc() */
|
||||
#define NOP 0
|
||||
#define ACC 1
|
||||
#define JMP 2
|
||||
|
||||
void print_instr(prg, step, cur)
|
||||
struct program *prg;
|
||||
int step;
|
||||
int cur;
|
||||
{
|
||||
struct instr *instr=prg->prg+step;
|
||||
char *op="unknown";
|
||||
|
||||
switch (instr->instr) {
|
||||
case NOP:
|
||||
op="nop";
|
||||
break;
|
||||
case JMP:
|
||||
op="jmp";
|
||||
break;
|
||||
case ACC:
|
||||
op="acc";
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "%03d: [%s][%4d] -> %4d\n", step, op, instr->val, cur);
|
||||
}
|
||||
|
||||
void print_prg(prg)
|
||||
struct program *prg;
|
||||
{
|
||||
unsigned i, psize=prg->length;
|
||||
struct instr *pinstr=prg->prg;
|
||||
|
||||
fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", prg, pinstr, psize);
|
||||
for (i=0; i<psize; ++i)
|
||||
print_instr(prg, i, 0);
|
||||
}
|
||||
|
||||
void reset_prg(prg)
|
||||
struct program *prg;
|
||||
{
|
||||
unsigned i, psize=prg->length;
|
||||
struct instr *pinstr=prg->prg;
|
||||
|
||||
for (i=0; i<psize; ++i)
|
||||
pinstr[i].visited=0;
|
||||
}
|
||||
|
||||
static struct program *add_line(prg, line)
|
||||
struct program *prg;
|
||||
char *line;
|
||||
{
|
||||
char sinstr[80];
|
||||
int val;
|
||||
unsigned cur, total;
|
||||
struct instr *pinstr;
|
||||
|
||||
if (!prg) {
|
||||
prg=malloc(sizeof (struct program));
|
||||
prg->prg=malloc(sizeof(struct instr)*BLOCKSIZE);
|
||||
|
||||
//fprintf(stderr, "alloc buf: prg=%p prog=%p\n", prg, prg->prg);
|
||||
prg->length=0;
|
||||
prg->total=BLOCKSIZE;
|
||||
}
|
||||
cur=prg->length;
|
||||
total=prg->total;
|
||||
|
||||
if (cur == total) {
|
||||
total+=BLOCKSIZE;
|
||||
prg->total=total;
|
||||
prg->prg=realloc(prg->prg, sizeof(struct instr)*total);
|
||||
//fprintf(stderr, "realloc buf: cur=%d total=%d prog=%p\n", cur, total, prg->prg);
|
||||
}
|
||||
pinstr=prg->prg+cur;
|
||||
|
||||
sscanf(line, "%s %d", sinstr, &val);
|
||||
pinstr->visited=0;
|
||||
pinstr->val=val;
|
||||
if (!strcmp(sinstr, "jmp"))
|
||||
pinstr->instr=JMP;
|
||||
else if (!strcmp(sinstr, "acc"))
|
||||
pinstr->instr=ACC;
|
||||
else pinstr->instr=NOP;
|
||||
prg->length++;
|
||||
|
||||
return prg;
|
||||
}
|
||||
|
||||
int run2(prg, ret)
|
||||
struct program *prg;
|
||||
int *ret;
|
||||
{
|
||||
int cur=0, length=prg->length;
|
||||
struct instr *instr=prg->prg;
|
||||
int res=0;
|
||||
|
||||
reset_prg(prg);
|
||||
//fprintf(stderr, "RUN: address=%p prg=%p size=%d\n", prg, instr, length);
|
||||
//print_prg(prg);
|
||||
while (cur>=0 && cur<length) {
|
||||
instr=prg->prg+cur;
|
||||
//print_instr(prg, cur, res);
|
||||
if (instr->visited) {
|
||||
return 1;
|
||||
}
|
||||
instr->visited=1;
|
||||
switch (instr->instr) {
|
||||
case NOP:
|
||||
cur++;
|
||||
break;
|
||||
case JMP:
|
||||
cur+=instr->val;
|
||||
break;
|
||||
case ACC:
|
||||
res+=instr->val;
|
||||
cur++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
*ret=res;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(ac, av)
|
||||
char **av;
|
||||
{
|
||||
char line[512];
|
||||
struct program *prg=NULL;
|
||||
struct instr *instr;
|
||||
int res=0, cur=0, length=0;
|
||||
|
||||
while (fgets(line, sizeof line, stdin)) {
|
||||
prg=add_line(prg, line);
|
||||
}
|
||||
instr=prg->prg;
|
||||
length=prg->length;
|
||||
if (run2(prg, &res)) {
|
||||
do {
|
||||
while (cur<length && instr[cur].instr == ACC)
|
||||
cur++;
|
||||
if (cur < length) {
|
||||
int isav=instr[cur].instr;
|
||||
switch (isav) {
|
||||
case NOP:
|
||||
instr[cur].instr=JMP;
|
||||
break;
|
||||
case JMP:
|
||||
instr[cur].instr=NOP;
|
||||
break;
|
||||
}
|
||||
if (!run2(prg, &res))
|
||||
break;
|
||||
instr[cur].instr=isav;
|
||||
|
||||
} else
|
||||
break;
|
||||
cur++;
|
||||
} while (cur>=0 && cur<length);
|
||||
}
|
||||
printf("%s : res=%d\n", *av, res);
|
||||
exit(0);
|
||||
|
||||
}
|
78
2020/day08/ex2.bash
Executable file
78
2020/day08/ex2.bash
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# ex1.bash: Advent2020 game, day 8/game 2.
|
||||
|
||||
CMD=${0##*/}
|
||||
shopt -s extglob
|
||||
|
||||
declare -a PRG=()
|
||||
declare -i ARG=()
|
||||
|
||||
function run() {
|
||||
local -i arg cur=0 res=0 size=$1
|
||||
local instr
|
||||
local -a steps=()
|
||||
|
||||
while ((cur < size)) && [[ ${steps[$cur]} != 1 ]]; do
|
||||
instr=${PRG[$cur]}
|
||||
arg=${ARG[$cur]}
|
||||
steps[$cur]=1
|
||||
case "$instr" in
|
||||
acc)
|
||||
((res += arg))
|
||||
((cur++))
|
||||
;;
|
||||
nop)
|
||||
((cur++))
|
||||
;;
|
||||
jmp)
|
||||
((cur += arg))
|
||||
;;
|
||||
esac
|
||||
done
|
||||
echo $res
|
||||
return $((cur < size))
|
||||
}
|
||||
|
||||
function print_prog()
|
||||
{
|
||||
local -i i
|
||||
for ((i=0; i<${#PRG[@]}; ++i)); do
|
||||
printf "%03d : [%s][%3d]\n" "$i" "${PRG[$i]}" "${ARG[$i]}"
|
||||
done
|
||||
}
|
||||
|
||||
declare -i i=0
|
||||
while read -r instr arg; do
|
||||
PRG[$i]=$instr
|
||||
ARG[$i]=$arg
|
||||
((i++))
|
||||
done
|
||||
|
||||
lastchanged=-1
|
||||
savstep=""
|
||||
while true; do
|
||||
res=$(run $i) && break
|
||||
|
||||
if ((lastchanged >= 0)); then
|
||||
PRG[$lastchanged]="$savstep"
|
||||
fi
|
||||
((lastchanged++))
|
||||
while ((lastchanged <= PRGSIZE)) && [[ ${PRG[$lastchanged]} == acc ]]; do
|
||||
((lastchanged++))
|
||||
done
|
||||
|
||||
savstep=${PRG[$lastchanged]}
|
||||
case $savstep in
|
||||
jmp)
|
||||
PRG[$lastchanged]="nop"
|
||||
;;
|
||||
nop)
|
||||
PRG[$lastchanged]="jmp"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
printf "%s : res:%d\n" "${CMD}" "$res"
|
||||
|
||||
exit 0
|
Reference in New Issue
Block a user