Files
adventofcode/src/pladcl/2024/day11/part1.pdl

135 lines
2.0 KiB
Plaintext

state start
return_if(`ln` < '0' or `ln` > '9')
read_number()
`dnSN`
`[ ]n`
`0SD`
blink()
`lo+so`
end
interrupt program_start
# C: cache
`0sc`
end
interrupt program_end
`10an`
`lop`
end
function print_array
for '?' in `l0` to `lc` do
array_get('X', `l?`)
`n[ ]n`
end
`10an`
end
function blink
# N: number
# D: depth
# if we hit max depth
if `lD` == 25 then
`LNstLDst`
return 1
end
# check cache
if `lN` < 1000000 then
if array_get('C', `lNlD1000000*+`) > 0 then
return array_get('C', `LNLD1000000*+`)
end
end
# 0 case -- check 1 rock
if `lN` == 0 then
`1SN`
`lD1+SD`
blink()
end
# even digit case -- check split rock
if `lNZ2%` == 0 then
`lN`
`10lNZ2/^`
`~` # get top and bottom
`SNSN`
`lD1+dSDSD`
blink()
blink()
`+`
end
# default case
if `lNZ2%` != 0 and `lN` != 0 then
`lN2024*SN`
`lD1+SD`
blink()
end
`s.`
# cacheable
if `lN` < 1000000 then
array_set('C', `lNlD1000000*+`, `l.`)
end
`LNstLDst` # clear params
return `l.`
end
function read_number
read_number_start()
read_number_end()
end
function read_number_start
-1
# ret _1 if not a digit
return_if(`ln` < 48)
return_if(`ln` > 57)
`st`
# throw the true digit onto the stack
`ln48-`
# inc i
adjust_index(1)
# recurse
read_number_start()
end
function read_number_end
# if we only have 1 item on stack (-1) we found no number, so return
return_if(1 == `z`)
# push entire number thing onto temporary stack
`[STz0<.]ds.x`
# multiply ten then pop and add repeatedly until done
`0`
`[LTd_1=qr10*+l.x]ds.x`
# get rid of the extra -1
`st`
# decrement i so we reread the skipped char
adjust_index(-1)
end