-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202107.py
80 lines (56 loc) · 2.02 KB
/
aoc202107.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""AoC 7, 2021: The Treachery of Whales."""
# Standard library imports
import collections
import functools
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return collections.Counter([int(pos) for pos in puzzle_input.split(",")])
def part1(data):
"""Solve part 1."""
return optimize(data, metric=linear_fuel(data))
def part2(data):
"""Solve part 2."""
return optimize(data, metric=triangular_fuel(data))
def optimize(crabs, metric):
"""Find the best position based on the metric function."""
# Guess a first to-position
pos = round(
sum(num_crabs * pos for pos, num_crabs in crabs.items()) / crabs.total()
)
pos_left, pos_right = pos - 1, pos + 1
step = -1 if metric(pos_left) < metric(pos_right) else 1
# Move towards the global minimum
while metric(pos + step) < metric(pos):
pos += step
return metric(pos)
def linear_fuel(crabs):
"""Factory for linear fuel metrics."""
@functools.cache
def _linear_fuel(to_pos):
"""Calculate total fuel needed to move to a given position, each step
costs 1 fuel."""
return sum(num_crabs * abs(to_pos - pos) for pos, num_crabs in crabs.items())
return _linear_fuel
def triangular_fuel(crabs):
"""Factory for triangular fuel metrics."""
@functools.cache
def _triangular_fuel(to_pos):
"""Calculate total fuel needed to move to a given position, each step
costs 1 extra fuel."""
return sum(
num_crabs * (steps := abs(to_pos - pos)) * (steps + 1) // 2
for pos, num_crabs in crabs.items()
)
return _triangular_fuel
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))