Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2020-12-03 - Toboggan Trajectory

(original .ipynb)

Day 3 puzzle input is a grid of "#" and "." characters representing a toboggan run dotted with trees, where "#" is a tree (mine is here). Part 1 involves traversing the toboggan run by advancing one row and three columns at a time, and counting how many trees you encounter (hit?). The pattern of trees repeats to the right forever. Part 2 involves traversing the toboggan run by a number of different row/column increments, and multiplying together the number of trees you encounter for each.

test_input_str = """..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#"""

puzzle_input_str = open("puzzle_input/day3.txt").read()

def part_one(input_str):
    return sum(
        1 for i, line 
        in enumerate(input_str.strip().split("\n"))
        if line[(3*i)%len(line)] == "#"
    )

assert 7 == part_one(test_input_str)
print("part one:", part_one(puzzle_input_str))
part one: 280
from functools import reduce

def part_two(input_str):
    test_input = [ c for c in input_str.strip().split("\n") ]
    
    slopes = (
        # (down, right)
        (1, 1),
        (1, 3),
        (1, 5),
        (1, 7),
        (2, 1),
    )
    
    tree_counts = [ check_slope(test_input, down, right) for (down, right) in slopes ]
    return reduce(lambda a, b: a * b, tree_counts)

assert 336 == part_two(test_input_str)
print("part two:", part_two(puzzle_input_str))
part two: 4355551200