Repository navigation
Expand file tree
/
Copy pathDay11.py
More file actions
78 lines (65 loc) · 2.32 KB
/
Copy pathDay11.py
File metadata and controls
78 lines (65 loc) · 2.32 KB
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
# https://adventofcode.com/2019/day/11
from IntCode import IntComputer
from functools import reduce
from util import filehelper as fh
program = fh.csv_to_list('day11-input.txt')
LOG = open('log.txt','w')
robot = IntComputer(program, wait_after_output=True, wait_for_input=True)
current_pos = (0,0)
panels_painted = set()
facing_directions = {'N': (0,-1), 'S': (0,1), 'E': (1,0), 'W': (-1,0)}
current_heading = 'N'
grid = {}
grid[current_pos] = 1
def moveRobot(pos, dir):
vector = facing_directions[dir]
return (pos[0]+vector[0], pos[1]+vector[1])
def turnRobot(dir, spin):
if dir == 'N':
return 'W' if spin == 0 else 'E'
elif dir == 'S':
return 'E' if spin == 0 else 'W'
elif dir == 'E':
return 'N' if spin == 0 else 'S'
elif dir == 'W':
return 'S' if spin == 0 else 'N'
def getColor():
if current_pos in grid:
return grid[current_pos]
else:
return 0
def concatenate(l):
s = ''
for i in l:
s += i
return s
while True:
current_color = getColor()
#LOG.write(f"Currently at: {current_pos}, Color: {current_color}, Facing: {current_heading}\n")
robot.inputs = [current_color]
next_color = robot.run()
#LOG.write(f"\tNext color: {next_color}\n")
if robot.finished: # After the last input, before halting, the robot pauses. Need to check if it's halted
print("DONE")
break
turn = robot.run()
#LOG.write(f"\tTurn: {turn}\n")
grid[current_pos] = next_color
current_heading = turnRobot(current_heading, turn)
#LOG.write(f"\tPainted here: {grid[current_pos]}, Turned toward: {current_heading}\n")
current_pos = moveRobot(current_pos, current_heading)
cols, rows = zip(*grid.keys())
min_cols = -min(cols) if min(cols) < 0 else 0
min_rows = -min(rows) if min(rows) < 0 else 0
cols = [*map(lambda x: min_cols + x, cols)]
rows = [*map(lambda x: min_rows + x, rows)]
message = [[' ' for i in range(max(cols)+1)] for j in range(max(rows)+1)]
#LOG.write(f"min_rows: {min_rows}, min_cols: {min_cols}\n")
for k,v in grid.items():
#LOG.write(f"{k[0]}, {k[1]}, {v}\n")
message[k[1]+min_rows][k[0]+min_cols] = '#' if v == 1 else ' '
#LOG.write(f"{k[0]}, {k[1]}, {v}, {message[k[1]+min_rows][k[0]+min_cols]}\n")
message = list(map(concatenate, message))
for line in message:
LOG.write(line)
LOG.write('\n')