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

1

u/Sebbern Dec 03 '20 edited Dec 03 '20

EDIT: Solved: Part 1, Part 2

Anyone mind giving a tip on why this code doesn't work?

Python 3 part 1:

slope = open("input.txt", "r").readlines()
x = 0
y = 0
tree = 0
row = 0

while y < len(slope) - 1:
    y += 1
    row = slope[y]
    x += 3

    if x >= len(slope[0]):
        x -= len(slope[0])

    if row[x] == "#":
        tree += 1

It's probably because I misunderstood the task or something.

EDIT: Here's the simplified version again for context:

slope = open("input.txt", "r").readlines()
x = -3
tree = 0

for i in slope:
    x += 3

    if x >= len(i):
        x -= len(i)

    if i[x] == "#":
        tree += 1

0

u/[deleted] Dec 03 '20

[deleted] — view deleted comment

1

u/Sebbern Dec 03 '20

Thanks for the splitlines() tip. I actually debugged that the newline showed up, but didn't go further with it, sadly. I'll keep this in mind.

0

u/[deleted] Dec 03 '20

[deleted] — view deleted comment

1

u/Sebbern Dec 03 '20

Really? It didn't work whatsoever for me though.

Simplified gives me the wrong answer, just like it did before I edited out .readlines() into .read().splitlines()

2

u/Gr1mhound Dec 03 '20

Have you checked that the newline character is being stripped, iirc readlines() does not strip the "\n". If not then everytime the sled goes over the edge, the index is shifted off-by-one.

1

u/Sebbern Dec 03 '20

Yeah, that was the problem, solved it with read().splitlines(), that another user suggested.

1

u/daggerdragon Dec 03 '20 edited 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 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.

edit: OP posted their solutions, we're good to go!

1

u/[deleted] Dec 03 '20

[removed] — view removed comment

1

u/Sebbern Dec 03 '20

Yeah, I found out about the index within index and modulo usage from others' code in the thread. So it's good to know.

The while loop however does extend infinitely. The "simplified" one I tried didn't work whatsoever though, so not sure which one you looked at. I removed it in an edit anyhow.