aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authorj-james2020-12-03 06:50:25 +0000
committerj-james2020-12-03 06:50:25 +0000
commitca62f5d0ae8a96c5d85d3a447a00150d186be605 (patch)
tree76a9e8bf93e309a77abf1f044e359dd7967dd98b /2020
parent7ffad964f3fab79b29f5705d1481a93613921a66 (diff)
Day Two: Create visualization
Diffstat (limited to '2020')
-rw-r--r--2020/twoFancy.nim70
1 files changed, 70 insertions, 0 deletions
diff --git a/2020/twoFancy.nim b/2020/twoFancy.nim
new file mode 100644
index 0000000..a16ef06
--- /dev/null
+++ b/2020/twoFancy.nim
@@ -0,0 +1,70 @@
+import os, strutils, terminal
+
+let input = paramStr(1)
+var unofficial, official: string
+var unofficialCount, officialCount: int = 0
+var length: int = 20
+
+for line in lines(input):
+ var frequency: int = 0
+ var unofficialPass, officialPass, unofficialValidity, officialValidity: string
+ let
+ entry: seq[string] = split(line, {'-', ' ', ':'})
+ min: int = parseInt(entry[0])
+ max: int = parseInt(entry[1])
+ letter: char = entry[2][0]
+ pass: string = entry[4]
+
+ if len(pass) > length:
+ length = len(pass)
+
+ for char in pass:
+ if char == letter:
+ inc(frequency)
+ if frequency < min:
+ unofficialPass.add(ansiForegroundColorCode(fgBlue) & $char)
+ elif char == letter and frequency > max:
+ unofficialPass.add(ansiForegroundColorCode(fgRed) & $char)
+ else:
+ unofficialPass.add(ansiForegroundColorCode(fgGreen) & $char)
+ else:
+ unofficialPass.add(ansiForegroundColorCode(fgDefault) & $char)
+ unofficialPass.add(ansiForegroundColorCode(fgDefault))
+ if min <= frequency and frequency <= max:
+ inc(unofficialCount)
+ unofficialValidity = ansiForegroundColorCode(fgGreen) & "VALID (" & $frequency & ")" & ansiForegroundColorCode(fgDefault)
+ elif frequency < min:
+ unofficialValidity = ansiForegroundColorCode(fgBlue) & "TOO FEW (" & $frequency & " < " & $min & ")" & ansiForegroundColorCode(fgDefault)
+ elif frequency > max:
+ unofficialValidity = ansiForegroundColorCode(fgRed) & "TOO MANY (" & $frequency & " > " & $max & ")" & ansiForegroundColorCode(fgDefault)
+
+ for i in 0..len(pass)-1:
+ if i == min-1:
+ if pass[min-1] == letter:
+ officialPass.add(ansiForegroundColorCode(fgGreen))
+ else:
+ officialPass.add(ansiForegroundColorCode(fgRed))
+ elif i == max-1:
+ if pass[max-1] == letter:
+ officialPass.add(ansiForegroundColorCode(fgGreen))
+ else:
+ officialPass.add(ansiForegroundColorCode(fgRed))
+ else:
+ officialPass.add(ansiForegroundColorCode(fgDefault))
+ officialPass.add(pass[i])
+ if pass[min-1] == letter xor pass[max-1] == letter:
+ inc(officialCount)
+ officialValidity = ansiForegroundColorCode(fgGreen) & "VALID (XOR)" & ansiForegroundColorCode(fgDefault)
+ elif pass[min-1] == letter and pass[max-1] == letter:
+ officialValidity = ansiForegroundColorCode(fgRed) & "INVALID (AND)" & ansiForegroundColorCode(fgDefault)
+ else:
+ officialValidity = ansiForegroundColorCode(fgBlue) & "INVALID (NOT)" & ansiForegroundColorCode(fgDefault)
+
+ unofficial.add(alignLeft($min & "-" & $max, 6) & ansiStyleCode(1) & $letter & ansiStyleCode(0) & ": " & unofficialPass & repeat(" ", length+2 - len(pass)) & ansiStyleCode(1) & unofficialValidity & ansiStyleCode(0) & "\n")
+ official.add(alignLeft($min & "-" & $max, 6) & ansiStyleCode(1) & $letter & ansiStyleCode(0) & ": " & unofficialPass & repeat(" ", length+2 - len(pass)) & ansiStyleCode(1) & officialValidity & ansiStyleCode(0) & "\n")
+
+unofficial.add("TOTAL VALID PASSWORDS (The Sled Rental Place Down The Street Policy): " & $unofficialCount & "\n")
+official.add("TOTAL VALID PASSWORDS (The Official Toboggan Corporate Policy): " & $officialCount)
+
+echo unofficial
+echo official