blob: 35c2308e4463c728c3339a66dca1b67bed21322b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/usr/bin/python
def get_path(data):
visited = []
loc = (0, 0)
visited.append(loc)
for d in data:
if d == "^":
loc = (loc[0], loc[1] + 1)
elif d == "v":
loc = (loc[0], loc[1] - 1)
elif d == "<":
loc = (loc[0] - 1, loc[1])
elif d == ">":
loc = (loc[0] + 1, loc[1])
visited.append(loc)
return visited
def solve_part1(data):
return len([*set(get_path(data))])
def solve_part2(data):
data1 = data[0::2]
data2 = data[1::2]
return len([*set(get_path(data1) + get_path(data2))])
def main():
# Read delivery instructions from input text file
with open("input.txt", "r") as f:
data = f.read()
print("Part 1 solution:", solve_part1(data)) # 2592 for me
print("Part 2 solution:", solve_part2(data)) # 2360 for me
if __name__ == "__main__":
main()
|