Python Tips & Tricks for the Advent of Code 2019

Day 1: The Tyranny of the Rocket Equation

import fileinputif __name__ == '__main__':
lines = [*fileinput.input()]
process(lines)
echo 'input' | ./day1.py
cat input.txt | ./day1.py
./day1.py input.txt

Day 2: 1202 Program Alarm

Day 3: Crossed Wires

grid = {
(0, 0): '.',
(0, 1): '#',
(1, 0): '#',
(1, 1): '.',
}
grid = {
(x, y): v
for y, row in enumerate(fileinput.input())
for x, v in enumerate(row.strip())
}
def minmax(it):
vals = list(it)
return min(vals), max(vals)
minx, maxx = minmax(x for x, y in grid.keys())
miny, maxy = minmax(y for x, y in grid.keys())
explored_nodes = {...}
moves = [
(-1, 0),
(+1, 0),
(0, -1),
(0, +1),
]
new_nodes = {
x + dx, y + dy
for x, y in current_nodes
for dx, dy in moves
if grid.get((x + dx, y + dy)) == '.'
and (x + dx, y + dy) not in explored_nodes
}
new_nodes = {
node
for x, y in current_nodes
for dx, dy in moves
if grid.get(node := (x + dx, y + dy)) == '.'
and node not in explored_nodes
}

Day 7: Amplification Circuit

Day 10: Monitoring Station (Asteroid lines of sight)

Using math.atan2 and defaultdict(list) to index the list of asteroids by angle

Day 12: The N-Body Problem

Day 13: Care Package (Breakout game)

def address_for_block(x, y):
return 1664 + ((((((25 * x + y) * 521) + 1011) % (64 * 1025)) % (8 * 1025)) % 1025)

Day 14: Space Stoichiometry

Day 15: Oxygen System

Day 16: Flawed Frequency Transmission

I decided to hang out on these hammocks while my slow solution to Day 16 ran.

Day 18: Many-Worlds Interpretation (Maze & Keys)

Day 20: Donut Maze

Day 21: Springdroid Adventure

Day 22: Slam Shuffle

> pow(2, 3, 7)
1 # 2 ** 3 % 7 == 1
> pow(2, -1, 7)
4
> 2 * 4 % 7
1

Day 25: Cryostasis (Christmas!)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store