From ad05caf7a5c182ce7c627d974ae9df7ca6ab57f1 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 24 Dec 2015 14:10:58 +0100 Subject: [PATCH] Solutions to day 23. --- day-23/input.txt | 49 ++++++++++++++++++++++++++++++++++++++++++++++ day-23/solution.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 day-23/input.txt create mode 100644 day-23/solution.py diff --git a/day-23/input.txt b/day-23/input.txt new file mode 100644 index 0000000..07b1c76 --- /dev/null +++ b/day-23/input.txt @@ -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 diff --git a/day-23/solution.py b/day-23/solution.py new file mode 100644 index 0000000..1f6a7bd --- /dev/null +++ b/day-23/solution.py @@ -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']) +