Skip to content

Commit 85023bc

Browse files
committed
🥒 ⭐ 🔑 🎅 🥒 Day 25, Sea Cucumbers did know where the keys are! 🔑 ⭐
1 parent a3d152c commit 85023bc

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# adventofcode-2021
22
:christmas_tree: :santa: Advent Of Code 2021 :anchor: :ship:
33

4+
![OMG The key was under the sub this whole time :O](calendar.gif)
5+
46
# Solutions
57

68
* [Day 1 - Sonar Sweep :boat: :anchor:](day1-sonar-sweep)
@@ -27,3 +29,4 @@
2729
* [Day 22 - Reactor Reboot :radioactive: :recycle:](day22-reactor-reboot)
2830
* [Day 23 - Amphipod :crab: :lobster: :shrimp: :lobster: :crab:](day23-amphipod)
2931
* [Day 24 - Arithmetic Logic Unit :floppy_disk: :computer: :stop_sign: :fire:](day24-arithmetic-logic-unit)
32+
* [Day 25 - Sea cucumber :cucumber: :star: :key: :santa: :cucumber:](day25-sea-cucumber)

calendar.gif

515 KB
Loading

day25-sea-cucumber/solution.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
def read_input(inpf):
2+
with open(inpf) as f:
3+
sea_map = []
4+
for line in f:
5+
line = line.strip()
6+
if not line:
7+
break
8+
sea_map.append([c for c in line])
9+
return sea_map
10+
11+
def to_state(sea_map):
12+
state = {'v': set(), '>': set()}
13+
for y, row in enumerate(sea_map):
14+
for x, c in enumerate(row):
15+
if c in '>v':
16+
state[c].add((x, y))
17+
return state, len(sea_map[0]), len(sea_map)
18+
19+
20+
def move_herd(state, width, height, herd):
21+
moving = state[herd]
22+
stationary = state['>' if herd == 'v' else 'v']
23+
dx, dy = (1, 0) if herd == '>' else (0, 1)
24+
25+
moved = set()
26+
unmoved = set()
27+
for x, y in moving:
28+
n = ((x+dx)%width, (y+dy)%height)
29+
if n in stationary or n in moving:
30+
unmoved.add((x, y))
31+
else:
32+
moved.add(n)
33+
ns = {}
34+
ns.update(state)
35+
ns[herd] = moved.union(unmoved)
36+
37+
return ns, len(moved)
38+
39+
def next_state(state, width, height):
40+
state, h_moved = move_herd(state, width, height, '>')
41+
state, v_moved = move_herd(state, width, height, 'v')
42+
43+
return state, h_moved or v_moved
44+
45+
46+
def print_state(state, w, h):
47+
vert = state['v']
48+
hor = state['>']
49+
for y in range(h):
50+
for x in range(w):
51+
n = (x, y)
52+
if n in vert:
53+
print('V', end='')
54+
elif n in hor:
55+
print('>', end='')
56+
else:
57+
print('.', end='')
58+
print()
59+
60+
61+
62+
def part1(sea_map):
63+
state, width, height = to_state(sea_map)
64+
step = 0
65+
while True:
66+
state, has_moved = next_state(state, width, height)
67+
step += 1
68+
if not has_moved:
69+
break
70+
return step
71+
72+
print(('\033[33m' + '★' + '\033[0m')*50)
73+
print('\033[32mThe cucumbers stop moving after: \033[0m\033[31m', part1(read_input('input')), '\033[0m\033[32msteps.\033[0m')
74+
print(('\033[33m' + '★' + '\033[0m' + '\033[32m' + '★' + '\033[0m')*25)

0 commit comments

Comments
 (0)