Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 20, 2025. It is now read-only.

Commit 7def1da

Browse files
Day 12 Python solutions
1 parent 01f79de commit 7def1da

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

‎12-1.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
3+
import heapq
4+
import numpy
5+
6+
class Map:
7+
def __init__(self, input):
8+
self.height = len(input)
9+
self.width = len(input[0])
10+
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint)
11+
12+
for row in range(self.height):
13+
for col in range(self.width):
14+
if input[row][col] == "S":
15+
self.start = [row, col]
16+
self.map[row, col] = 0
17+
elif input[row][col] == "E":
18+
self.end = [row, col]
19+
self.map[row, col] = ord('z') - ord('a')
20+
else:
21+
self.map[row, col] = ord(input[row][col]) - ord('a')
22+
23+
self.lengths = numpy.empty((self.height, self.width), dtype=numpy.uint)
24+
self.lengths.fill(pow(2, 32) - 1)
25+
self.lengths[self.start[0], self.start[1]] = 0
26+
27+
def solve(self):
28+
queue = []
29+
heapq.heappush(queue, (0, (self.start[0], self.start[1])))
30+
31+
while len(queue) > 0:
32+
length, coords = heapq.heappop(queue)
33+
row, col = coords
34+
if length > self.lengths[row, col]:
35+
continue
36+
37+
ns = [
38+
[row + 1, col],
39+
[row - 1, col],
40+
[row, col + 1],
41+
[row, col - 1]
42+
]
43+
44+
neighbours = []
45+
for n in ns:
46+
if n[0] >= 0 and \
47+
n[0] < self.height and \
48+
n[1] >= 0 and \
49+
n[1] < self.width and \
50+
self.map[n[0], n[1]] <= self.map[row, col] + 1:
51+
neighbours.append((n[0], n[1]))
52+
53+
for v in neighbours:
54+
new_length = length + 1
55+
if new_length < self.lengths[v[0], v[1]]:
56+
self.lengths[v[0], v[1]] = new_length
57+
heapq.heappush(queue, (new_length, (v[0], v[1])))
58+
59+
return self.lengths[self.end[0], self.end[1]]
60+
61+
62+
lines = [list(line.strip()) for line in open('12.input').readlines()]
63+
64+
map = Map(lines)
65+
66+
print(map.solve())

‎12-2.py‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
3+
import heapq
4+
import numpy
5+
6+
class Map:
7+
def __init__(self, input):
8+
self.height = len(input)
9+
self.width = len(input[0])
10+
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint)
11+
12+
for row in range(self.height):
13+
for col in range(self.width):
14+
if input[row][col] == "S":
15+
self.map[row, col] = 0
16+
elif input[row][col] == "E":
17+
self.start = [row, col]
18+
self.map[row, col] = ord('z') - ord('a')
19+
else:
20+
self.map[row, col] = ord(input[row][col]) - ord('a')
21+
22+
self.lengths = numpy.empty((self.height, self.width), dtype=numpy.uint)
23+
self.lengths.fill(pow(2, 32) - 1)
24+
self.lengths[self.start[0], self.start[1]] = 0
25+
26+
def solve(self):
27+
queue = []
28+
heapq.heappush(queue, (0, (self.start[0], self.start[1])))
29+
30+
while len(queue) > 0:
31+
length, coords = heapq.heappop(queue)
32+
row, col = coords
33+
if length > self.lengths[row, col]:
34+
continue
35+
36+
ns = [
37+
[row + 1, col],
38+
[row - 1, col],
39+
[row, col + 1],
40+
[row, col - 1]
41+
]
42+
43+
neighbours = []
44+
for n in ns:
45+
if n[0] >= 0 and \
46+
n[0] < self.height and \
47+
n[1] >= 0 and \
48+
n[1] < self.width and \
49+
self.map[row, col] <= self.map[n[0], n[1]] + 1:
50+
neighbours.append((n[0], n[1]))
51+
52+
for v in neighbours:
53+
new_length = length + 1
54+
if new_length < self.lengths[v[0], v[1]]:
55+
self.lengths[v[0], v[1]] = new_length
56+
heapq.heappush(queue, (new_length, (v[0], v[1])))
57+
58+
def solution(self):
59+
closest = pow(2, 32) - 1
60+
61+
it = numpy.nditer(self.map, flags=['multi_index'])
62+
for element in it:
63+
if element > 0:
64+
continue
65+
66+
if self.lengths[it.multi_index[0], it.multi_index[1]] < closest:
67+
closest = self.lengths[it.multi_index[0], it.multi_index[1]]
68+
69+
return closest
70+
71+
72+
lines = [list(line.strip()) for line in open('12.input').readlines()]
73+
74+
map = Map(lines)
75+
76+
map.solve()
77+
78+
print(map.solution())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /