DEV Community

Marco Servetto
Marco Servetto

Posted on

Advent of code Day 17

Nothing special to discuss this time.
I think I've done the dumbest exhaustive search ever, without even too much consideration to get good bounds.
The interesting bit is the formatting:
do you think this code is formatted ok? in 42 you can write in a very different style, and it is not clear how to format certain things.

reuse [L42.is/AdamsTowel]
Point = Data:{Num x, Num y}
Velocity = Data:{Num x, Num y}
Probe = Data:{
  var Point location = \(x=0Num,y=0Num)
  var Velocity velocity
  mut method Void step() = (
    (x,y) = \velocity
    \location(\location.with(x=\x+x).with(y=\y+y))
    \velocity(\velocity
      .with(x=0Num.max(x-1Num)).with(y=y-1Num))
    )
  }
Square = Data:{
  Num x1, Num x2, Num y1, Num y2
  method Bool #in1 (Point that)=
    that.x().isInRange(this.x1() to=this.x2()) 
    && that.y().isInRange(this.y1() to=this.y2())
  }

Main = (
  //input=S"target area: x=88..125, y=-157..-103"
  Square target=Square(
    x1=88Num x2=126Num y1=Num"-157" y2=Num"-102")
  result = 0Num.acc()(
    for x in Range(0I to=1000I),
    for y in Range(I"-300" to=1500I) (
      v = Velocity(x=\(x),y=\(y))
      p = Probe(velocity=v)
      res = 0Num.acc()(while !(p.location() in target) 
        ,,&& p.location().y()>=target.y1()
        ,,&& p.location().x()<=target.x2() ( 
        p.step() 
        \val(\val.max(p.location().y()))
        ))
      //Part1
      //if p.location() in target \val(\val.max(res))
      if p.location() in target \addOne()//Part2
      //\val(\val.max(res))
      )
    )
  Debug(result)//12246 and 3528
  )
Enter fullscreen mode Exit fullscreen mode

For example, consider this part:

res = 0Num.acc()(while !(p.location() in target) 
  ,,&& p.location().y()>=target.y1()
  ,,&& p.location().x()<=target.x2() ( 
  p.step() 
  \val(\val.max(p.location().y()))
  ))
Enter fullscreen mode Exit fullscreen mode

In 42 the comma (,) is just a spacing character like space and new line, so we can use it as often as we want. In practice, it can be used to distinguish different kinds of indentation.
Here I'm using it to differentiate the indentation of the long while condition and the indentation of the while body.
This is further complicated by the fact that the while is the argument of the #apply method of Num.Acc.

Ideas?

Discussion (0)