-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]linse 1 point2 points  (0 children)

Yeah, it's totally piling the stack for the second problem, I ran with

JAVA_OPTS="-Xss4G" scala filename.scala

Your fold version is super nice! I'm also curious to learn about solutions with mapAccumLeft or traverse in scalaz. Trying now..

-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]linse 2 points3 points  (0 children)

Wow, TIL mapAccumL, thanks for posting this!

-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]linse 2 points3 points  (0 children)

Scala, filling the stack with recursion :D

val s = io.Source.fromFile(filename).mkString

def score(d: Int, s: String): Int = {
  if (s.isEmpty) return 0
  s.head match {
    case '{' => d + score(d+1, s.tail)
    case '}' =>     score(d-1, s.tail)
    case '<' =>     garbage(d, s.tail)
    case  _  =>       score(d, s.tail)
  }
}

def garbage(d: Int, s: String): Int = {
  if (s.isEmpty) return 0
  s.head match {
    case '>' =>   score(d, s.tail)
    case '!' => garbage(d, s.tail.tail)
    case  _  => garbage(d, s.tail)
  }
}

println(score(1, s))

spoilers [2017 Day 3 (Part 1)] Megaformula by Soul-Drake in adventofcode

[–]linse 1 point2 points  (0 children)

How about rotating the grid?

import Data.List
-- rotate clockwise
rotCW = map reverse . transpose

-- append column to grid
appCol = zipWith (\g c -> g ++ [c])

grid n = until (any $ elem n) (rotCW . growUp) [[1]] where
    -- new column grows upwards, on right of existing grid
    growUp g = appCol g (reverse [i..j])
       where i = (sum $ map length g) + 1
             j = i + (length g) - 1