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!

88 Upvotes

1.3k comments sorted by

View all comments

2

u/RodDylan Dec 03 '20

For some reason, my Python code is buggy somehow. Part 1 took less than five minutes, and after a very quick refactor part 2 should have been easy.

def day3a(right, down):
lines = data3.split('\n')
width = len(lines[0])
hits = 0
for row in range(0, len(lines), down):
    pos = (right*row) % width
if lines[row][pos] == '#':
    hits += 1
return hits

print(day3a(3, 1))

def day3():
a = (day3a(1, 1))
b = (day3a(3, 1))
c = (day3a(5, 1))
d = (day3a(7, 1))
e = (day3a(1, 2)) # should be two higher then I get
print(a, b, c, d, e)
print(a * b * c * d * (e+2)) # hack

day3()

Am I missing something really obvious?

2

u/lukewarm1997 Dec 03 '20

The co-ordinates you need for (1,2) would be:

row = 0, pos = 0

row = 2, pos = 1

row = 4, pos = 2

You correctly increase row by 2 every time (down = 2), however when calculating pos you do (right*row). 0, 0 works but on the next loop you get:

row = 2

pos = (1*2) = 2

So it's going to 2,2 instead.

Hope this helps :)

Potential Solution: Divide by down, so: (right*row/down)

1

u/LukeHanson1991 Dec 03 '20

Make sure to use integer division in this case so: (right*row//down)

pos needs to be an integer

1

u/RodDylan Dec 03 '20

Ugh, so obvious now! Haha!

Thank you ever so much!

1

u/lukewarm1997 Dec 03 '20

Don’t worry, I couldn’t see it for ages too. Had to step through it 1 line at a time to notice it

1

u/daggerdragon Dec 03 '20

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your working code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

1

u/toshok Dec 03 '20

right*row

doesn't work when down > 1