-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamato_11.py
67 lines (55 loc) · 2.55 KB
/
gamato_11.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
def run_intcode(input_data, start_panel):
op_counts = [0, 3, 3, 1, 1, 2, 2, 3, 3, 1]
program = [int(x) for x in input_data] + [0] * 10000
i, relative_base = 0, 0
panels = {(0, 0): start_panel}
pos = (0, 0)
outputs = []
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
dir_idx = 0
while program[i] != 99:
modes = [int(x) for x in f"{program[i]:0>5}"[:3]][::-1]
instruction = int(f"{program[i]:0>5}"[3:])
op_count = op_counts[instruction]
base = [relative_base if modes[x] == 2 else 0 for x in range(op_count)]
operands = [program[i+x+1] if modes[x] == 1 else program[base[x] + program[i+x+1]] for x in range(op_count)]
if instruction == 1:
program[base[2] + program[i + 3]] = operands[0] + operands[1]
elif instruction == 2:
program[base[2] + program[i + 3]] = operands[0] * operands[1]
elif instruction == 3:
program[base[0] + program[i + 1]] = panels[pos] if pos in panels else 0
elif instruction == 4:
outputs.append(operands[0])
if len(outputs) == 2:
panels[pos] = outputs[0]
dir_idx = (dir_idx + (1 if outputs[1] else -1)) % len(directions)
pos = (pos[0] + directions[dir_idx][0], pos[1] + directions[dir_idx][1])
outputs = []
elif instruction == 5:
i = (operands[1] - 3) if operands[0] != 0 else i
elif instruction == 6:
i = (operands[1] - 3) if operands[0] == 0 else i
elif instruction == 7:
program[base[2] + program[i + 3]] = int(operands[0] < operands[1])
elif instruction == 8:
program[base[2] + program[i + 3]] = int(operands[0] == operands[1])
elif instruction == 9:
relative_base += operands[0]
i += op_count + 1
return panels
def generate_registration(intcode_result):
"""
Generates the ascii array
:param intcode_result: result of the intcode operation
:return: array of ascii characters
"""
grid = [[" "] * 50 for _ in range(7)]
for panel in zip(intcode_result.keys(), intcode_result.values()):
grid[panel[0][0]][panel[0][1]] = "#" if panel[1] else " "
return grid
if __name__ == "__main__":
data = open("input11.txt", 'r').readline().split(',')
print("Part One:", "Panel Count =", len(run_intcode(data, 0)))
registration = generate_registration(run_intcode(data, 1))
[print(" ".join(panel)) for panel in registration]