-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.nim
55 lines (43 loc) · 1.28 KB
/
day04.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import strutils, strscans, algorithm, tables
let input = readFile("./inputs/04.txt").splitLines
type
SleepRecord = array[60, int]
Solution = tuple
minute, guard: int
var
currentGuard, sleepStart: int
guards = initTable[int, SleepRecord]()
for i, line in sorted input:
var
date: string
hh, mm, guard: int
if line.scanf("[$+ $i:$i] Guard #$i begins shift", date, hh, mm, guard):
currentGuard = guard
if not guards.hasKey(guard):
var sr: SleepRecord
guards[guard] = sr
if line.scanf("[$+ $i:$i] falls asleep", date, hh, mm):
sleepStart = mm
if line.scanf("[$+ $i:$i] wakes up", date, hh, mm):
for m in sleepStart ..< mm:
inc guards[currentGuard][m]
var
highestSleepingTime, maxSleepAmount: int
first, second: Solution
for guard, sleepRecord in guards.pairs:
var sleepingTime, maxAmount, sleepiestMinute: int
for minute, slept in sleepRecord:
sleepingTime += slept
if slept > maxAmount:
maxAmount = slept
sleepiestMinute = minute
if slept > maxSleepAmount:
maxSleepAmount = slept
second = (minute, guard)
if sleepingTime > highestSleepingTime:
highestSleepingTime = sleepingTime
first = (sleepiestMinute, guard)
proc `$`(x: Solution): string =
$(x.minute * x.guard)
echo first
echo second