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!

90 Upvotes

1.3k comments sorted by

View all comments

1

u/Arjunnn Dec 03 '20

I'll post only p2. p1 is a generalised p2 anyway. This one's just figuring out that scripting languages have string handling methods than cpp et al and babbys introduction to the modulo operator

from math import prod
with open('3.in') as f:
    grid = [i.strip() for i in f.readlines()]
    H = len(grid) 
    W = len(grid[0])


    slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
    trees = [0 for i in range(len(slopes))]
    

    for i in range(len(slopes)):
        dx = slopes[i][0]
        dy = slopes[i][1]
        x, y = 0, 0
        ans = 0
        while True:
            x = (x + dx) % W
            y = y + dy
            if y >= H:
                trees[i] = ans
                break
            if grid[y][x] == '#': ans += 1
    print(prod(trees))

1

u/daggerdragon Dec 03 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!