~deciduously/aoc2020

b949c81055235f0b651e05d83c65e1374febdcd1 — Ben Lovy 1 year, 6 months ago e87bde2
Day 4 Part 2
2 files changed, 36 insertions(+), 15 deletions(-)

M 4/day4
M 4/day4.nim
M 4/day4 => 4/day4 +0 -0
M 4/day4.nim => 4/day4.nim +36 -15
@@ 1,4 1,4 @@
import options, strutils
import options, re, strutils

proc readLines(name: string): seq[string] =
    var ret: seq[string] = @[]


@@ 29,7 29,9 @@ proc `$`(p: Passport): string =
        "No ID"

proc newPassport(): Passport =
    Passport(byr: none(string), iyr: none(string), eyr: none(string), hgt: none(string), hcl: none(string), ecl: none(string), pid: none(string), cid: none(string))
    Passport(byr: none(string), iyr: none(string), eyr: none(string), hgt: none(
            string), hcl: none(string), ecl: none(string), pid: none(string),
            cid: none(string))

proc passportFromString(str: string): Passport =
    let parts = str.split(' ')


@@ 55,22 57,41 @@ proc passportFromString(str: string): Passport =
                ret.cid = some(kv[1])
            else: break
    ret
            

proc isValidPartOne(p: Passport): bool =
    p.byr.isSome and p.iyr.isSome and p.eyr.isSome and p.hgt.isSome and p.hcl.isSome and p.ecl.isSome and p.pid.isSome
    p.byr.isSome and p.iyr.isSome and p.eyr.isSome and p.hgt.isSome and
            p.hcl.isSome and p.ecl.isSome and p.pid.isSome

# proc validateByr(str: string): bool
# etc
proc validateByr(str: Option[string]): bool =
    if str.isNone: return false
    let i = parseInt(str.get())
    i >= 1920 and i <= 2002

proc validateIyr(str: Option[string]): bool =
    if str.isNone: return false
    let i = parseInt(str.get())
    i >= 2010 and i <= 2020

proc validateEyr(str: Option[string]): bool =
    if str.isNone: return false
    let i = parseInt(str.get())
    i >= 2020 and i <= 2030

proc validateHgt(str: Option[string]): bool =
    str.isSome and match(str.get(), re"[0-9]+(in|cm)")

proc validateHcl(str: Option[string]): bool =
    str.isSome and match(str.get(), re"#[0-9a-f]{6}")

proc validateEcl(str: Option[string]): bool =
    str.isSome and match(str.get(), re"(amb|blu|brn|gry|grn|hzl|oth)")

proc validatePid(str: Option[string]): bool =
    str.isSome and match(str.get(), re"[0-9]{9}")

proc isValidPartTwo(p: Passport): bool =
    # byr
    # iyr
    # eyr
    # hgt
    # hcl
    # ecl
    # pid
    validateByr(p.byr) and validateIyr(p.iyr) and validateHgt(p.hgt) and validateEyr(p.eyr) and
    validateHcl(p.hcl) and validateEcl(p.ecl) and validatePid(p.pid)

proc generatePassports(filename: string): seq[Passport] =
    let strs = readLines(filename)


@@ 83,6 104,6 @@ let passports = generatePassports("puzzle")
echo passports
var res = 0
for pass in passports:
    if pass.isValid(): res += 1
    if pass.isValidPartTwo(): res += 1

echo res
\ No newline at end of file
echo res