This commit is contained in:
Natsu Kagami 2023-12-11 13:44:38 +01:00
parent 30b5d266f2
commit 506e288f61
Signed by: nki
GPG key ID: 55A032EB38B49ADB
2 changed files with 174 additions and 0 deletions

34
Day11.scala Normal file
View file

@ -0,0 +1,34 @@
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

140
inputs/day11.input Normal file
View file

@ -0,0 +1,140 @@
........................#...............................................#................................#................................#.
....#..............#......................#............#....................................................................#...............
................................#.............................................................#.............................................
.........#.....................................#...................#........................................................................
....................................................#.......................#..............................#................................
..#................................................................................................#................#......................#
.........................................................#.........................#.....#..................................................
..................................#.............................#...............................................#.............#.............
.......#.....#...........................#..............................................................#.............................#.....
......................#...............................#................................................................#....................
............................#.................#........................#.....................................#..............................
....#.................................#...............................................#.....................................................
...............#...........................................................#............................................................#...
..................................#...........................................................#...................#...........#.............
....................................................................................................................................#.......
................................................................#................#..........................#...............................
...#..............#........#.........#.............#........................................................................................
.........................................................#.............#.............#..............#...................#...................
........#...................................................................................................................................
..............................................................................................#.................................#........#..
....................#.......................................#.....................................................#.........................
#..............................#.......#......#...................................#......#..................................................
...........#.......................................#......................................................................#.................
.......................................................................................................................................#....
............................................................................................................................................
.........................#...................................#...............................#..............................................
..................................#.......................................................................#.............#...................
.........................................#...........................#...............................#.........#..........................#.
............#......#............................................#...............................#...................................#.......
.............................#.....................................................#........................................................
....#........................................#...............................#.................................................#............
......................#............................#.....#.........#........................................................................
.........................................................................................................#..................................
.......#........................#................................................#..........................................................
...............................................#..........................#................#..........................................#.....
.#................#.............................................................................................#......#....................
..............................................................................................................................#.............
..........................#........................#.............................................#..........................................
.............#..........................#............................................#..............................#.......................
........#..............................................#.....................#..........................#..................#................
............................................................#.....#..................................................................#......
....................#.........................#..........................#..................#...................#...........................
..............................#...................................................#................#........................................
.........................................#..............................................#..................................................#
.#........#.........................................#.....................................................#.....................#...........
.........................................................#.........................................................#........................
......#...........................#.........................................................................................................
................................................#........................................................................................#..
...................................................................#...............#........................................................
.....................#................................#..............................................................................#......
...........#................................................................................#.....#.........................................
....#........................#............................................#..............................................#..................
................#.......#........................................................#........................................................#.
#...................................................................................................................#.......................
....................#...................#..............#.................................#...................#..............................
.............#....................#.................................................#..................#...................#................
.................................................................................................................#.....................#....
................................................................................#...........................................................
.......................#.............#....................#.................................................................................
.......#.............................................................................................#..............#.......................
...................#.....................................................................................................................#..
......................................................#..................#.......................................................#..........
..........................#..................................................................#..............................................
............#.................................#.........................................#...................#............#..................
.....................#......................................#......................................................#..........#.............
.#.........................................................................#......#...................#.....................................
............................................................................................................................................
................................#...........................................................#...............................................
........................................#..........................#..................................................#.................#...
....#........................................#.....................................................#.............................#..........
..........................................................#.................................................................................
...............#............................................................................................................................
..........#............#............................#.............................#...........................................#......#......
....................................#........................................................#...............#..............................
...........................................#..............................#...............................................................#.
.#...........................#.......................................................................#......................................
.........................................................#...........#....................................#.................................
......#.......#...................#...........................#.......................#.........#..................#........................
............................................................................#.........................................................#.....
.....................#.........................................................................................#........#...................
..........................#.............................................#................#........................................#.........
............................................................................................................................................
....#.....#.....#..................................#.........#.....................#....................#...................................
........................................#.....................................................................................#.............
...............................................................................................#............................................
.......#........................#...........................................#............................................................#..
....................#......#....................#...........................................................................................
.........................................................#...........#..........#.............................#...........#.................
......................................................................................#.....................................................
....................................................................................................#................#..........#.....#.....
............#.........#.....................#...............#..............................................#................................
.......#............................#.......................................................................................................
.......................................................................#......................#................#...................#........
............................................................................................................................................
........................................................................................................................#...................
..........#..........#.......................#.........................................................................................#....
...................................................................#.................#...........#..........................................
........................................#................#..................................................................................
................#....................................................................................................#......................
............................................................................................................................................
.......#....................#.................................................................#...............#.........................#...
.................................#...........#.....#...........................................................................#............
...........#.......................................................#....................#...................................................
........................................#...............................................................................#...................
............................................................................................................................................
.....................................................................................................#......................................
................#.........................................#..................#................................................#.......#.....
....#................................#.............#...................................#.....................#.......#......................
.........................#......................................#...........................................................................
...........................................................................................#.......#........................................
..........#.....................#..............#........................#...................................................................
..#.........................................................................................................................................
....................#.....................................#.......................#.........................................................
..........................#.............#...............................................................................#.........#.........
......................................................................................#.....................................................
.....................................................................#..........................#...........................................
.......#.........#...........................................................#.......................#........#............#............#...
........................#....................................#..............................................................................
.......................................#............................................#....................#.........................#........
..............................................#....................................................................#........................
..............#..................#.....................................#..........................#.........................................
..........................................#..........#..........................#...........................................................
.....#......................................................................................#................#..............................
.....................#................................................................#.......................................#.............
.....................................................................#......................................................................
...........#.....#............................#.....................................................................................#.......
................................#.....#..........................#........#..........................................#....................#.
...........................#.................................................................#........#.......#.............................
#...........................................................................................................................................
........#........................................#...................................#......................................................
.............................................................................................................................#..............
.................................#..............................#........................#.........#........#...............................
............#.....#..........................#.......................#....................................................................#.
........................#........................................................#..........................................................
............................................................#..............#......................................#.........................
...#........................................................................................#...............................................
...........................................................................................................#......................#.........
..........#.......................................................................................#.........................................
......................#................#............................................#.................................................#.....
...........................#.....................#........#...................................................#.............................