-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday17.py
executable file
·115 lines (92 loc) · 3.33 KB
/
day17.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
#!/usr/bin/env python3
from utils.all import *
fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
from lib.intcode import IntcodeVM
prog = read_ints(fin)
vm = IntcodeVM(prog)
def vm_write(v):
if 0 <= v <= 0x7f:
sys.stderr.write(chr(v))
else:
print('==================== NON ASCII:', v)
# Dump initial grid
# vm.write = vm_write
# vm.run()
grid = [
'........#######..........................',
'........#.....#..........................',
'........#.....#..........................',
'........#.....#..........................',
'......###########........................',
'......#.#.....#.#........................',
'......#.#.....#######....................',
'......#.#.......#...#....................',
'......#.#.....###########................',
'......#.#.....#.#...#...#................',
'#######.#########...#...#.....#..........',
'#.............#.....#...#.....#..........', # 'R,6,L,6,L,10,L,8,L,6',
'#.............#.....#...#.....#..........', # 'L,10,L,6,R,6,L,6',
'#.............#.....#...#.....#..........', # 'L,10,L,8,L,6,L,10',
'#.............#...^######.....#..........', # 'L,6,R,6,L,8,L,10',
'#.............#.....#.........#..........', # 'R,6,R,6,L,6,L,10',
'#.............#######.........###########', # 'L,8,L,6,L,10,L,6',
'#.......................................#', # 'R,6,L,8,L,10,R,6',
'###########.............................#', # 'R,6,L,6,L,10,R,6',
'..........#.............................#', # 'L,8,L,10,R,6',
'..........#.............................#',
'..........#.............................#',
'........#######.........................#',
'........#.#...#.........................#',
'....#######...#...................#######',
'....#...#.....#...................#......',
'....#...#.....#...................#......',
'....#...#.....#...................#......',
'....#...#.....#.......#######.....#......',
'....#...#.....#.......#.....#.....#......',
'....###########.......#.....#.....#......',
'........#.............#.....#.....#......',
'........#######.......#.....#.....#......',
'..............#.......#.....#.....#......',
'..............#.......#.....#######......',
'..............#.......#..................',
'..............#.......#..................',
'..............#.......#..................',
'..............#########..................',
]
intersections = set()
for y, l in enumerate(grid):
for x, c in enumerate(l):
if c == '#':
ok = True
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
xx, yy = x + dx, y + dy
if 0 <= yy < len(grid) and 0 <= xx < len(l) and grid[yy][xx] !='#':
ok = False
break
if ok:
intersections.add((x, y))
# print(intersections)
ans = sum(x*y for x,y in intersections)
advent.submit_answer(1, ans)
def vm_write2(v):
if v > 0x7f:
# print('----', v, '----')
advent.submit_answer(2, v)
vm.reset()
vm.code[0] = 2
vm.write = vm_write2
main = 'R,6,L,6,L,10,L,8,L,6,L,10,L,6,R,6,L,6,L,10,L,8,L,6,L,10,L,6,R,6,L,8,L,10,R,6,R,6,L,6,L,10,L,8,L,6,L,10,L,6,R,6,L,8,L,10,R,6,R,6,L,6,L,10,R,6,L,8,L,10,R,6'
# Compress above program by hand...
main = 'A,B,A,B,C,A,B,C,A,C'
funcs = [
'R,6,L,6,L,10',
'L,8,L,6,L,10,L,6',
'R,6,L,8,L,10,R,6',
]
subs = '\n'.join(funcs)
debug = 'n'
robot_prog = list(map(ord, '{}\n{}\n{}\n'.format(main, subs, debug)))
vm.run(robot_prog)