aboutsummaryrefslogtreecommitdiff
path: root/2020/four.nim
diff options
context:
space:
mode:
authorj-james2020-12-04 10:45:26 +0000
committerj-james2020-12-04 10:56:54 +0000
commit7e3e5476f4a22b6a8dbfb685ee55ec1a74ffb66a (patch)
tree91ba93667b12428a6f755c59297c80112902ccd2 /2020/four.nim
parent4edd4fd104d4ff5f5e118e1327d800ec56fd3087 (diff)
Day Four
Diffstat (limited to '2020/four.nim')
-rw-r--r--2020/four.nim57
1 files changed, 57 insertions, 0 deletions
diff --git a/2020/four.nim b/2020/four.nim
new file mode 100644
index 0000000..a550b75
--- /dev/null
+++ b/2020/four.nim
@@ -0,0 +1,57 @@
+# Day Four: Passport Processing
+import os, strutils
+
+let input: string = paramStr(1)
+var fieldedPassports, validPassports: int = 0
+
+for passport in split(readFile(input), "\n\n"):
+ var fields, valid: int = 0
+ for pairs in split(strip(replace(passport, "\n", " ")), " "):
+ let
+ key: string = split(pairs, ":")[0]
+ value: string = split(pairs, ":")[1]
+ case key
+ of "byr":
+ if parseInt(value) >= 1920 and parseInt(value) <= 2002:
+ inc(valid)
+ inc(fields)
+ of "iyr":
+ if parseInt(value) >= 2010 and parseInt(value) <= 2020:
+ inc(valid)
+ inc(fields)
+ of "eyr":
+ if parseInt(value) >= 2020 and parseInt(value) <= 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:
+ inc(valid)
+ elif units == "in" and parseInt(height) >= 59 and parseInt(height) <= 76:
+ inc(valid)
+ inc(fields)
+ of "hcl":
+ block 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)
+ if fields >= 7:
+ inc(fieldedPassports)
+ if valid >= 7:
+ inc(validPassports)
+
+echo fieldedPassports
+echo validPassports