h
This commit is contained in:
parent
d57166fdd7
commit
fe181838e8
2 changed files with 74 additions and 52 deletions
61
day14.py
61
day14.py
|
@ -2,57 +2,72 @@ import sys
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
def shift(map, dx, dy):
|
def shift(map, dx, dy):
|
||||||
swapped = True
|
for y in range(max(0, -dy), len(map) - max(0, dy)):
|
||||||
while swapped:
|
for x in range(max(0, -dx), len(map) - max(0, dx)):
|
||||||
swapped = False
|
(x1, y1) = (x, y)
|
||||||
for y in range(max(0, -dy), len(map) - max(0, dy)):
|
while True:
|
||||||
for x in range(max(0, -dx), len(map) - max(0, dx)):
|
(x2, y2) = (x1 + dx, y1 + dy)
|
||||||
if map[y + dy][x + dx] == "." and map[y][x] == "O":
|
if 0 <= y2 < len(map) and 0 <= x2 < len(map[y2]) and map[y2][x2] == ".":
|
||||||
map[y + dy][x + dx] = "O"
|
(x1, y1) = (x2, y2)
|
||||||
map[y][x] = "."
|
else:
|
||||||
swapped = True
|
break
|
||||||
|
|
||||||
|
if (x1, y1) != (x, y):
|
||||||
|
map[y1][x1] = "O"
|
||||||
|
map[y][x] = "."
|
||||||
|
|
||||||
|
|
||||||
def weigh(map):
|
def weigh(map):
|
||||||
return sum((len(map) - y) * row.count("O") for (y, row) in enumerate(map))
|
return sum((len(map) - y) * row.count("O") for (y, row) in enumerate(map))
|
||||||
|
|
||||||
CYCLES = 1_000_000_000
|
|
||||||
|
|
||||||
|
# @profile
|
||||||
def main():
|
def main():
|
||||||
|
CYCLES = 1_000_000_000
|
||||||
|
|
||||||
with open(sys.argv[1]) as f:
|
with open(sys.argv[1]) as f:
|
||||||
map = [list(l) for l in f.read().strip().splitlines()]
|
map = [[c for c in l] for l in f.read().strip().splitlines()]
|
||||||
|
|
||||||
part1 = 0
|
part1 = 0
|
||||||
|
pprint(map)
|
||||||
shift(map, 0, -1)
|
shift(map, 0, -1)
|
||||||
|
pprint(map)
|
||||||
part1 = weigh(map)
|
part1 = weigh(map)
|
||||||
|
shift(map, -1, 0)
|
||||||
|
pprint(map)
|
||||||
|
shift(map, 0, 1)
|
||||||
|
pprint(map)
|
||||||
|
shift(map, 1, 0)
|
||||||
|
pprint(map)
|
||||||
|
|
||||||
|
for i in range(1, 100):
|
||||||
|
shift(map, 0, -1)
|
||||||
|
shift(map, -1, 0)
|
||||||
|
shift(map, 0, 1)
|
||||||
|
shift(map, 1, 0)
|
||||||
|
|
||||||
seen = {}
|
seen = {}
|
||||||
history = []
|
history = []
|
||||||
for i in range(CYCLES):
|
(i, j) = (0, 0)
|
||||||
|
for i in range(100, CYCLES):
|
||||||
shift(map, 0, -1)
|
shift(map, 0, -1)
|
||||||
if i == 0: pprint(map)
|
|
||||||
shift(map, -1, 0)
|
shift(map, -1, 0)
|
||||||
if i == 0: pprint(map)
|
|
||||||
shift(map, 0, 1)
|
shift(map, 0, 1)
|
||||||
if i == 0: pprint(map)
|
|
||||||
shift(map, 1, 0)
|
shift(map, 1, 0)
|
||||||
if i == 0: pprint(map)
|
|
||||||
|
|
||||||
s = str(map)
|
s = str(map)
|
||||||
|
history.append(weigh(map))
|
||||||
if s in seen:
|
if s in seen:
|
||||||
|
j = seen[s]
|
||||||
break
|
break
|
||||||
seen[s] = i
|
seen[s] = i
|
||||||
history.append(weigh(map))
|
|
||||||
|
|
||||||
j = seen[s]
|
offset = (CYCLES - j) % (i - j) - 1
|
||||||
offset = (CYCLES - j) % (i + 1 - j) + j
|
|
||||||
part2 = history[offset]
|
part2 = history[offset]
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
# 98887 too low
|
|
||||||
# 98898 too high
|
|
||||||
# 103761 too high
|
|
||||||
|
|
65
day15.py
65
day15.py
|
@ -2,7 +2,8 @@ import os
|
||||||
import sys
|
import sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
def hash(s):
|
|
||||||
|
def hash(s: str) -> int:
|
||||||
v = 0
|
v = 0
|
||||||
for c in s:
|
for c in s:
|
||||||
if c == ",":
|
if c == ",":
|
||||||
|
@ -12,36 +13,42 @@ def hash(s):
|
||||||
v %= 256
|
v %= 256
|
||||||
return v
|
return v
|
||||||
|
|
||||||
with open(sys.argv[1]) as f:
|
|
||||||
s = f.read().strip()
|
|
||||||
|
|
||||||
boxes = defaultdict(list)
|
def main() -> None:
|
||||||
|
with open(sys.argv[1]) as f:
|
||||||
|
s = f.read().strip()
|
||||||
|
|
||||||
part1 = 0
|
boxes: defaultdict[int, list[tuple[str, int]]] = defaultdict(list)
|
||||||
for s_ in s.split(","):
|
|
||||||
part1 += hash(s_)
|
|
||||||
|
|
||||||
if s_[-1] == "-":
|
part1 = 0
|
||||||
label = s_[:-1]
|
for s_ in s.split(","):
|
||||||
h = hash(label)
|
part1 += hash(s_)
|
||||||
for i in range(len(boxes[h])):
|
|
||||||
if boxes[h][i][0] == label:
|
|
||||||
del boxes[h][i]
|
|
||||||
break
|
|
||||||
elif s_[-2] == "=":
|
|
||||||
label = s_[:-2]
|
|
||||||
focal = int(s_[-1])
|
|
||||||
h = hash(label)
|
|
||||||
for i in range(len(boxes[h])):
|
|
||||||
if boxes[h][i][0] == label:
|
|
||||||
boxes[h][i] = (label, focal)
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
boxes[h].append((label, focal))
|
|
||||||
|
|
||||||
part2 = 0
|
if s_[-1] == "-":
|
||||||
for (box_i, box) in boxes.items():
|
label = s_[:-1]
|
||||||
for (slot, (_, focal)) in enumerate(box):
|
h = hash(label)
|
||||||
part2 += (box_i + 1) * (slot + 1) * focal
|
for i in range(len(boxes[h])):
|
||||||
|
if boxes[h][i][0] == label:
|
||||||
|
del boxes[h][i]
|
||||||
|
break
|
||||||
|
elif s_[-2] == "=":
|
||||||
|
label = s_[:-2]
|
||||||
|
focal = int(s_[-1])
|
||||||
|
h = hash(label)
|
||||||
|
for i in range(len(boxes[h])):
|
||||||
|
if boxes[h][i][0] == label:
|
||||||
|
boxes[h][i] = (label, focal)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
boxes[h].append((label, focal))
|
||||||
|
|
||||||
print(part1, part2)
|
part2 = 0
|
||||||
|
for (box_i, box) in boxes.items():
|
||||||
|
for (slot, (_, focal)) in enumerate(box):
|
||||||
|
part2 += (box_i + 1) * (slot + 1) * focal
|
||||||
|
|
||||||
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue