Advent of code, last puzzle

marcoservetto

Marco Servetto

Posted on December 25, 2021

Advent of code, last puzzle

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

reuse [L42.is/AdamsTowel]
Fs = Load:{reuse[L42.is/FileSystem]}

Cell = Collection.Enum:{
  Right={},Down={},Empty={}
  }
Map = (
  input = Fs.Real.#$of().read(\"input")
  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=(
  input = Fs.Real.#$of().read(\"input")
  var imm map = Map(\()(
    for s in input.split(S.nl()) for c in s.split()(
      if c==S"." ( \add(Cell.Empty()) )
      if c==S">" ( \add(Cell.Right()) )
      if c==S"v" ( \add(Cell.Down()) )
    )))
  res = 0I.acc()(while Bool.true() (
    new = StepDown(map=StepRight(map=map))
    if new==map ( Break() )
    map:=new
    v=\val
    \addOne
    ))
  Debug(res)
Enter fullscreen mode Exit fullscreen mode

Overall, this adventure guided me to try many different kinds of algorithms in 42, and by doing so I discover a bunch of bugs and missing features in AdamsTowel.

Still, this programming style is very far from the ultimate goal of 42: Enforcing modular security.
This is where 42 shines, but this mostly emerges when the program is large enough that can not be reasonably done by a single programmer and where long term maintenance is kept into account.

I will be back to Dev.to after the new year break to talk more about the details of 42 and on how it enforces modular security.

Marco!

💖 💪 🙅 🚩
marcoservetto
Marco Servetto

Posted on December 25, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of code Day 11
adventofcode Advent of code Day 11

December 11, 2021

Advent of code, last puzzle
adventofcode Advent of code, last puzzle

December 25, 2021

Advent of code Day 17
adventofcode Advent of code Day 17

December 17, 2021

Advent of code day 16
adventofcode Advent of code day 16

December 17, 2021

Advent of code  Day 9
adventofcode Advent of code Day 9

December 9, 2021