This commit is contained in:
a 2020-12-07 23:42:33 -06:00
parent 0a02b1199c
commit fbdf7129ba
3 changed files with 735 additions and 0 deletions

70
08/08.py Normal file
View file

@ -0,0 +1,70 @@
def read_file():
with open("input.txt","r") as f:
return f.read().split('\n')
def process(instructions):
acc = 0
i = 0
visited = []
while True:
if i not in range(len(instructions)):
# we've terminated bois
return acc, False
if i in visited:
# it loops
return acc, True
visited.append(i)
instruction = instructions[i]
operation = instruction[:3]
argument = instruction[4:]
if operation == "nop":
i += 1
elif operation == "acc":
acc += int(argument)
i += 1
elif operation == "jmp":
i += int(argument)
def part1(instructions):
acc, repeats = process(instructions)
return acc
def terminates(instructions):
if process(instructions):
return False
return True
def part2(instructions):
acc = 0
i = 0
visited = []
while True:
if i in visited:
print(visited)
for i in reversed(visited):
new_instructions = [instruction for instruction in instructions]
if instructions[i][:3] == "jmp":
new_instructions[i] = instructions[i].replace("jmp","nop")
elif instructions[i][:3] == "nop":
new_instructions[i] = instructions[i].replace("nop","jmp")
new_acc, repeats = process(new_instructions)
if not repeats:
return new_acc
visited.append(i)
instruction = instructions[i]
operation = instruction[:3]
argument = instruction[4:]
if operation == "nop":
i += 1
elif operation == "acc":
acc += int(argument)
i += 1
elif operation == "jmp":
i += int(argument)
def main():
instructions = read_file()
print(part1(instructions), part2(instructions))
if __name__ == "__main__":
main()

656
08/input.txt Normal file
View 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

9
08/input_sample.txt Normal file
View file

@ -0,0 +1,9 @@
nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6