diff options
| author | blenovo <bk@gmail.com> | 2025-07-16 18:55:48 +0200 |
|---|---|---|
| committer | blenovo <bk@gmail.com> | 2025-07-16 18:55:48 +0200 |
| commit | 99a7d62c30069a5ffe2210a72c7cf81e76a1f241 (patch) | |
| tree | 71c5c529b7f2fda9b5b5897a56a4ef3199400709 /2020/aoc2020-d08.py | |
| parent | 15662865f0886209d871a7225bfc62cffd2e0783 (diff) | |
summertime warmup session with 2020 event
Diffstat (limited to '2020/aoc2020-d08.py')
| -rw-r--r-- | 2020/aoc2020-d08.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/2020/aoc2020-d08.py b/2020/aoc2020-d08.py new file mode 100644 index 0000000..8965514 --- /dev/null +++ b/2020/aoc2020-d08.py @@ -0,0 +1,93 @@ +#advent of code 2020 +#day 08 +#a little bit bruteforce-ish, +#for part 2 I mark instruction that are actually used by boot code +#then I loop over the marked instructions and replace them 1 by 1 until +#the infinite cycle is broken +#also the input kept getting broken and needed to change the parsing +#which isn't a hard bug, but an annoying one +#ALSO during rewriting I figured out that I don't need to calculate +#part 1 separately, it's already used during cycle detection +#although this might just be a coincidence; honestly I'm not confident +#in cycle detection puzzles + +BootCode = [] +PuzzleInput = open("08.in","r") +for x in PuzzleInput: + if len(x) == 0: continue; #please ignore it, I'm bad at parsing + instr, val = x.split(" "); + BootCode.append(x.replace("\n","")); +PuzzleInput.close(); + +index = 0; +accumulator = 0; +visited = set(); +#the "Im sure it works" method for part 1 +while True: + instr, val = BootCode[index].split(" "); + #order += 1; + if index in visited: break; + visited.add(index); + if instr == "nop": + index += 1; + elif instr == "acc": + accumulator += int(val); + index += 1; + elif instr == "jmp": + index += int(val); + +print("part 1 =",accumulator); + +index = 0; +accumulator = 0; +accVals = dict(); +p1 = None; +#the "it works for me but might be luck" method for part 1 +while index <= len(BootCode): + instr, val = BootCode[index].split(" "); + Current, Previous = accVals.get(index,(None,None)); + if Previous and Current and Previous%accumulator == Previous%accumulator: + p1 = Current; + break; + accVals[index] = (accumulator, Current); + if instr == "nop": + index += 1; + elif instr == "acc": + accumulator += int(val); + index += 1; + elif instr == "jmp": + index += int(val); + +print("part 1 =",p1); + +p2 = None; +for i in accVals: + if BootCode[i].split(" ")[0] == "acc": continue; + index = 0; + accumulator = 0; + accValsNested = dict(); + NoCycle = True; + while index < len(BootCode): + instr, val = BootCode[index].split(" "); + Current, Previous = accValsNested.get(index,(None,None)); + if Previous and Current and Previous%accumulator == Previous%accumulator: + NoCycle = False; + break; + accValsNested[index] = (accumulator, Current); + if index == i: #swap instructions + if instr == "nop": + instr = "jmp"; + elif instr == "jmp": + instr = "nop"; + if instr == "nop": + index += 1; + elif instr == "acc": + accumulator += int(val); + index += 1; + elif instr == "jmp": + index += int(val); + if NoCycle: + p2 = accumulator; + break; + +print("part 2 =", p2); |
