This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
62 lines (37 loc) · 1.46 KB
/
day7.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
import os
import numpy as np
from aocd import get_data
from dotenv import load_dotenv
def get_session() -> str:
load_dotenv()
return os.getenv('SESSION_COOKIE')
def get_list(data: str = None, day: int = None, year: int = None):
if not data:
numbers = [int(x) for x in get_data(get_session(), day=day, year=year).split(',')]
else:
numbers = [int(x) for x in data.split(',')]
return numbers
def part1(numbers):
return get_part1_fuel(get_crabs(numbers), get_median(numbers))
def part2(numbers):
return get_part2_fuel(get_crabs(numbers), get_mean(numbers))
def get_part2_fuel(crabs, position):
for crab in crabs:
crab["fuel"] = get_cheapest_fuel(crab, position)
return sum([crab["fuel"] for crab in crabs])
def get_cheapest_fuel(crab, position):
return sum(range(abs(crab["position"] - position) + 1))
def get_part1_fuel(crabs, position):
for crab in crabs:
crab["fuel"] = get_horizontal_position(crab, position)
return sum([crab["fuel"] for crab in crabs])
def get_horizontal_position(crab, position):
return abs(crab["position"] - position)
def get_crabs(numbers):
return [{"position": int(i), "fuel": 0} for i in numbers]
def get_median(numbers):
return round(int(np.median(numbers)))
def get_mean(numbers):
return round(int(np.mean(numbers)))
print(f"Part 1: {part1(get_list(data=None, day=7, year=2021))}")
print(f"Part 2: {part2(get_list(data=None, day=7, year=2021))}")