This commit is contained in:
Peter Hudec 2018-01-25 16:47:09 +01:00
parent 5998d17b6a
commit 7b877c8243
Signed by: peter.hudec
GPG Key ID: 427BD558E277E410
5 changed files with 192 additions and 0 deletions

23
12/input.puzzle Normal file
View File

@ -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

6
12/input.sample Normal file
View File

@ -0,0 +1,6 @@
cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a

80
12/part01.py Executable file
View File

@ -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)

80
12/part02.py Executable file
View File

@ -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)

View File

@ -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