-
Notifications
You must be signed in to change notification settings - Fork 1
/
day13.nim
31 lines (27 loc) · 817 Bytes
/
day13.nim
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
import strutils
proc parse(path: string): (int, seq[(int, int)]) =
let file = open path
result[0] = parseInt readLine(file)
let buses = readLine(file).split(',')
for i, bus in buses:
if bus != "x":
result[1].add (i, parseInt(bus))
func part1(timestamp: int, buses: seq[(int, int)]): int =
var earliest = int.high
for (_, bus) in buses:
let wait = bus - (timestamp mod bus)
if wait < earliest:
earliest = wait
result = bus * wait
func part2(buses: seq[(int, int)]): int =
var prime = 1
for (i, bus) in buses:
let newPrime = prime * bus
for n in countup(result, newPrime, prime):
if (n+i) mod bus == 0:
result = n
prime = newPrime
break
let (timestamp, buses) = parse "inputs/13.txt"
echo part1(timestamp, buses)
echo part2(buses)