aboutsummaryrefslogtreecommitdiff
path: root/2020/four.nim
blob: c46929768d000a28b01280aff4ca1479181ebbba (plain) (blame)
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
# Day Four: Passport Processing
import os, strutils

let input: string = paramStr(1)
var fieldedPassports, validPassports: int = 0

for passport in input.readFile().split("\n\n"):
  var fields, valid: int = 0
  for pairs in passport.replace("\n", " ").strip().split(" "):
    let
      key: string = pairs.split(":")[0]
      value: string = pairs.split(":")[1]
    case key
    of "byr":
      if parseInt(value) in 1920 .. 2002:
        inc(valid)
    of "iyr":
      if parseInt(value) in 2010 .. 2020:
        inc(valid)
    of "eyr":
      if parseInt(value) in 2020 .. 2030:
        inc(valid)
    of "hgt":
      let
        units: string = value[^2..^1]
        height: string = value[0..^3]
      if units == "cm" and parseInt(height) in 150 .. 193:
        inc(valid)
      elif units == "in" and parseInt(height) in 59 .. 76:
        inc(valid)
    of "hcl":
      block hex:
        for char in value[1..^1]:
          if char notin {'0'..'9', 'a'..'f'}:
            break hex
        if value[0] == '#':
          inc(valid)
    of "ecl":
      if value in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
        inc(valid)
    of "pid":
      if len(value) == 9:
        inc(valid)
    else:
      dec(fields)
    inc(fields)
  if fields >= 7:
    inc(fieldedPassports)
  if valid >= 7:
    inc(validPassports)

echo fieldedPassports
echo validPassports