-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.py
executable file
·102 lines (76 loc) · 7.18 KB
/
17.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
from Intcode_Computer import Intcode_Computer
puzzle_input = "1,330,331,332,109,2608,1102,1182,1,16,1101,1399,0,24,102,1,0,570,1006,570,36,102,1,571,0,1001,570,-1,570,1001,24,1,24,1105,1,18,1008,571,0,571,1001,16,1,16,1008,16,1399,570,1006,570,14,21102,1,58,0,1105,1,786,1006,332,62,99,21102,333,1,1,21101,0,73,0,1106,0,579,1101,0,0,572,1101,0,0,573,3,574,101,1,573,573,1007,574,65,570,1005,570,151,107,67,574,570,1005,570,151,1001,574,-64,574,1002,574,-1,574,1001,572,1,572,1007,572,11,570,1006,570,165,101,1182,572,127,102,1,574,0,3,574,101,1,573,573,1008,574,10,570,1005,570,189,1008,574,44,570,1006,570,158,1106,0,81,21102,340,1,1,1105,1,177,21101,0,477,1,1106,0,177,21102,514,1,1,21101,0,176,0,1106,0,579,99,21101,184,0,0,1106,0,579,4,574,104,10,99,1007,573,22,570,1006,570,165,1001,572,0,1182,21101,375,0,1,21101,211,0,0,1106,0,579,21101,1182,11,1,21102,222,1,0,1105,1,979,21102,388,1,1,21101,0,233,0,1105,1,579,21101,1182,22,1,21101,0,244,0,1105,1,979,21101,401,0,1,21101,0,255,0,1105,1,579,21101,1182,33,1,21101,266,0,0,1105,1,979,21101,0,414,1,21101,0,277,0,1105,1,579,3,575,1008,575,89,570,1008,575,121,575,1,575,570,575,3,574,1008,574,10,570,1006,570,291,104,10,21101,1182,0,1,21102,313,1,0,1105,1,622,1005,575,327,1101,1,0,575,21102,327,1,0,1106,0,786,4,438,99,0,1,1,6,77,97,105,110,58,10,33,10,69,120,112,101,99,116,101,100,32,102,117,110,99,116,105,111,110,32,110,97,109,101,32,98,117,116,32,103,111,116,58,32,0,12,70,117,110,99,116,105,111,110,32,65,58,10,12,70,117,110,99,116,105,111,110,32,66,58,10,12,70,117,110,99,116,105,111,110,32,67,58,10,23,67,111,110,116,105,110,117,111,117,115,32,118,105,100,101,111,32,102,101,101,100,63,10,0,37,10,69,120,112,101,99,116,101,100,32,82,44,32,76,44,32,111,114,32,100,105,115,116,97,110,99,101,32,98,117,116,32,103,111,116,58,32,36,10,69,120,112,101,99,116,101,100,32,99,111,109,109,97,32,111,114,32,110,101,119,108,105,110,101,32,98,117,116,32,103,111,116,58,32,43,10,68,101,102,105,110,105,116,105,111,110,115,32,109,97,121,32,98,101,32,97,116,32,109,111,115,116,32,50,48,32,99,104,97,114,97,99,116,101,114,115,33,10,94,62,118,60,0,1,0,-1,-1,0,1,0,0,0,0,0,0,1,4,0,0,109,4,2101,0,-3,587,20101,0,0,-1,22101,1,-3,-3,21101,0,0,-2,2208,-2,-1,570,1005,570,617,2201,-3,-2,609,4,0,21201,-2,1,-2,1105,1,597,109,-4,2106,0,0,109,5,2101,0,-4,629,21002,0,1,-2,22101,1,-4,-4,21102,0,1,-3,2208,-3,-2,570,1005,570,781,2201,-4,-3,653,20101,0,0,-1,1208,-1,-4,570,1005,570,709,1208,-1,-5,570,1005,570,734,1207,-1,0,570,1005,570,759,1206,-1,774,1001,578,562,684,1,0,576,576,1001,578,566,692,1,0,577,577,21101,702,0,0,1105,1,786,21201,-1,-1,-1,1106,0,676,1001,578,1,578,1008,578,4,570,1006,570,724,1001,578,-4,578,21102,1,731,0,1105,1,786,1105,1,774,1001,578,-1,578,1008,578,-1,570,1006,570,749,1001,578,4,578,21101,756,0,0,1106,0,786,1105,1,774,21202,-1,-11,1,22101,1182,1,1,21101,0,774,0,1105,1,622,21201,-3,1,-3,1106,0,640,109,-5,2105,1,0,109,7,1005,575,802,21002,576,1,-6,21002,577,1,-5,1106,0,814,21101,0,0,-1,21102,1,0,-5,21101,0,0,-6,20208,-6,576,-2,208,-5,577,570,22002,570,-2,-2,21202,-5,39,-3,22201,-6,-3,-3,22101,1399,-3,-3,1202,-3,1,843,1005,0,863,21202,-2,42,-4,22101,46,-4,-4,1206,-2,924,21102,1,1,-1,1105,1,924,1205,-2,873,21102,35,1,-4,1105,1,924,1202,-3,1,878,1008,0,1,570,1006,570,916,1001,374,1,374,2101,0,-3,895,1101,2,0,0,2102,1,-3,902,1001,438,0,438,2202,-6,-5,570,1,570,374,570,1,570,438,438,1001,578,558,922,20101,0,0,-4,1006,575,959,204,-4,22101,1,-6,-6,1208,-6,39,570,1006,570,814,104,10,22101,1,-5,-5,1208,-5,31,570,1006,570,810,104,10,1206,-1,974,99,1206,-1,974,1101,0,1,575,21102,973,1,0,1105,1,786,99,109,-7,2105,1,0,109,6,21102,0,1,-4,21101,0,0,-3,203,-2,22101,1,-3,-3,21208,-2,82,-1,1205,-1,1030,21208,-2,76,-1,1205,-1,1037,21207,-2,48,-1,1205,-1,1124,22107,57,-2,-1,1205,-1,1124,21201,-2,-48,-2,1105,1,1041,21102,1,-4,-2,1105,1,1041,21102,-5,1,-2,21201,-4,1,-4,21207,-4,11,-1,1206,-1,1138,2201,-5,-4,1059,2101,0,-2,0,203,-2,22101,1,-3,-3,21207,-2,48,-1,1205,-1,1107,22107,57,-2,-1,1205,-1,1107,21201,-2,-48,-2,2201,-5,-4,1090,20102,10,0,-1,22201,-2,-1,-2,2201,-5,-4,1103,2101,0,-2,0,1106,0,1060,21208,-2,10,-1,1205,-1,1162,21208,-2,44,-1,1206,-1,1131,1106,0,989,21102,1,439,1,1105,1,1150,21102,1,477,1,1106,0,1150,21101,514,0,1,21102,1149,1,0,1105,1,579,99,21101,0,1157,0,1105,1,579,204,-2,104,10,99,21207,-3,22,-1,1206,-1,1138,2101,0,-5,1176,2101,0,-4,0,109,-6,2106,0,0,0,5,34,1,38,1,38,1,38,11,3,11,24,1,3,1,34,1,3,1,34,1,3,1,30,5,3,1,30,1,7,1,30,1,7,1,1,11,3,9,6,1,7,1,1,1,9,1,3,1,7,1,6,5,3,5,7,1,3,1,7,1,10,1,5,1,1,1,7,1,3,1,7,1,4,5,1,1,5,1,1,1,7,5,7,1,4,1,3,1,1,1,5,1,1,1,19,1,4,1,3,1,1,1,3,5,19,1,4,1,3,1,1,1,3,1,1,1,21,1,4,1,3,11,19,1,4,1,5,1,3,1,1,1,1,1,19,12,1,5,1,1,15,6,3,1,7,1,1,1,3,1,15,1,4,1,3,1,7,1,1,1,3,1,15,1,4,1,3,1,7,1,1,1,3,1,15,1,4,5,7,11,11,1,18,1,3,1,3,1,11,1,18,11,9,1,22,1,3,1,1,1,9,1,22,5,1,1,9,1,28,1,9,1,28,11,4"
puzzle_input = puzzle_input.split(",")
class ASCII(Intcode_Computer):
def __init__(self, instructions):
Intcode_Computer.__init__(self, instructions)
self.do_read_map = True
self.map = ""
self.map_parsed = {}
self.W = 0
self.H = 0
# Part 2
def read_input(self):
if len(self.input_list) == 0:
raise Exception("Input not found")
i = self.input_list[0]
self.input_list = self.input_list[1:]
return i
def process_output(self, output):
if self.do_read_map:
self.map += chr(output)
print(chr(output), end="")
else:
print(output)
def parse_map(self):
map = self.map.split("\n")
for y, line in enumerate(map):
line = list(line)
for x, char in enumerate(line):
self.map_parsed[(x,y)] = char
self.W = max(self.W, x)
self.H = max(self.H, y)
def find_intersections(self):
if len(self.map_parsed) == 0:
raise Exception("Must parse map first.")
intersections = []
for (position, char) in self.map_parsed.items():
if char != "#":
continue
x,y = position
# Beginning or End
if x == 0 or y == 0 or x == self.W or y == self.H:
continue
if self.map_parsed[(x-1,y)] == "#" and self.map_parsed[(x+1,y)] == "#" and self.map_parsed[(x,y-1)] == "#" and self.map_parsed[(x,y+1)] == "#":
intersections.append(position)
return intersections
# Part 1
cpu = ASCII(puzzle_input)
cpu.run()
cpu.parse_map()
# Intersections
intersections = cpu.find_intersections()
print("Found intersections:", intersections)
sum = 0
for x,y in intersections:
sum += x*y
print("Sum:", sum)
# Part 2
print("\n\nPart 2")
#path = "L4 L4 L10 R4 R4 L4 L4 R8 R10 L4 L4 L10 R4 R4 L10 R10 L4 L4 L10 R4 R4 L10 R10 R4 L4 L4 R8 R10 R4 L10 R10 R4 L10 R10 R4 L4 L4 R8 R10".split(" ")
inputs = [
"A,B,A,C,A,C,B,C,C,B\n",
"L,4,L,4,L,10,R,4\n",
"R,4,L,4,L,4,R,8,R,10\n",
"R,4,L,10,R,10\n"
]
real_inputs = []
for val in inputs:
real_inputs += [str(y) for y in [ord(x) for x in list(val)]]
control = puzzle_input[:]
control[0] = 2
cpu = ASCII(control)
cpu.input_list = real_inputs + [ord("n"), ord("\n")]
cpu.do_read_map = False
cpu.run()