Compare commits
2 Commits
cbd7e79917
...
d16807f80d
| Author | SHA1 | Date | |
|---|---|---|---|
| d16807f80d | |||
| c365cc5b64 |
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from heapq import heappush, heappop
|
||||||
|
|
||||||
|
|
||||||
def part_1(input):
|
def part_1(input):
|
||||||
@@ -11,16 +12,13 @@ def part_1(input):
|
|||||||
start = (0, 0)
|
start = (0, 0)
|
||||||
end = (x_size - 1, y_size - 1)
|
end = (x_size - 1, y_size - 1)
|
||||||
cost = {start: 0}
|
cost = {start: 0}
|
||||||
queue = set([start])
|
queue = [(0, start)]
|
||||||
done = set()
|
done = set()
|
||||||
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
||||||
while len(queue):
|
while len(queue):
|
||||||
cur_cost = None
|
cur_cost, current = heappop(queue)
|
||||||
for n in queue:
|
if current in done:
|
||||||
if cur_cost == None or cost[n] < cur_cost:
|
continue
|
||||||
cur_cost = cost[n]
|
|
||||||
current = n
|
|
||||||
queue.remove(current)
|
|
||||||
(xc, yc) = current
|
(xc, yc) = current
|
||||||
next = set()
|
next = set()
|
||||||
for (xn, yn) in neighbors:
|
for (xn, yn) in neighbors:
|
||||||
@@ -31,7 +29,7 @@ def part_1(input):
|
|||||||
next_cost = cur_cost + nodes[n]
|
next_cost = cur_cost + nodes[n]
|
||||||
if not n in cost or next_cost < cost[n]:
|
if not n in cost or next_cost < cost[n]:
|
||||||
cost[n] = next_cost
|
cost[n] = next_cost
|
||||||
queue.add(n)
|
heappush(queue, (next_cost, n))
|
||||||
done.add(current)
|
done.add(current)
|
||||||
result = cost[end]
|
result = cost[end]
|
||||||
print("Part 1 result:", result)
|
print("Part 1 result:", result)
|
||||||
@@ -47,16 +45,13 @@ def part_2(input):
|
|||||||
start = (0, 0)
|
start = (0, 0)
|
||||||
end = ((x_size * repeat) - 1, (y_size * repeat) - 1)
|
end = ((x_size * repeat) - 1, (y_size * repeat) - 1)
|
||||||
cost = {start: 0}
|
cost = {start: 0}
|
||||||
queue = set([start])
|
queue = [(0, start)]
|
||||||
done = set()
|
done = set()
|
||||||
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
||||||
while len(queue):
|
while len(queue):
|
||||||
cur_cost = None
|
cur_cost, current = heappop(queue)
|
||||||
for n in queue:
|
if current in done:
|
||||||
if cur_cost == None or cost[n] < cur_cost:
|
continue
|
||||||
cur_cost = cost[n]
|
|
||||||
current = n
|
|
||||||
queue.remove(current)
|
|
||||||
(xc, yc) = current
|
(xc, yc) = current
|
||||||
next = set()
|
next = set()
|
||||||
for (xn, yn) in neighbors:
|
for (xn, yn) in neighbors:
|
||||||
@@ -72,7 +67,7 @@ def part_2(input):
|
|||||||
next_cost -= 9
|
next_cost -= 9
|
||||||
if not (x, y) in cost or (cur_cost + next_cost) < cost[(x, y)]:
|
if not (x, y) in cost or (cur_cost + next_cost) < cost[(x, y)]:
|
||||||
cost[(x, y)] = cur_cost + next_cost
|
cost[(x, y)] = cur_cost + next_cost
|
||||||
queue.add((x, y))
|
heappush(queue, (cur_cost + next_cost, (x, y)))
|
||||||
done.add(current)
|
done.add(current)
|
||||||
result = cost[end]
|
result = cost[end]
|
||||||
print("Part 2 result:", result)
|
print("Part 2 result:", result)
|
||||||
|
|||||||
@@ -73,38 +73,23 @@ def calc_result(packets):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def part_1(input):
|
def solve(input):
|
||||||
result = 0
|
result_p1 = 0
|
||||||
line = input[0].rstrip().strip('0')
|
line = input[0].rstrip().strip('0')
|
||||||
msg = bin(int(line, 16))
|
msg = bin(int(line, 16))[2:]
|
||||||
msg = msg[2:]
|
while len(msg) < len(line) * 4:
|
||||||
while len(msg) % 4:
|
|
||||||
msg = '0' + msg
|
msg = '0' + msg
|
||||||
packets = []
|
packets = []
|
||||||
next = 0
|
next = 0
|
||||||
while -1 != next:
|
while -1 != next:
|
||||||
next, result = get_next_packet(msg, next, packets, result)
|
next, result_p1 = get_next_packet(msg, next, packets, result_p1)
|
||||||
print("Part 1 result:", result)
|
result_p2 = calc_result(packets)
|
||||||
|
print("Part 1 result:", result_p1)
|
||||||
|
print("Part 2 result:", result_p2)
|
||||||
def part_2(input):
|
|
||||||
result = 0
|
|
||||||
line = input[0].rstrip().strip('0')
|
|
||||||
msg = bin(int(line, 16))
|
|
||||||
msg = msg[2:]
|
|
||||||
while len(msg) % 4:
|
|
||||||
msg = '0' + msg
|
|
||||||
packets = []
|
|
||||||
next = 0
|
|
||||||
while -1 != next:
|
|
||||||
next, _ = get_next_packet(msg, next, packets)
|
|
||||||
result = calc_result(packets)
|
|
||||||
print("Part 2 result:", result)
|
|
||||||
|
|
||||||
|
|
||||||
input = list()
|
input = list()
|
||||||
p = Path(__file__).with_name('input.txt')
|
p = Path(__file__).with_name('input.txt')
|
||||||
with open(p) as f:
|
with open(p) as f:
|
||||||
input = f.readlines()
|
input = f.readlines()
|
||||||
part_1(input)
|
solve(input)
|
||||||
part_2(input)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user