Marco Servetto

Posted on

# Advent of code, last puzzle

And this is the solution for the last day of advent of code 2021

Cell = Collection.Enum:{
Right={},Down={},Empty={}
}
Map = (
var I col = 0I
var I row = 0I
for s in input.split(S.nl()) (row+=1I)
for s in input.split(S.nl())().split() (col+=1I)
Collection.matrix(Cell.List,row=row,col=col)
)//row=137, col=139

Right = {class method Map.Coord (Map.Coord that) =
that.with(col=(\col+1I).mod(139I))
}
Down = {class method Map.Coord (Map.Coord that)=
that.with(row=(\row+1I).mod(137I))
}

StepRight = {class method Map (Map map) =(
res = Map(\()(for v in map \add(v)))
for c in map.coords(), v in map if v==Cell.Right() (
cr = Right(c)
go = map.val(cr)==Cell.Empty()
if go (
res.set(cr val=v)
res.set(c val=Cell.Empty())
)
)
res
)}
StepDown = {class method Map (Map map) =(
res = Map(\()(for v in map \add(v)))
for c in map.coords(), v in map if v==Cell.Down() (
cd = Down(c)
go = map.val(cd)==Cell.Empty()
if go (
res.set(cd val=v)
res.set(c val=Cell.Empty())
)
)
res
)}

Main=(
var imm map = Map(\()(
for s in input.split(S.nl()) for c in s.split()(
)))
res = 0I.acc()(while Bool.true() (
new = StepDown(map=StepRight(map=map))
if new==map ( Break() )
map:=new
v=\val