aboutsummaryrefslogtreecommitdiff
path: root/2019/nim/day04.nim
diff options
context:
space:
mode:
authorj-james2022-12-12 02:15:44 +0000
committerj-james2022-12-12 02:18:55 +0000
commita7ba598a54a8883c83cb9ef2b04d653eecd92e28 (patch)
tree06d362d3a88c615d8747ddf9a961bf491a8575a5 /2019/nim/day04.nim
parent13007178188cd89b31e3e41090e8a8ed64867c13 (diff)
Reorganize 2019 solutions
Diffstat (limited to '2019/nim/day04.nim')
-rw-r--r--2019/nim/day04.nim37
1 files changed, 37 insertions, 0 deletions
diff --git a/2019/nim/day04.nim b/2019/nim/day04.nim
new file mode 100644
index 0000000..eb6d933
--- /dev/null
+++ b/2019/nim/day04.nim
@@ -0,0 +1,37 @@
+# Day Four: Secure Container
+import os, strutils, sequtils
+
+let input: string = paramStr(1)
+let bounds: seq[int] = map(split(strip(readFile(input)), '-'), parseInt)
+var valid, details: int = 0
+
+proc criteria(password: int): bool =
+ var password: string = $password
+ for digit in 0 .. 4:
+ if password[digit] > password[digit+1]:
+ return false
+ for digit in 0 .. 4:
+ if password[digit] == password[digit+1]:
+ return true
+ return false
+
+proc extracriteria(password: int): bool =
+ var password: string = $password
+ for digit in 0 .. 4:
+ if password[digit] > password[digit+1]:
+ return false
+ for digit in 0 .. 4:
+ if password[digit] == password[digit+1]:
+ if digit == 0 or password[digit] != password[digit-1]:
+ if digit == 4 or password[digit] != password[digit+2]:
+ return true
+ return false
+
+for password in bounds[0] .. bounds[1]:
+ if criteria(password): inc(valid)
+
+for password in bounds[0] .. bounds[1]:
+ if extracriteria(password): inc(details)
+
+echo valid
+echo details