~repos /plum

#treesitter#compiler#wasm

git clone https://pyrossh.dev/repos/plum.git

A statically typed, imperative programming language inspired by rust, python



test/aoc_2020_2.plum



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