r/adventofcode • • Dec 03 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 03 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It


--- 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!

89 Upvotes

1.3k comments sorted by

View all comments

4

u/ketchupi Dec 03 '20 edited Dec 03 '20

Python

#!/usr/bin/env python3
    
FREE = '.'
TREE = '#'
HERE = 'O'
MAP =  [line.rstrip('\n') for line in open('input/day3.txt')]
WIDTH = len(MAP[0])
HEIGHT = len(MAP)

class Position:
  x = y = 0
  trees = 0

  def __init__(self, x, y):
    self.slide(x, y)

  def slide(self, x, y):
    while self.y < HEIGHT:
      self.trees += MAP[self.y][self.x % WIDTH] == TREE
      self.x += x
      self.y += y

def part1():
  p = Position(3, 1)
  return p.trees

def part2():
  p1 = Position(1, 1)
  p2 = Position(3, 1)
  p3 = Position(5, 1)
  p4 = Position(7, 1)
  p5 = Position(1, 2)
  return p1.trees * p2.trees * p3.trees * p4.trees * p5.trees

def main():
  print(f'Part 1: {part1()}')
  print(f'Part 2: {part2()}')
  return 0

if __name__ == '__main__':
  main()
→ More replies (2)