add day 13
This commit is contained in:
parent
4802886c99
commit
3983a072b0
9
13/1.py
Normal file
9
13/1.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
with open("input") as f:
|
||||
content = [x.strip() for x in f]
|
||||
|
||||
timestamp = int(content[0])
|
||||
buses = [int(x) for x in content[1].split(",") if x != "x"]
|
||||
next_deps = [((int(timestamp / x) + 1) * x) - timestamp for x in buses]
|
||||
next_dep = min(next_deps)
|
||||
next_bus = buses[next_deps.index(next_dep)]
|
||||
print(next_bus * next_dep)
|
31
13/2.py
Normal file
31
13/2.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
def is_correct_timestamp(buses, timestamp):
|
||||
for pos, bus in buses:
|
||||
if (timestamp + pos) % bus != 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
# t -> initial timestamp
|
||||
# dx -> dx
|
||||
def doa2(buses, t, dx):
|
||||
timestamp = t
|
||||
while True:
|
||||
if is_correct_timestamp(buses, timestamp):
|
||||
break
|
||||
timestamp += dx
|
||||
|
||||
return timestamp
|
||||
|
||||
with open("input") as f:
|
||||
content = [x.strip() for x in f]
|
||||
|
||||
splcont = content[1].split(",")
|
||||
tbuses = zip(range(len(splcont)), [int(x) if x != "x" else None for x in splcont])
|
||||
buses = list(filter(lambda x: x[1] != None, tbuses))
|
||||
|
||||
dx = buses[0][1]
|
||||
t = buses[0][0]
|
||||
for i in range(1, len(buses)):
|
||||
t = doa2(buses[i:i+2], t, dx)
|
||||
dx *= buses[i][1]
|
||||
|
||||
print(t)
|
Loading…
Reference in a new issue