mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-01-15 14:21:06 +00:00
2024 day 14 part 2
This commit is contained in:
parent
42b5ef35b9
commit
abb9d5ba1f
@ -8,9 +8,10 @@ def parse_bots(lines):
|
|||||||
return [lines[i:i+2] for i in range(0, len(lines), 2)] # [(pos, velocity), ...]
|
return [lines[i:i+2] for i in range(0, len(lines), 2)] # [(pos, velocity), ...]
|
||||||
|
|
||||||
|
|
||||||
def simulate_bots(bots, grid_size, steps=100):
|
def simulate_bots(bots, grid_size, steps=100, part2=False):
|
||||||
step = 0
|
step = 0
|
||||||
width, height = grid_size
|
width, height = grid_size
|
||||||
|
stats = []
|
||||||
while step < steps:
|
while step < steps:
|
||||||
new_bots = []
|
new_bots = []
|
||||||
for pos, velocity in bots:
|
for pos, velocity in bots:
|
||||||
@ -26,7 +27,13 @@ def simulate_bots(bots, grid_size, steps=100):
|
|||||||
new_bots.append((pos, velocity))
|
new_bots.append((pos, velocity))
|
||||||
bots = new_bots
|
bots = new_bots
|
||||||
step += 1
|
step += 1
|
||||||
return [pos for pos, _ in bots]
|
|
||||||
|
if part2:
|
||||||
|
# search step which maximizes safety value
|
||||||
|
safety = calculate_safety(Counter([p for p, _ in bots]), grid_size)
|
||||||
|
stats.append((safety, step))
|
||||||
|
|
||||||
|
return [pos for pos, _ in bots], stats
|
||||||
|
|
||||||
|
|
||||||
def determine_quadrant(pos, grid_size):
|
def determine_quadrant(pos, grid_size):
|
||||||
@ -43,23 +50,34 @@ def determine_quadrant(pos, grid_size):
|
|||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
def part1(bots, grid_size):
|
def calculate_safety(bots, grid_size):
|
||||||
bots = simulate_bots(bots, grid_size)
|
|
||||||
c = Counter(bots)
|
|
||||||
total_quadrants = [0, 0, 0, 0]
|
total_quadrants = [0, 0, 0, 0]
|
||||||
for pos, count in c.items():
|
for pos, count in bots.items():
|
||||||
q = determine_quadrant(pos, grid_size)
|
q = determine_quadrant(pos, grid_size)
|
||||||
if q is None: # ignore middle row and col
|
if q is None: # ignore middle row and col
|
||||||
continue
|
continue
|
||||||
total_quadrants[q] += count
|
total_quadrants[q] += count
|
||||||
#print_grid(c, grid_size)
|
|
||||||
return prod(total_quadrants)
|
return prod(total_quadrants)
|
||||||
|
|
||||||
|
|
||||||
|
def part1(bots, grid_size):
|
||||||
|
bots, _ = simulate_bots(bots, grid_size)
|
||||||
|
c = Counter(bots)
|
||||||
|
return calculate_safety(c, grid_size)
|
||||||
|
|
||||||
|
|
||||||
|
def part2(bots, grid_size):
|
||||||
|
max_step = grid_size[0] * grid_size[1] # input is periodic
|
||||||
|
_, stats = simulate_bots(bots, grid_size, max_step, part2=True)
|
||||||
|
return sorted(stats)[0][1]
|
||||||
|
|
||||||
|
|
||||||
def main(lines):
|
def main(lines):
|
||||||
bots = parse_bots(lines)
|
bots = parse_bots(lines)
|
||||||
total = part1(bots, grid_size)
|
total = part1(bots, grid_size)
|
||||||
print("Part 1: ", total)
|
print("Part 1: ", total)
|
||||||
|
p2 = part2(bots, grid_size)
|
||||||
|
print("Part 2: ", p2)
|
||||||
|
|
||||||
|
|
||||||
def print_grid(c, grid_size):
|
def print_grid(c, grid_size):
|
||||||
|
Loading…
Reference in New Issue
Block a user