lluque

AoC day 18

Dec 21st, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.62 KB | None | 0 0
  1. import scala.annotation.tailrec
  2.  
  3. object day18 extends App {
  4.  
  5.   def nextRow(prev: String): String = {
  6.     ('.' +: prev :+ '.').sliding(3).map(c => if (c.head != c.last) '^' else '.').mkString
  7.   }
  8.  
  9.   def safeTiles(n: Int): Int = {
  10.     @tailrec
  11.     def countUp(row: String, acc: (Int, Int)): Int = acc match {
  12.       case (x, y) if x == n => y
  13.       case (x, y) => countUp(nextRow(row), (x + 1, y + row.count(_ == '.')))
  14.     }
  15.  
  16.     val row1 = "......^.^^.....^^^^^^^^^...^.^..^^.^^^..^.^..^.^^^.^^^^..^^.^.^.....^^^^^..^..^^^..^^.^.^..^^..^^^.."
  17.     countUp(row1, (0, 0))
  18.   }
  19.  
  20.   safeTiles(40)
  21.   safeTiles(400000)
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment