DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 3: Toboggan Trajectory

Collapse
pzfrenchy profile image
Dan French

Python effort, short and simple and gives the right answer.

treeCounter = 0
right = 5
down = 1
x = 0
y = 0

with open('day3.txt', 'r') as file:
    lines = file.read().splitlines()
    while y < len(lines):
        line = lines[y]
        for n in range(7):
            line = line + line
        if line[x] == "#":
            treeCounter += 1
        x = x + int(right)
        y = y + int(down)
    print(treeCounter)
Enter fullscreen mode Exit fullscreen mode
Collapse
rabbiyos profile image
Rabbi Yosef Baruch Fromer

Hi,
Can you explain why "line = line + line" 7 times?
(Maybe i missed something in the question...)
10x :)

Collapse
pzfrenchy profile image
Dan French

Hi Rabbi, you have a short string that repeats indefinitely. As you move right you quickly hit the end of the string. It was a very simple way to repeat the pattern otherwise you get an index out of range error when you exceed the original string length.
Its not very efficient as it repeats every line but it works in this instance. If you had to create longer strings you might want to refactor to check if you've hit end of string and only repeat then.
Dan

Thread Thread
rabbiyos profile image
Rabbi Yosef Baruch Fromer

Great! That helped me understand my mistake (Thous that end of line leads to the next line) :)