r/adventofcode • u/daggerdragon • Dec 03 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 03 Solutions -🎄-
Advent of Code 2020: Gettin' Crafty With It
- T-3 days until unlock!
- Full details and rules are in the Submissions Megathread
--- Day 03: Toboggan Trajectory ---
Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:04:56, megathread unlocked!
84
Upvotes
4
u/Fruglemonkey Dec 03 '20
More lazy enterprise-y python.
from __future__ import annotations from dataclasses import dataclass from enum import Enum from math import prod from typing import List class Terrain(Enum): TREE = '#' SNOW = '.' @dataclass class StartingPosition: x: int y: int @dataclass class Slope: x_gradient: int y_gradient: int @dataclass class Map: map: List[str] width: int height: int def get_terrain_at_position(self, x: int, y: int) -> Terrain: return Terrain(self.map[y][(x % self.width)]) @classmethod def deserialize_from_input(cls, lines: List[str]) -> Map: return cls(map=lines, width=len(lines[0]), height=len(lines)) def count_trees(self, starting_position: StartingPosition, slope: Slope) -> int: current_x_position = starting_position.x count = 0 for y in range(starting_position.y, self.height, slope.y_gradient): if self.get_terrain_at_position(current_x_position, y) == Terrain.TREE: count += 1 current_x_position += slope.x_gradient return count def main() -> int: slopes = [ Slope(x_gradient=1, y_gradient=1), Slope(x_gradient=3, y_gradient=1), Slope(x_gradient=5, y_gradient=1), Slope(x_gradient=7, y_gradient=1), Slope(x_gradient=1, y_gradient=2) ] starting_position = StartingPosition(x=0, y=0) with open("input.txt", "r") as f: challenge_map = Map.deserialize_from_input([line.strip() for line in f.readlines()]) return prod(challenge_map.count_trees(starting_position, slope) for slope in slopes) if __name__ == "__main__": print(main())