I Heard You Like Registers — Python — #adventofcode Day 8
Series
This post is part of the series Advent of Code 2017
- Reflections on #aoc2017
- The Halting Problem — Python — #adventofcode Day 25
- Electromagnetic Moat — Rust — #adventofcode Day 24
- Coprocessor Conflagration — Haskell — #adventofcode Day 23
- Sporifica Virus — Rust — #adventofcode Day 22
- Fractal Art — Python — #adventofcode Day 21
- Particle Swarm — Python — #adventofcode Day 20
- A Series of Tubes — Rust — #adventofcode Day 19
- Duet — Haskell — #adventofcode Day 18
- Spinlock — Rust/Python — #adventofcode Day 17
- Permutation Promenade — Julia — #adventofcode Day 16
- Dueling Generators — Rust — #adventofcode Day 15
- Disk Defragmentation — Haskell — #adventofcode Day 14
- Packet Scanners — Haskell — #adventofcode Day 13
- Digital Plumber — Python — #adventofcode Day 12
- Hex Ed — Python — #adventofcode Day 11
- Knot Hash — Haskell — #adventofcode Day 10
- Stream Processing — Haskell — #adventofcode Day 9
- > I Heard You Like Registers — Python — #adventofcode Day 8 <
- Recursive Circus — Ruby — #adventofcode Day 7
- Memory Reallocation — Python — #adventofcode Day 6
- A Maze of Twisty Trampolines — C++ — #adventofcode Day 5
- High Entropy Passphrases — Python — #adventofcode Day 4
- Spiral Memory — Go — #adventofcode Day 3
- Corruption Checksum — Python — #adventofcode Day 2
- Inverse Captcha — Coconut — #adventofcode Day 1
- Advent of Code 2017: introduction
Today’s challenge describes a simple instruction set for a CPU, incrementing and decrementing values in registers according to simple conditions. We have to interpret a stream of these instructions, and to prove that we’ve done so, give the highest value of any register, both at the end of the program and throughout the whole program.
!!! commentary
This turned out to be a nice straightforward one to implement, as the instruction format was easily parsed by regular expression, and Python provides the eval
function which made evaluating the conditions a doddle.
Import various standard library bits that we’ll use later.
import re
import fileinput as fi
from math import inf
from collections import defaultdict
We could just parse the instructions by splitting the string, but using a regular expression is a little bit more robust because it won’t match at all if given an invalid instruction.
INSTRUCTION_RE = re.compile(r'(\w+) (inc|dec) (-?\d+) if (.+)\s*')
def parse_instruction(instruction):
match = INSTRUCTION_RE.match(instruction)
return match.group(1, 2, 3, 4)
Executing an instruction simply checks the condition and if it evaluates to True
updates the relevant register.
def exec_instruction(registers, instruction):
name, op, value, cond = instruction
value = int(value)
if op == 'dec':
value = -value
if eval(cond, globals(), registers):
registers[name] += value
highest_value
returns the maximum value found in any register.
def highest_value(registers):
return sorted(registers.items(), key=lambda x: x[1], reverse=True)[0][1]
Finally, loop through all the instructions and carry them out, updating global_max
as we go. We need to be able to deal with registers that haven’t been accessed before. Keeping the registers in a dictionary means that we can evaluate the conditions directly using eval
above, passing it as the locals
argument. The standard dict
will raise an exception if we try to access a key that doesn’t exist, so instead we use collections.defaultdict
, which allows us to specify what the default value for a non-existent key will be. New registers start at 0, so we use a simple lambda to define a function that always returns 0.
global_max = -inf
registers = defaultdict(lambda: 0)
for i in map(parse_instruction, fi.input()):
exec_instruction(registers, i)
global_max = max(global_max, highest_value(registers))
print('Max value:', highest_value(registers))
print('All-time max:', global_max)
Webmentions
You can respond to this post, "I Heard You Like Registers — Python — #adventofcode Day 8", by:
liking, boosting or replying to a tweet or toot that mentions it; or
sending a webmention from your own site to https://erambler.co.uk/blog/day-08/
Comments
Powered by Cactus Comments 🌵