mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Solutions to day 23.
This commit is contained in:
49
day-23/input.txt
Normal file
49
day-23/input.txt
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
jio a, +19
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
jmp +23
|
||||||
|
tpl a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
inc a
|
||||||
|
tpl a
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
jio a, +8
|
||||||
|
inc b
|
||||||
|
jie a, +4
|
||||||
|
tpl a
|
||||||
|
inc a
|
||||||
|
jmp +2
|
||||||
|
hlf a
|
||||||
|
jmp -7
|
||||||
49
day-23/solution.py
Normal file
49
day-23/solution.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
from __future__ import division, print_function
|
||||||
|
import fileinput
|
||||||
|
import re
|
||||||
|
|
||||||
|
def run(instructions, registers):
|
||||||
|
instrptr = 0
|
||||||
|
while instrptr < len(instructions):
|
||||||
|
instruction = instructions[instrptr]
|
||||||
|
if "j" in instruction[0]:
|
||||||
|
if instruction[0] == "jie":
|
||||||
|
doJump = registers[instruction[1]] % 2 == 0
|
||||||
|
elif instruction[0] == "jio":
|
||||||
|
doJump = registers[instruction[1]] == 1
|
||||||
|
else:
|
||||||
|
doJump = True
|
||||||
|
|
||||||
|
if doJump:
|
||||||
|
instrptr = instrptr + int(instruction[-1])
|
||||||
|
else:
|
||||||
|
instrptr += 1
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
|
if instruction[0] == "hlf":
|
||||||
|
registers[instruction[1]] //= 2
|
||||||
|
elif instruction[0] == "tpl":
|
||||||
|
registers[instruction[1]] *= 3
|
||||||
|
else:
|
||||||
|
registers[instruction[1]] += 1
|
||||||
|
|
||||||
|
instrptr += 1
|
||||||
|
|
||||||
|
return registers
|
||||||
|
|
||||||
|
registers = {
|
||||||
|
'a': 0,
|
||||||
|
'b': 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
instructions = []
|
||||||
|
|
||||||
|
for line in fileinput.input():
|
||||||
|
instructions.append(tuple(x.strip() for x in re.match(r"(hlf|tpl|inc|jmp|jie|jio) (a|b)?,? ?(\+?-?[0-9]+)?", line).groups() if x is not None))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print(run(instructions, {'a': 0, 'b': 0})['b'])
|
||||||
|
print(run(instructions, {'a': 1, 'b': 0})['b'])
|
||||||
|
|
||||||
Reference in New Issue
Block a user