-
Notifications
You must be signed in to change notification settings - Fork 1
/
day22.py
165 lines (143 loc) · 4.45 KB
/
day22.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
import os.path
import re
import math
from collections import defaultdict
import itertools
import md5
import msvcrt
import sys
def get_input(filename=None):
if not filename:
filename = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
with open(filename) as fp:
input = fp.read().rstrip()
lines = input.split('\n')
depth = int(re.search(r'^depth: (\d+)$', lines[0]).group(1))
target = map(int, re.search(r'^target: (\d+),(\d+)$', lines[1]).groups())
return depth, target
INDEX_CACHE = {}
def geologic_index(x, y, depth, target):
if (x, y) not in INDEX_CACHE:
if x == 0 and y == 0:
index = 0
elif x == target[0] and y == target[1]:
index = 0
elif y == 0:
index = x * 16807
elif x == 0:
index = y * 48271
else:
index = erosion_level(x - 1, y, depth, target) * erosion_level(x, y - 1, depth, target)
INDEX_CACHE[(x, y)] = index
return INDEX_CACHE[(x, y)]
EROSION_CACHE = {}
def erosion_level(x, y, depth, target):
if (x, y) not in EROSION_CACHE:
EROSION_CACHE[(x, y)] = (geologic_index(x, y, depth, target) + depth) % 20183
return EROSION_CACHE[(x, y)]
def terrain_type(x, y, depth, target):
return erosion_level(x, y, depth, target) % 3
ROCKY = 0
WET = 1
NARROW = 2
TORCH = frozenset([ROCKY, NARROW])
CLIMBING_GEAR = frozenset([ROCKY, WET])
NEITHER = frozenset([WET, NARROW])
TOOLS = {
ROCKY: set([TORCH, CLIMBING_GEAR]),
WET: set([CLIMBING_GEAR, NEITHER]),
NARROW: set([TORCH, NEITHER]),
}
def switch_tool(terrain, tool):
return list(TOOLS[terrain] - set([tool]))[0]
def astar_all(start, goal_func, neighbors_func, heuristic_func):
explored = set()
frontier = [start]
g = {start: 0}
h = {start: heuristic_func(start)}
f = {start: h[start]}
while frontier:
current = frontier.pop(0)
explored.add(current)
if goal_func(current):
yield current, g[current]
continue
for neighbor, distance in neighbors_func(current):
if neighbor in explored:
continue
distance += g[current]
if neighbor not in frontier:
frontier.append(neighbor)
g[neighbor] = distance
h[neighbor] = heuristic_func(neighbor)
f[neighbor] = distance + h[neighbor]
elif distance < g[neighbor]:
g[neighbor] = distance
f[neighbor] = distance + h[neighbor]
frontier.sort(key=lambda node: (f[node],h[node]))
def astar(start, goal_func, neighbors_func, heuristic_func):
for result in astar_all(start, goal_func, neighbors_func, heuristic_func):
return result
return None
def bfs(start, goal_func, neighbors_func):
explored = set()
frontier = [start]
g = {start: 0}
while frontier:
current = frontier.pop(0)
explored.add(current)
if goal_func(current):
return current, g[current]
for neighbor, distance in neighbors_func(current):
if neighbor in explored:
continue
distance += g[current]
if neighbor not in frontier:
frontier.append(neighbor)
g[neighbor] = distance
elif distance < g[neighbor]:
g[neighbor] = distance
frontier.sort(key=lambda node: g[node])
def neighbors(depth, target):
def wrapper(state):
x, y, tool = state
dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
for dx, dy in dirs:
if x + dx < 0 or y + dy < 0:
continue
terrain = terrain_type(x + dx, y + dy, depth, target)
if terrain not in tool:
continue
yield (x + dx, y + dy, tool), 1
terrain = terrain_type(x, y, depth, target)
yield (x, y, switch_tool(terrain, tool)), 7
return wrapper
def heuristic(target):
def wrapper(state):
x, y, tool = state
return abs(target[0] - x) + abs(target[1] - y) + (7 if tool != TORCH else 0)
return wrapper
def goal(target):
def wrapper(state):
x, y, tool = state
return x == target[0] and y == target[1] and tool == TORCH
return wrapper
def part1(input):
depth, target = input
risk = 0
for y in xrange(target[1] + 1):
for x in xrange(target[0] + 1):
risk += terrain_type(x, y, depth, target)
return risk
def part2(input):
depth, target = input
start = (0, 0, TORCH)
return astar(start, goal(target), neighbors(depth, target), heuristic(target))[1]
## return bfs(start, goal(target), neighbors(depth, target))[1]
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(usage='%prog [options] [<input.txt>]')
options, args = parser.parse_args()
input = get_input(*args)
print part1(input)
print part2(input)