Part 1 | Part 2 | Total | |
---|---|---|---|
Time | >24h | 2:17 | >24h |
This is another day that I didn't stay up until midnight to do the problem when it came out. Completed on 12/24. Took roughly 55:19 in total.
from helpers.datagetter import data_in, submit
from collections import defaultdict
ans = 0
din = data_in(split=True, numbers=False)
grid = defaultdict(int)
for i in range(len(din)):
for j in range(len(din[0])):
if din[i][j] == "#":
grid[(j, i)] = 1
num_elves = len(grid)
def get_adjacent(at):
x, y = at
output = []
for i in range(-1, 2):
output.append(grid[(x + i, y - 1)])
output.append(grid[(x + 1, y)])
for i in range(1, -2, -1):
output.append(grid[(x + i, y + 1)])
output.append(grid[(x - 1, y)])
output.append(grid[(x - 1, y - 1)])
return output
dire = 0
# Simulate 10 rounds
for _ in range(10):
elves = grid.copy().keys()
moves = defaultdict(list)
for elf in elves:
if grid[elf] != 1:
continue
adj = get_adjacent(elf)
if max(adj) == 0:
continue
for i in range(4):
if (dire + i) % 4 == 0:
if max(adj[0:3]) == 0:
moves[(elf[0], elf[1] - 1)].append(elf)
break
elif (dire + i) % 4 == 1:
if max(adj[4:7]) == 0:
moves[(elf[0], elf[1] + 1)].append(elf)
break
elif (dire + i) % 4 == 2:
if max(adj[6:9]) == 0:
moves[(elf[0] - 1, elf[1])].append(elf)
break
elif (dire + i) % 4 == 3:
if max(adj[2:5]) == 0:
moves[(elf[0] + 1, elf[1])].append(elf)
break
for to, elves in moves.items():
if len(elves) == 1:
grid[elves[0]] = 0
grid[to] = 1
dire += 1
tl = [5, 5]
br = [5, 5]
for key, val in grid.items():
if val:
tl[0] = min(tl[0], key[0])
br[0] = max(br[0], key[0])
tl[1] = min(tl[1], key[1])
br[1] = max(br[1], key[1])
ans = (br[1] - tl[1] + 1) * (br[0] - tl[0] + 1) - num_elves
print(ans)
submit(ans)
Small change to make it run until all elves don't have any other elves around them.
from helpers.datagetter import data_in, submit
from collections import defaultdict
ans = 0
din = data_in(split=True, numbers=False)
grid = defaultdict(int)
for i in range(len(din)):
for j in range(len(din[0])):
if din[i][j] == "#":
grid[(j, i)] = 1
num_elves = len(grid)
def get_adjacent(at):
x, y = at
output = []
for i in range(-1, 2):
output.append(grid[(x + i, y - 1)])
output.append(grid[(x + 1, y)])
for i in range(1, -2, -1):
output.append(grid[(x + i, y + 1)])
output.append(grid[(x - 1, y)])
output.append(grid[(x - 1, y - 1)])
return output
dire = 0
found = True
while found:
elves = grid.copy().keys()
moves = defaultdict(list)
found = False
for elf in elves:
if grid[elf] != 1:
continue
adj = get_adjacent(elf)
if max(adj) == 0:
continue
found = True
for i in range(4):
if (dire + i) % 4 == 0:
if max(adj[0:3]) == 0:
moves[(elf[0], elf[1] - 1)].append(elf)
break
elif (dire + i) % 4 == 1:
if max(adj[4:7]) == 0:
moves[(elf[0], elf[1] + 1)].append(elf)
break
elif (dire + i) % 4 == 2:
if max(adj[6:9]) == 0:
moves[(elf[0] - 1, elf[1])].append(elf)
break
elif (dire + i) % 4 == 3:
if max(adj[2:5]) == 0:
moves[(elf[0] + 1, elf[1])].append(elf)
break
for to, elves in moves.items():
if len(elves) == 1:
grid[elves[0]] = 0
grid[to] = 1
dire += 1
print(dire)
submit(dire)