From aa2b28e4ef7c0f0388acd7f0be73860d0d6c85ba Mon Sep 17 00:00:00 2001 From: j-james Date: Fri, 4 Dec 2020 03:17:13 -0800 Subject: Day Four: Clean up code --- 2020/four.nim | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/2020/four.nim b/2020/four.nim index a550b75..89bc335 100644 --- a/2020/four.nim +++ b/2020/four.nim @@ -12,42 +12,38 @@ for passport in split(readFile(input), "\n\n"): value: string = split(pairs, ":")[1] case key of "byr": - if parseInt(value) >= 1920 and parseInt(value) <= 2002: + if parseInt(value) in 1920 .. 2002: inc(valid) - inc(fields) of "iyr": - if parseInt(value) >= 2010 and parseInt(value) <= 2020: + if parseInt(value) in 2010 .. 2020: inc(valid) - inc(fields) of "eyr": - if parseInt(value) >= 2020 and parseInt(value) <= 2030: + if parseInt(value) in 2020 .. 2030: inc(valid) - inc(fields) of "hgt": let - units: string = value[len(value)-2..len(value)-1] - height: string = value[0..len(value)-3] - if units == "cm" and parseInt(height) >= 150 and parseInt(height) <= 193: + 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) >= 59 and parseInt(height) <= 76: + elif units == "in" and parseInt(height) in 59 .. 76: inc(valid) - inc(fields) of "hcl": block hex: + for char in value[1..^1]: + if char notin {'0'..'9', 'a'..'f'}: + break hex if value[0] == '#': - for char in value[1..len(value)-1]: - if char notin {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}: - break hex inc(valid) - inc(fields) of "ecl": if value in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]: inc(valid) - inc(fields) of "pid": if len(value) == 9: inc(valid) - inc(fields) + else: + dec(fields) + inc(fields) if fields >= 7: inc(fieldedPassports) if valid >= 7: -- cgit v1.2.3-70-g09d2