-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
executable file
·47 lines (33 loc) · 1.25 KB
/
day07.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
#! /usr/bin/env python3
### local imports
import utils
def burnCost(x: int, y: int) -> int:
return abs(x - y)
def advancedBurnCost(x: int, y: int) -> int:
n = burnCost(x, y)
return (n * (n + 1)) // 2
@utils.part1
def part1(puzzleInput: str):
# Parse the horizontal position ints from the puzzle input
positions = [int(n) for n in puzzleInput.strip().split(",")]
# Iterate through the the various possible alignment points and calculate
# the total movement cost for each submarine therein
costs = [
sum([burnCost(n, target) for n in positions])
for target in range(min(positions), max(positions) + 1)
]
# The answer is the alignment point with the lowest total cost
utils.printAnswer(min(costs))
# Pass the parsed positions to Part 2
return positions
@utils.part2
def part2(_, positions: list[int]):
# Similar to Part 1, we will iterate through each alignment option,
# but this time we will use the more complicated fuel usage algorithm
costs = [
sum([advancedBurnCost(n, target) for n in positions])
for target in range(min(positions), max(positions) + 1)
]
# Once again, the answer is the lowest total cost
utils.printAnswer(min(costs))
utils.start()