aoc2023/Day11.scala

35 lines
1.1 KiB
Scala
Raw Normal View History

2023-12-11 12:44:38 +00:00
package aoc.day11
import aoc._
import scala.annotation.tailrec
val board = lines.toSeq
// part 1
val countByRow = board.map(_.count(_ == '#')).toList
val countByCol = (0 until board(0).length()).map(col => board.count(row => row(col) == '#')).toList
@tailrec def totalDist(expandBy: Int)(count: Long, leftDist: Long, accum: Long)(counts: List[Int]): Long =
counts match
case Nil => accum
case 0 :: next => totalDist(expandBy)(count, leftDist + expandBy * count /* expanded case */, accum)(next)
case cur :: next =>
totalDist(expandBy)(count + cur, leftDist + count + cur, accum + cur * leftDist)(next)
def totalDist(expandBy: Int)(counts: List[Int]): Long = totalDist(expandBy)(0, 0, 0)(counts)
def part1 =
inline def count(r: List[Int]): Long = totalDist(2)(r)
val res = count(countByRow) + count(countByCol)
println(res)
def part2 =
inline def count(r: List[Int]): Long = totalDist(1_000_000)(r)
val res = count(countByRow) + count(countByCol)
println(res)
@main def Day11(part: Int) = part match
case 1 => part1
case 2 => part2