Advent of code Day 13
Tags: adventofcode, adventofcode2021, programming, 42
Published: 2021-12-13 20:53:46

Code of today.

I'm getting to use the ultimate power of the '\' more and more.
It really look and feels like java streams, but no lambdas to be seen!
A side effect seams to be that I tend to close a lot of lines with a pile of parenthesis, since there is only one natural indentation level.
Look for example to the Folds function!
```
Point = Data.AddList:Data:{I x, I y}
Folds = {class method Point.List (S that)=\()((
  ss=that.split(S.nl()++S.nl()) 
  _=ss()
  for line in ss().split(S.nl()) (
    isY = line.startsWith(S"fold along y=")
    if  isY \add(\(x=0I,y=ReadFold(line)))
    else    \add(\(x=ReadFold(line),y=0I))
  )))}
Max={class method Point (Point.List that)=(
  var maxX = 0I
  var maxY = 0I
  for (x,y) in that (
    if x>maxX (maxX:=x)
    if y>maxY (maxY:=y)
    )
  Point(x=(maxX*2I)+1I,y=(maxY*2I)+1I)
  )}
ReadFold ={class method I(S that)=(
  (size) = S"fold along y="
  I(string=that.subString(size to=\size))
  )}
Matrix = (
  input = Fs.Real.#$of().read(\"input")
  ps = Folds(input)
  (x,y) = Max(ps)
  Collection.matrix(I.List,row=y, col=x)
  )
Fold={
  class method Void (mut Matrix m, Point range, Point fold)=(
    (x,y) = fold
    if x==0I \y(m=m,range=range,fold=y)
    else     \x(m=m,range=range,fold=x)
    )
  class method Void x(mut Matrix m, Point range, I fold)=(
    for xi in Range(fold),
        xj in Range(fold+1I,to=(fold*2I)+1I).reverse() (
      for y in Range(range.y()) (
        new = m.val(row=y col=xi)+m.val(row=y col=xj)
        m.set(row=y col=xi val=new)
        m.set(row=y col=xj val=0I)
        )
      )
    )
  class method Void y(mut Matrix m, Point range, I fold)=(
    for yi in Range(fold),
        yj in Range(fold+1I,to=(fold*2I)+1I).reverse() (
      for x in Range(range.x()) (
        new = m.val(row=yi col=x)+m.val(row=yj col=x)
        m.set(row=yi col=x val=new)
        m.set(row=yj col=x val=0I)
        )
      )
    )
  }
Main=(
  input = Fs.Real.#$of().read(\"input")
  ps = Folds(input)
  range = Max(ps)
  (x,y) = range
  m = Matrix(\()(
    for xi in Range(x), for yi in Range(y) \add(0I)
    ))
  ss = input.split(S.nl()++S.nl())
  for line in ss().split(S.nl()) (
    s=line.split(S",")
    xi = I(string=s())
    yi = I(string=s())
    m.set(row=yi col=xi val=1I)
    )
  folds = Point.List()(for line in ss().split(S.nl()) (
    isY = line.startsWith(S"fold along y=")
    if  isY \add(\(x=0I,y=ReadFold(line)))
    else    \add(\(x=ReadFold(line),y=0I))
    ))
  for f in folds i in Range(folds.size())(
    Fold(m=m,range=range,fold=f)
    )
  for yi in Range(7I)(//7 and 41 are just reasonable sizes :-/
    Debug(S"".builder()(for xi in Range(41I) \add(
      if m.val(row=yi,col=xi)==0I S" " else S"#"
      )))//CEJKLUGJ
    )
  )
```
What have I learnt?
-Matrix need to have class methods giving you the max range!
-Matrix need to have a functionality to be initialized with a single value in all cells.
-There is no easy way to print a char without going new line, so in the solution I had to accumulate on a full line string before printing... is this ok? should I have Debug.print(..) to not go new line?


You can find the 42 tutorial in video form at
(https://www.youtube.com/watch?v=Xsz5yxNR_Vo&list=PLWsQqjANQic8c5wG3LfSe-mMiBKfOtBFJ)