DEV Community

Marco Servetto
Marco Servetto

Posted on

Advent of code, last puzzle

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

reuse []
Fs = Load:{reuse[]}

Cell = Collection.Enum:{
Map = (
  input = Fs.Real.#$of().read(\"input")
  var I col = 0I
  var I row = 0I
  for s in input.split( (row+=1I)
  for s in input.split( (col+=1I)
  )//row=137, col=139

Right = {class method Map.Coord (Map.Coord that) = 
Down = {class method Map.Coord (Map.Coord that)=

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())
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())

  input = Fs.Real.#$of().read(\"input")
  var imm map = Map(\()(
    for s in input.split( 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() )
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 after the new year break to talk more about the details of 42 and on how it enforces modular security.


Top comments (0)