add day 13
This commit is contained in:
parent
4802886c99
commit
3983a072b0
4 changed files with 44 additions and 0 deletions
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)
|
2
13/einput
Normal file
2
13/einput
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
939
|
||||||
|
7,13,x,x,59,x,31,19
|
2
13/input
Normal file
2
13/input
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
1000507
|
||||||
|
29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,631,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,13,19,x,x,x,23,x,x,x,x,x,x,x,383,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,17
|
Loading…
Add table
Reference in a new issue