Added 2020 (python)
This commit is contained in:
BIN
2020/13/__pycache__/prog.cpython-38.pyc
Normal file
BIN
2020/13/__pycache__/prog.cpython-38.pyc
Normal file
Binary file not shown.
BIN
2020/13/__pycache__/test.cpython-38.pyc
Normal file
BIN
2020/13/__pycache__/test.cpython-38.pyc
Normal file
Binary file not shown.
2
2020/13/input
Normal file
2
2020/13/input
Normal file
@ -0,0 +1,2 @@
|
||||
1002462
|
||||
37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,601,x,x,x,x,x,x,x,x,x,x,x,19,x,x,x,x,17,x,x,x,x,x,23,x,x,x,x,x,29,x,443,x,x,x,x,x,x,x,x,x,x,x,x,13
|
52
2020/13/prog.py
Normal file
52
2020/13/prog.py
Normal file
@ -0,0 +1,52 @@
|
||||
from math import gcd
|
||||
|
||||
def get_input(sample = False):
|
||||
with open("sample" if sample else 'input', 'r') as f:
|
||||
ret = []
|
||||
lines = f.readlines()
|
||||
ret.append(int(lines[0]))
|
||||
ret.append([int(x) if x != "x" else x for x in lines[1].split(',')])
|
||||
|
||||
return ret
|
||||
|
||||
def get_next_bus_departure(bus_id: int, cur_time: int):
|
||||
return bus_id * math.ceil(cur_time / bus_id)
|
||||
|
||||
def get_result(data: list, part = 1):
|
||||
cur_time = data[0]
|
||||
busses = data[1]
|
||||
|
||||
if part == 1:
|
||||
eta_n_bus = {}
|
||||
for bus in busses:
|
||||
if bus == 'x': continue
|
||||
|
||||
eta = get_next_bus_departure(bus, cur_time)
|
||||
eta_n_bus[eta] = bus
|
||||
|
||||
earliest = min(eta_n_bus.keys())
|
||||
earliest_bus = eta_n_bus[earliest]
|
||||
waiting_time = earliest - cur_time
|
||||
return waiting_time * earliest_bus
|
||||
else:
|
||||
step = busses[0]
|
||||
bus_n_offsets = [(bus, offset) for offset, bus in enumerate(busses) if bus != 'x']
|
||||
time = 0
|
||||
|
||||
for bus, offset in bus_n_offsets[1:]:
|
||||
while True:
|
||||
time += step
|
||||
if (time + offset) % bus == 0:
|
||||
step = lcm(step, bus)
|
||||
break
|
||||
return time
|
||||
|
||||
|
||||
|
||||
|
||||
def lcm(a, b):
|
||||
return abs(a*b) // gcd(a, b)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(get_result(get_input(), part = 2))
|
2
2020/13/sample
Normal file
2
2020/13/sample
Normal file
@ -0,0 +1,2 @@
|
||||
939
|
||||
7,13,x,x,59,x,31,19
|
20
2020/13/test.py
Normal file
20
2020/13/test.py
Normal file
@ -0,0 +1,20 @@
|
||||
from prog import *
|
||||
import unittest
|
||||
|
||||
class Methods(unittest.TestCase):
|
||||
def test_get_input(self):
|
||||
inp = get_input(sample = True)
|
||||
self.assertEqual(inp, [939, [7, 13, 'x', 'x', 59, 'x', 31, 19]])
|
||||
|
||||
class Result(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.inp = get_input(sample = True)
|
||||
|
||||
def test_part_1(self):
|
||||
self.assertEqual(get_result(self.inp), 295)
|
||||
|
||||
def test_part_2(self):
|
||||
self.assertEqual(get_result([0, [7, 13]], part = 2), 77)
|
||||
self.assertEqual(get_result([0, [17, 'x', 13, 19]], part = 2), 3417)
|
||||
self.assertEqual(get_result(self.inp, part = 2), 1068781)
|
||||
|
Reference in New Issue
Block a user