| READ_MIN_OCCURANCES(Step(0))
| READ_MAX_OCCURANCES(Step(1))
| READ_CHAR_TO_COUNT(Step(2))
| COUNT_OCCURANCES(Step(3))
| READ_MIN_OCCURANCES => 0
| READ_MAX_OCCURANCES => 1
| READ_CHAR_TO_COUNT => 2
type PasswordCheckState =
initialCheckState() -> PasswordCheckState =
step: READ_MIN_OCCURANCES,
input = fs.readFile!("./examples/test/demos/aoc2020/2.txt")
valid_passwords_count = 0
state = initialCheckState()
if state.step == READ_MIN_OCCURANCES
state.step = READ_MAX_OCCURANCES
state.min_occurances *= 10
state.min_occurances += (c - '0') as Int
else if state.step == READ_MAX_OCCURANCES
state.step = READ_CHAR_TO_COUNT
state.max_occurances *= 10
state.max_occurances += (c - '0') as Int
else if state.step == READ_CHAR_TO_COUNT
# skip `${c}: `, (+1 will be skipped by loop)
state.step = COUNT_OCCURANCES
else if state.step == COUNT_OCCURANCES
if state.current_occurances >= state.min_occurances && state.current_occurances <= state.max_occurances
valid_passwords_count += 1
state = initialCheckState()
if c == state.char_to_count
state.current_occurances += 1
writeLn(valid_passwords_count)