-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (26 loc) · 1.1 KB
/
main.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
from src.common.file_utils import get_path, read_lines
def part_one(filename: str) -> int:
lines = read_lines(get_path(__file__, filename))
earliest, shortest_wait, bus_id = int(lines[0]), float('inf'), None
for b in [int(x) for x in lines[1].split(',') if x.isnumeric()]:
wait = (((earliest // b) * b) + b) - earliest
if wait < shortest_wait:
shortest_wait, bus_id = wait, b
return shortest_wait * bus_id if shortest_wait else -1
def part_two(filename: str) -> int:
lines = read_lines(get_path(__file__, filename))
running_product, earliest_bus = 1, 0
# uses chinese remainder theorem
for (index, bus) in enumerate(lines[1].split(",")):
if bus != "x":
while ((earliest_bus + index) % int(bus)) != 0:
earliest_bus += running_product
running_product *= int(bus)
return earliest_bus
if __name__ == '__main__':
print("---Part One---")
part_one_res = part_one("input.txt")
print(part_one_res)
print("---Part Two---")
part_two_res = part_two("input.txt")
print(part_two_res)