-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.py
executable file
·196 lines (166 loc) · 7.47 KB
/
19.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
from Intcode_Computer import Intcode_Computer
puzzle_input = "109,424,203,1,21102,11,1,0,1106,0,282,21102,18,1,0,1106,0,259,2102,1,1,221,203,1,21102,1,31,0,1106,0,282,21101,0,38,0,1106,0,259,21001,23,0,2,22101,0,1,3,21101,1,0,1,21102,57,1,0,1106,0,303,2101,0,1,222,21002,221,1,3,20102,1,221,2,21101,259,0,1,21101,0,80,0,1105,1,225,21102,1,83,2,21101,0,91,0,1105,1,303,1202,1,1,223,20102,1,222,4,21101,259,0,3,21101,225,0,2,21102,1,225,1,21101,118,0,0,1106,0,225,21002,222,1,3,21101,179,0,2,21102,1,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21101,0,148,0,1105,1,259,1202,1,1,223,21001,221,0,4,20101,0,222,3,21102,1,19,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,1,195,0,105,1,109,20207,1,223,2,21002,23,1,1,21102,-1,1,3,21102,214,1,0,1106,0,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,21201,-3,0,1,21202,-2,1,2,22101,0,-1,3,21101,0,250,0,1106,0,225,22101,0,1,-4,109,-5,2106,0,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21201,-2,0,3,21101,343,0,0,1105,1,303,1106,0,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21201,-4,0,1,21101,384,0,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21202,1,1,-4,109,-5,2105,1,0"
puzzle_input = puzzle_input.split(",")
class Tractor(Intcode_Computer):
def __init__(self, instructions):
Intcode_Computer.__init__(self, instructions)
self.input_buffer = []
self.out = 0
def read_input(self):
if len(self.input_buffer) == 0:
return int(input("INPUT: "))
return self.input_buffer.pop(0)
def process_output(self, output):
self.out = output
print("#" if output == 1 else ".", end="")
# Part 1
cpu = Tractor(puzzle_input)
w = 50
h = 50
beam_area = 0
initial_map = {}
for y in range(h):
for x in range(w):
cpu.out = 0
cpu.input_buffer = [x,y]
cpu.run()
initial_map[(x,y)] = cpu.out
beam_area += cpu.out
print()
# 131
print("Tractor beam area:", beam_area)
print("Part 2 incomplete; uses approximation WITHOUT validation of real values, ToDo...")
quit()
# 0 10 20 30 40
# 01234567890123456789012345678901234565789012345678
# 0 1 #.................................................
# 1 0 ..................................................
# 2 1 ...#..............................................
# 3 0 ..................................................
# 4 1 ......#...........................................
# 5 1 .......#..........................................
# 6 1 .........#........................................
# 7 2 ..........##......................................
# 8 2 ...........##.....................................
# 9 2 .............##...................................
# 10 2 ..............##..................................
# 11 3 ...............###................................
# 12 3 .................###..............................
# 13 3 ..................###.............................
# 14 3 ....................###...........................
# 15 3 .....................###..........................
# 16 4 ......................####........................
# 17 3 ........................###.......................
# 18 4.........................####.....................
# 19 5 ..........................#####...................
# 20 4 ............................####..................
# 21 5 .............................#####................
# 22 5 ..............................#####...............
# 23 5 ................................#####.............
# 24 6 .................................######...........
# 25 6 ..................................######..........
# 26 6 ....................................######........
# 27 6 .....................................######.......
# 28 6 .......................................######.....
# 29 7 ........................................#######...
# 30 7 .........................................#######..
# 31 ? ...........................................#######
# 32 ? ............................................######
# 33 ? .............................................#####
# 34 ? ...............................................###
# 35 ? ................................................##
# 36 ? .................................................#
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
# ? ..................................................
nums_offsets = """0 1 0
1 0 0
2 1 3
3 0 0
4 1 6
5 1 7
6 1 9
7 2 10
8 2 12
9 2 13
10 2 14
11 3 15
12 3 17
13 3 18
14 3 20
15 3 21
16 4 22
17 3 24
18 4 25
19 5 26
20 4 28
21 5 30
22 5 31
23 5 33
24 6 34
25 6 35
26 6 37
27 6 38
28 6 40
29 7 41
30 7 42""".split("\n")
sum_n = 0
sum_x = 0
length = 0
for num_offset in nums_offsets:
y, n, x = list(map(int, num_offset.split(" "*6)))
fac_n = n/y if y > 0 else 0
fac_x = x/y if y > 0 else 0
if fac_n > 0:
sum_n += fac_n
sum_x += fac_x
length += 1
#print(
# f"%2d" % y, n, f"%2d" % x, "\t",
# f"%1.3f" % fac_n, "\t",
# f"%1.3f" % fac_x
#)
approx_fac_n = sum_n/length
approx_fac_x = sum_x/length
print("Approximation factors: n:", approx_fac_n, "x:", approx_fac_x)
for num_offset in nums_offsets:
y, n, x = list(map(int, num_offset.split(" "*6)))
fac_n = n/y if y > 0 else 0
fac_x = x/y if y > 0 else 0
if fac_n > 0:
sum_n += fac_n
sum_x += fac_x
length += 1
print(
"y =", f"%2d" % y,
"\tn =", n, f"%2.2f" % (y * approx_fac_n),
"\tx =", f"%2d" % x, f"%2.2f" % (y * approx_fac_x)
)
# Rundungsfehler zu berücksichtigen
y1 = round(100/approx_fac_n)
x1 = round(y1 * approx_fac_x)
n1 = round(y1 * approx_fac_n)
o1 = x1 + n1 - 100 # Box offset
y2 = y1 + 100
x2 = round(y2 * approx_fac_x)
n2 = round(y2 * approx_fac_n)
while o1 <= x2:
y1 += 1
x1 = round(y1 * approx_fac_x)
n1 = round(y1 * approx_fac_n)
o1 = x1 + n1 - 100 # Box offset
y2 = y1 + 100
x2 = round(y2 * approx_fac_x)
n2 = round(y2 * approx_fac_n)
print("Trying", x1, y1)