DEV Community 👩‍💻👨‍💻

Marco Servetto
Marco Servetto

Posted on

Advent of code Day 13

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)=\()((
  for line in ss().split( (
    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)
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)
  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)
  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(
  for line in ss().split( (
    xi = I(string=s())
    yi = I(string=s())
    m.set(row=yi col=xi val=1I)
  folds = Point.List()(for line in ss().split( (
    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())(
  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"#"
Enter fullscreen mode Exit fullscreen mode

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

Oldest comments (0)

We are hiring! Do you want to be our Senior Platform Engineer? Are you capable of chipping in across sysadmin, ops, and site reliability work, while supporting the open source stack that runs DEV and other communities?

This role might just be for you!

Apply now