advent-of-code/2020/src/day2.nim

28 lines
1 KiB
Nim
Raw Normal View History

2020-12-03 09:57:12 -06:00
import streams, strutils, re
2020-12-02 21:33:18 -06:00
2020-12-03 09:54:58 -06:00
type PasswordPolicy = tuple[min: int, max: int, keyChar: char]
2020-12-02 21:33:18 -06:00
2020-12-03 11:42:18 -06:00
let parsePasswordLineRe = re"^(\d+)-(\d+) (.): (.*)$"
proc parsePasswordLine(str: string): (PasswordPolicy, string) =
var matches: array[4, 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
2020-12-03 09:54:58 -06:00
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 part1*(s: Stream): int =
2020-12-03 11:42:18 -06:00
for (pp, pw) in s.asPasswordPolicies:
if isValidPassword(pw, pp): inc result
2020-12-03 09:54:58 -06:00
proc part2*(s: Stream): int =
2020-12-03 11:42:18 -06:00
for (pp, pw) in s.asPasswordPolicies:
if isValidPasswordPart2(pw, pp): inc result