48 lines
995 B
Nim
48 lines
995 B
Nim
|
import std/[strutils]
|
||
|
import lib/loader
|
||
|
|
||
|
|
||
|
type
|
||
|
PasswordItem = object
|
||
|
min: int
|
||
|
max: int
|
||
|
letter: char
|
||
|
pwd: string
|
||
|
|
||
|
|
||
|
var data: seq[PasswordItem]
|
||
|
for line in loadStrings(2):
|
||
|
let parts = line.split()
|
||
|
let minmax = parts[0].split('-')
|
||
|
let item = PasswordItem(
|
||
|
min: parseInt(minmax[0]),
|
||
|
max: parseInt(minmax[1]),
|
||
|
letter: parts[1].strip(chars = {':'})[0],
|
||
|
pwd: parts[2]
|
||
|
)
|
||
|
data.add(item)
|
||
|
|
||
|
|
||
|
proc partOne() =
|
||
|
var total = 0
|
||
|
for item in data:
|
||
|
let c = item.pwd.count(item.letter)
|
||
|
if c >= item.min and c <= item.max:
|
||
|
total += 1
|
||
|
echo "One: ", total
|
||
|
|
||
|
|
||
|
proc partTwo() =
|
||
|
var total = 0
|
||
|
for item in data:
|
||
|
let first = (item.pwd[item.min - 1] == item.letter)
|
||
|
let last = (item.pwd[item.max - 1] == item.letter)
|
||
|
if (first and not last) or (last and not first):
|
||
|
total += 1
|
||
|
echo "Two: ", total
|
||
|
|
||
|
|
||
|
when isMainModule:
|
||
|
partOne()
|
||
|
partTwo()
|
||
|
|