-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday04.nim
45 lines (41 loc) · 1.32 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
import aoc, strutils
func isValid1(passport: string): bool =
for r in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]:
if r notin passport: return false
return true
func isValid2(passport: string): bool =
for field in passport.splitWhitespace:
let v = field[4..^1] # ':' is always at index 3
case field[0..2]
of "byr":
if parseInt(v) notin 1920..2002: return false
of "iyr":
if parseInt(v) notin 2010..2020: return false
of "eyr":
if parseInt(v) notin 2020..2030: return false
of "hgt":
case v[^2..^1]
of "cm":
if parseInt(v[0..^3]) notin 150..193: return false
of "in":
if parseInt(v[0..^3]) notin 59..76: return false
else: return false
of "hcl":
if not (v.startsWith('#') and v.len == 7): return false
for h in v[1..6]:
if h notin {'0'..'9', 'a'..'f'}: return false
of "ecl":
if v notin ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]: return false
of "pid":
if v.len != 9: return false
for l in v:
if l notin {'0'..'9'}: return false
return true
proc solve(path: string): IntSolutions =
let passports = readFile path
for passport in passports.split("\n\n"):
if passport.isValid1:
inc result.first
if passport.isValid2:
inc result.second
echo solve "inputs/04.txt"