Skip to content

Instantly share code, notes, and snippets.

@xpqz
Last active December 3, 2020 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpqz/2a4ecacbd3801ec02f35a59d6454ed06 to your computer and use it in GitHub Desktop.
Save xpqz/2a4ecacbd3801ec02f35a59d6454ed06 to your computer and use it in GitHub Desktop.
Advent of Code 2020, Day03 in Python
"""
Day 3: Toboggan Trajectory
https://adventofcode.com/2020/day/3
Stefan Kruger
"""
from math import ceil, prod
def path(d, dy, dx):
return ((dy * i, (dx * i) % len(d[0])) for i in range(ceil(len(d) / dy)))
def trees(d, dy, dx):
return sum(d[y][x] == '#' for (y, x) in path(d, dy, dx))
with open('data/test.txt') as f:
data = list(f.read().splitlines())
result = [trees(data, *v) for v in [[1, 1], [1, 3], [1, 5], [1, 7], [2, 1]]]
print(f"Part1: {result[1]}\nPart2: {prod(result)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment