From 7b877c824393b425068919bfe81cca1d146a032b Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Thu, 25 Jan 2018 16:47:09 +0100 Subject: [PATCH] day 12 --- 12/input.puzzle | 23 ++++++++++++++ 12/input.sample | 6 ++++ 12/part01.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 12/part02.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ 5 files changed, 192 insertions(+) create mode 100644 12/input.puzzle create mode 100644 12/input.sample create mode 100755 12/part01.py create mode 100755 12/part02.py diff --git a/12/input.puzzle b/12/input.puzzle new file mode 100644 index 0000000..3729d83 --- /dev/null +++ b/12/input.puzzle @@ -0,0 +1,23 @@ +cpy 1 a +cpy 1 b +cpy 26 d +jnz c 2 +jnz 1 5 +cpy 7 c +inc d +dec c +jnz c -2 +cpy a c +inc a +dec b +jnz b -2 +cpy c b +dec d +jnz d -6 +cpy 19 c +cpy 11 d +inc a +dec d +jnz d -2 +dec c +jnz c -5 diff --git a/12/input.sample b/12/input.sample new file mode 100644 index 0000000..01a004b --- /dev/null +++ b/12/input.sample @@ -0,0 +1,6 @@ +cpy 41 a +inc a +inc a +dec a +jnz a 2 +dec a diff --git a/12/part01.py b/12/part01.py new file mode 100755 index 0000000..1a0381f --- /dev/null +++ b/12/part01.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +import re +import argparse + +RE_CPY = re.compile(r'^cpy (.*) (.)') +RE_INC = re.compile(r'^inc (.)') +RE_DEC = re.compile(r'^dec (.)') +RE_JNZ = re.compile(r'^jnz (.*) (-?\d+)') + +INSTRUCTIONS = [] +REGISTER = {'a': 0, 'b': 0, 'c': 0, 'd': 0 } + +def instruction_cpy(k, v): + global REGISTER + if v in REGISTER: + v = REGISTER[v] + REGISTER[k] = int(v) + return 1 + +def instruction_inc(k): + global REGISTER + REGISTER[k] += 1 + return 1 + +def instruction_dec(k): + global REGISTER + REGISTER[k] -= 1 + return 1 + +def instruction_jnz(k, v): + global REGISTER + if k in REGISTER: + if REGISTER[k]: + return v + elif(int(k)): + return v + return 1 + + +def load_file(filename): + with open(filename) as f: + for line in f: + yield line.strip() + +def main(args): + for LINE in load_file(args.input): + match = RE_CPY.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(2), match.group(1)], 'function': instruction_cpy}) + continue + match = RE_INC.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(1)], 'function': instruction_inc}) + continue + match = RE_DEC.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(1)], 'function': instruction_dec}) + continue + match = RE_JNZ.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(1), int(match.group(2))], 'function': instruction_jnz}) + continue + raise Exception("line not parsable: {}". format(LINE)) + + line = 0 + while True: + if line >= len(INSTRUCTIONS): + break + code = INSTRUCTIONS[line] + line += code['function'](*code['values']) + + print(REGISTER['a']) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01') + parser.add_argument('--input', '-i', action='store', required=True, help='input file') + args = parser.parse_args() + + main(args) diff --git a/12/part02.py b/12/part02.py new file mode 100755 index 0000000..97bf42a --- /dev/null +++ b/12/part02.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +import re +import argparse + +RE_CPY = re.compile(r'^cpy (.*) (.)') +RE_INC = re.compile(r'^inc (.)') +RE_DEC = re.compile(r'^dec (.)') +RE_JNZ = re.compile(r'^jnz (.*) (-?\d+)') + +INSTRUCTIONS = [] +REGISTER = {'a': 0, 'b': 0, 'c': 1, 'd': 0 } + +def instruction_cpy(k, v): + global REGISTER + if v in REGISTER: + v = REGISTER[v] + REGISTER[k] = int(v) + return 1 + +def instruction_inc(k): + global REGISTER + REGISTER[k] += 1 + return 1 + +def instruction_dec(k): + global REGISTER + REGISTER[k] -= 1 + return 1 + +def instruction_jnz(k, v): + global REGISTER + if k in REGISTER: + if REGISTER[k]: + return v + elif(int(k)): + return v + return 1 + + +def load_file(filename): + with open(filename) as f: + for line in f: + yield line.strip() + +def main(args): + for LINE in load_file(args.input): + match = RE_CPY.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(2), match.group(1)], 'function': instruction_cpy}) + continue + match = RE_INC.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(1)], 'function': instruction_inc}) + continue + match = RE_DEC.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(1)], 'function': instruction_dec}) + continue + match = RE_JNZ.match(LINE) + if match: + INSTRUCTIONS.append({'values': [match.group(1), int(match.group(2))], 'function': instruction_jnz}) + continue + raise Exception("line not parsable: {}". format(LINE)) + + line = 0 + while True: + if line >= len(INSTRUCTIONS): + break + code = INSTRUCTIONS[line] + line += code['function'](*code['values']) + + print(REGISTER['a']) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01') + parser.add_argument('--input', '-i', action='store', required=True, help='input file') + args = parser.parse_args() + + main(args) diff --git a/README.md b/README.md index 546ee1c..fcfb541 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,6 @@ - **Day 8**: Two-Factor Authentication - **Day 9**: Explosives in Cyberspace - **Day 10**: Balance Bots +- **Day 11**: Radioisotope Thermoelectric Generators (TODO) +- **Day 12**: Leonardo's Monorail +