mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-12-21 15:46:28 +00:00
7
This commit is contained in:
parent
e5acaa81e6
commit
2664d8150b
13
2021/07/README.md
Normal file
13
2021/07/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# 07
|
||||
|
||||
The first ist quite east, just find the median, and then calculate cost.
|
||||
|
||||
For the second exercise we need triangle numbers. And the median won't work anymore.
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>349769</li>
|
||||
<li>99540554</li>
|
||||
</ol>
|
||||
</details>
|
58
2021/07/python/main.py
Normal file
58
2021/07/python/main.py
Normal file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from os.path import join, dirname
|
||||
|
||||
# Day 07
|
||||
|
||||
# Common
|
||||
|
||||
|
||||
def read_input(filename):
|
||||
data = join(dirname(__file__), '..', filename)
|
||||
with open(data) as f:
|
||||
return f.read().strip()
|
||||
|
||||
|
||||
test = read_input('test.txt')
|
||||
data = read_input('input.txt')
|
||||
|
||||
# 1
|
||||
|
||||
|
||||
def get_cheapest_alignment(raw: str) -> int:
|
||||
crabs = [int(x) for x in raw.split(',')]
|
||||
median = sorted(crabs)[len(crabs) // 2]
|
||||
costs = [abs(x - median) for x in crabs]
|
||||
return sum(costs)
|
||||
|
||||
|
||||
print('1.')
|
||||
result = get_cheapest_alignment(test)
|
||||
print(result)
|
||||
result = get_cheapest_alignment(data)
|
||||
print(result)
|
||||
|
||||
# 2
|
||||
|
||||
|
||||
def triangle_number(distance: int) -> int:
|
||||
return distance * (distance + 1) // 2
|
||||
|
||||
|
||||
def get_cheapest_alignment_complex(raw: str) -> int:
|
||||
crabs = [int(x) for x in raw.split(',')]
|
||||
lower = min(crabs)
|
||||
upper = max(crabs)
|
||||
lowest = -1
|
||||
for target in range(lower, upper + 1):
|
||||
cost = sum([triangle_number(abs(x - target)) for x in crabs])
|
||||
if lowest == -1 or cost < lowest:
|
||||
lowest = cost
|
||||
return lowest
|
||||
|
||||
|
||||
print('2.')
|
||||
result = get_cheapest_alignment_complex(test)
|
||||
print(result)
|
||||
result = get_cheapest_alignment_complex(data)
|
||||
print(result)
|
Loading…
Reference in New Issue
Block a user