From e6683932e88705a494ce82d9e8c306248d6f2d1f Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 4 Dec 2020 11:02:05 -0600 Subject: [PATCH] More cleanup, stick precompiled regexes in a table --- 2020/src/aoc2020.nim | 4 ++-- 2020/src/day2.nim | 20 ++++++-------------- 2020/src/day4.nim | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/2020/src/aoc2020.nim b/2020/src/aoc2020.nim index df5140a..720eab8 100644 --- a/2020/src/aoc2020.nim +++ b/2020/src/aoc2020.nim @@ -8,5 +8,5 @@ proc solve_for_day(n: int) {.used.} = echo solvers[n]() when isMainModule: - # solve_all() - solve_for_day(4) + solve_all() + # solve_for_day(4) diff --git a/2020/src/day2.nim b/2020/src/day2.nim index d62ae7d..df05993 100644 --- a/2020/src/day2.nim +++ b/2020/src/day2.nim @@ -1,4 +1,4 @@ -import streams, strutils, re +import streams, strutils, re, sequtils type PasswordPolicy = tuple[min: int, max: int, keyChar: char] @@ -8,20 +8,12 @@ proc parsePasswordLine(str: string): (PasswordPolicy, string) = if match(str, parsePasswordLineRe, matches): return ((min: parseInt(matches[0]), max: parseInt(matches[1]), keyChar: matches[2][0]), matches[3]) -iterator asPasswordPolicies(s: Stream): (PasswordPolicy, string) = - for line in s.lines(): yield parsePasswordLine line - -proc isValidPassword(str: string, pp: PasswordPolicy): bool = - let count = str.count pp.keyChar - (pp.min <= count) and (count <= pp.max) - -proc isValidPasswordPart2(str: string, pp: PasswordPolicy): bool = - (str[pp.min - 1] == pp.keyChar) xor (pp.keyChar == str[pp.max - 1]) +proc asPasswordPolicies(s: Stream): seq[(PasswordPolicy, string)] = + toSeq(s.lines()).mapIt(it.parsePasswordLine) proc part1*(s: Stream): int = - for (pp, pw) in s.asPasswordPolicies: - if isValidPassword(pw, pp): inc result + s.asPasswordPolicies.countIt(it[1].count(it[0].keyChar) in it[0].min..it[0].max) proc part2*(s: Stream): int = - for (pp, pw) in s.asPasswordPolicies: - if isValidPasswordPart2(pw, pp): inc result + s.asPasswordPolicies.countIt( + (it[1][it[0].min - 1] == it[0].keyChar) xor (it[0].keyChar == it[1][it[0].max - 1])) diff --git a/2020/src/day4.nim b/2020/src/day4.nim index ec45a33..46b36ba 100644 --- a/2020/src/day4.nim +++ b/2020/src/day4.nim @@ -1,15 +1,23 @@ import streams, strutils, sets, sequtils, sugar, re, tables +let regexes = toTable({ + "hcl": re"^#[0-9a-f]{6}$", + "ecl": re"^(amb|blu|brn|gry|grn|hzl|oth)$", + "pid": re"^\d{9}$", + "hgt": re"^\d+(cm|in)$", + "nondec": re"[^0-9]", +}) + let validators = toTable({ - "hcl": (v: string) => v.match re"^#[0-9a-f]{6}$", - "ecl": (v: string) => v.match re"^(amb|blu|brn|gry|grn|hzl|oth)$", - "pid": (v: string) => v.match re"^\d{9}$", + "hcl": (v: string) => v.match regexes["hcl"], + "ecl": (v: string) => v.match regexes["ecl"], + "pid": (v: string) => v.match regexes["pid"], "byr": (v: string) => parseInt(v) in 1920..2002, "iyr": (v: string) => parseInt(v) in 2010..2020, "eyr": (v: string) => parseInt(v) in 2020..2030, "hgt": proc (v: string): bool = let (mn, mx) = if v.endsWith("cm"): (150, 193) else: (59, 76) - v.match(re"^\d+(cm|in)$") and v.replace(re"[^0-9]").parseInt in mn..mx, + v.match(regexes["hgt"]) and v.replace(regexes["nondec"]).parseInt in mn..mx, "cid": (v: string) => true, })