From f9cc8c2824e073cd40d95f890aa14aedb0ea81f3 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Thu, 14 Dec 2023 16:12:09 +0100 Subject: [PATCH] Add day 14 --- Day14.scala | 69 +++++++++++++++++++++++++++++++ inputs/day14.input | 100 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 Day14.scala create mode 100644 inputs/day14.input diff --git a/Day14.scala b/Day14.scala new file mode 100644 index 0000000..bb44e76 --- /dev/null +++ b/Day14.scala @@ -0,0 +1,69 @@ +package aoc.day14 + +import aoc._ +import scala.collection.mutable + +type Board = Seq[Seq[Char]] + +val board = lines.map(_.toSeq).toSeq + +enum Direction { case Up, Down, Left, Right } +import Direction._ + +extension (board: Board) + inline def doTiltLeft(inline before: Board => Board, inline after: Board => Board): Board = + after(before(board).map { col => + col + .splitBy(_ == '#') // split by tilted rocks + .map { group => + val (rocks, empty) = group.partition(_ == 'O') + (rocks ++ empty).mkString + } + .mkString("#") + .toSeq + }) + + def doTilt(direction: Direction): Board = direction match + case Up => doTiltLeft(_.transpose, _.transpose) + case Down => doTiltLeft(_.transpose.map(_.reverse), _.map(_.reverse).transpose) + case Left => doTiltLeft(identity, identity) + case Right => doTiltLeft(_.map(_.reverse), _.map(_.reverse)) + + def cycle = Seq(Up, Left, Down, Right).foldLeft(board)(_.doTilt(_)) + + def load = + board.reverse // rows in reverse + .zipWithIndex.map { case (row, idx) => row.count(_ == 'O') * (idx + 1L) }.sum + +// part 1 + +def part1 = + println(board.doTilt(Up).load) + +// part 2 + +object Cycling: + private val memo = mutable.Map.empty[(Board, Int), Board] + private def getPower(b: Board, p: Int): Board = // b.cycle 2^p times + memo.getOrElseUpdate( + (b, p), { + if p == 0 then b.cycle + else getPower(getPower(b, p - 1), p - 1) + } + ) + + def apply(b: Board, times: Long): Board = + @scala.annotation.tailrec + def loop(b: Board, i: Int): Board = + val pow = 1L << i + if pow > times then b + else if (pow & times) > 0 then loop(getPower(b, i), i + 1) + else loop(b, i + 1) + loop(b, 0) + +def part2 = + println(Cycling(board, 1_000_000_000).load) + +@main def Day14(part: Int) = part match + case 1 => part1 + case 2 => part2 diff --git a/inputs/day14.input b/inputs/day14.input new file mode 100644 index 0000000..5b4e57e --- /dev/null +++ b/inputs/day14.input @@ -0,0 +1,100 @@ +......O........#....O..O.O#O.O.##.O#.O##.O#......O........O....O.#..OO#.OO.#..#.........#.O..#O..#.O +O#..O...O....O.#...##.O....O..O.#...#...O..O#.#.#......#.O##O#....#..O...O..#.#......#.#O..#..#....# +O....##.O#O##.O...#...#.#.......OO.##..###.OO..O.#.#.OO#O..##..O.O.#...#..O.#....O.##.O#OO.......... +....O#.O..OO.#..OOOO#.......O.....O.#O......OOO#....O.....O......O.....#..OOO.....O...O.O##OO.....## +OO#..#.O.##..##..#........O..OO.......#..O.OO.OO..#O...OO.##...#.OOO...O.O.O..#.O..#..O.O.##.O...OOO +#.....#OO.O......O..####O...O.....O.......#..#OO#..#...O...OOO.#....O#.....O...........#O#OO...O..O. +#O.O......O...O.O...O##.O#..OO....O..##.OO#O.##O.#.#.......O.O#.....OOOO......#..OO###OO.......O..#. +#.O.OO...#...##.O..OO..#.......O.O.O.O......O#....##.OOO.O.....#O#..##...OO.......#OOO.O..O......##. +..O.OO.O.#O.O........#..##.#.OO.O..##.O...O.#.....#..O.#....O..#O..O.#OO......O........#.#....#O..#. +..O......OO.O............OO#....##....#......O.#..O..O...O.O..#O......O.OO#.#.......O....#....##.O.# +.....#.O#O.#..#.#.##....#O....O.O.O.OO......#..O.O..#..OOOO....#.#..O.#.O.O..O.O#.O...O#..#..O##.O.. +O..O#.OO.#.##.#.........OO.#.....#OO...........O....O..OO..O.O###..O.O.#O...O.O...#...O..O#O...OO... +.#.#...OO...#.O...O.........#OO..#.....O....##.#.OO.#.#.#OOO...#OOO.O.O..O#......#.##..........O.... +....#..#...#...#.......#..#......##..O........O.OO.#O..#O.#.......#..#..O.O#.#...O#..O...O#.O...O.#O +.#O#O.O..........#O##.O#.#.........##.#............#.#.....#..#OO...#O....#OO.O.OO.#..#...O#........ +.#..#.O#O...##O....O.....##...OOO#...#O..#..O##O..#..OO.#...#O.....#.O...O#.O#.OO...###.#..O#O..OO.. +...#...O....O.#....#..#.O#OO...O.O#.#....O..#OO.#.O....#O#...O...O..O.##O..#.......O.##....O##....O. +#...O...O...O#O##O.O.#.#.....#.O...#.....##..#.#OO....OO#.......#..O..O.#.#O...#.#.#.....OO.O...OO.. +.O...OO.O.....##.O.##.#O.....#O..O.###..#..##.O.##O....OO..#O#....O.....O...#O#O...OO.........O..##O +.O..OO.#O.#...O...O...#...O....#.O.O.O....#O.###O.#..#..##OO.O...O.#.#.#..O...#.....O.#.O.O.#..O.... +O...OO.#...O.#..#.O..O#..O#....##.#O.##...#......#O#O..O..#.##.....O...#O#..O.O...OO...OO..#.OO.O.OO +.O.O....O.O.##O#....##..##O......#.#O#..O..#...OO..O...O.#..#O#..#..#.O...#.O.OO....O#..O##.O..O..O. +....#..O..O.OO.#.O...#OOOOO.OO.O#..O....#.#O.##........#OO.#.#.O.#.OO#...#OO..#.OO...O..#.O.....O#.# +.#O...O......#OOO#..#..#....O...O..O.#O....#O#...O..#...OO.#.O.O...O..#..#..O..##.O##.....O.O..OO..# +..#O.##.#.......OOOO.OO.....O..O........##.O.OO....O###..#.O...#O.O.#OO.#....O.O.#..........#.OO.... +.##...##.#......O.#.OO#...#..O##..#..##.O.......O#..O.O.#.O..#.....#..O#....O....OO.OO.O..#...O#..O. +..##O##O......O.#O.O.....#..#.#O#.O..#O#O..#OO.#..#.#.....##O.................O.....O#OO...##...O..O +....#O#O..O.#..O....#.....#.....#......O.#O........O#..O.O#..OO...#.#.O..##....O.....#......O#....#. +#.....#...O....OO..O##.OO....O.#..#O.........O..............O....##OO##.....#.##.O...#..O..#O#OO...O +...O.#O#...O..OO#...O.#O#.......#..O...O#.#..#O...#.O.O....O##...##...#.OO...O.O..OO#.#..........##. +.......#OO....#.#O#..........O.##.O.O#.....O.OO.O.OO##...##O...OO.O..O#.OO...O......O#O..O.O.##...OO +..O.O#.#.....OO............O.O..#.O#.O.O.#.##.#O.O...#.O.....O..#.#....O..#O#O....O.O.#..OOO..O..O.. +..O.#.O.............#OO#.#O....#O#...OO..O##...#.....O...............#..#..O..O#....O..##...O..O.O#. +...........O.........##O#.#.O.O..O##........#..#....#O..##O..##.OO...O#.##..#..#.#O....O.O.#........ +...............OOO.#........O....#.#O..##.O...O.O#.......#.#O.#...#O.......###.......O.OO...O.O..O.# +.#.O#....#......O...OO##..O#O...O.O#.O.##.OO...OOOO....OO....#.#....##.###.O.O..O..##....#.......... +..OO.....OO#O.........O.#..O....#......#.#.#.#...##O.OO#.O......O..O.#O....O#...OOO....O..O.O..#..#O +#O...O.O.......##..#..O..O...O...O..O.#..#.#.O.OO............OO....#OOOO#...OO.#.O#OO....O##.O....#. +.O#..OOO.O.#.......O....#......O.OO.#......#..#.O.......###.#....#O..O....O#.O.....#O#....O#.#.....# +....OOO#.#..#.O..O....#...O.#...OO....#..OO.O#O..O....O.O...O...O#O..O..O......O.#O..O#O..........O# +..O..#.OO###....#..O....#.O..#.....O.O.#..OOO.O...##...O#..##..O#.O.......O.#.###...O.....O......O.. +..O##..OO..O.O...O.#....OO.OOO.O........O......#.O..O..........O...O#OOO..O....O.#..O##O.......#.... +O.O..OO##.#...#O#..#...O..#.......#O..###..OOO..O..#.O#.O..#....O.OO#.##..##O.......OO#OO..#.O....#O +...#..O.#........OO.O....OOO#.O..O#O.......O....O........#O#...#O#.#..OO..O....#O.O....O...O###.#... +O..#.......#..#.....O..OOO.#..#OO...#O.O...O.....##O.O.#O....#.....#...#.........OO.O....#...O#O#... +.O..O.#O....O#O.###....#O.O.....................###......O..#...O.OO.....#.......O..#...OO.......##. +............#O#..#.#.......#.##....#O.#....O.#...O...OOO.O.....#O..####O...O..#..#.O##....#OO##.#.#. +....O.#..#.....#...#OO#.##...O..OO....#....O#..O......OO.OO.#OO....#.O..#.....#.##..#.O...O.O#.##O.. +...#OO#.O.O......#...O.O.....#......#.......................#.#.....O..O.#OO.#..OOO.OO.O.......#..#. +O#..#OO.OO.O#....##.O.#....OO..#.#.....O...O...#.O...O...O..#..O.....O..OO..O...O...O#....#...O.O... +.......OO.#O..#O#OO.#.............#....O.O#..#..#.....#..O.O#..#..##O.O.O...O.O.#O..OO..#O...OO...#. +#..#O.O.O.....O..O#.#............##O#....O.O#...#...............#OO..O.....#...OOO#.......OO...#.... +..#...O...OO..#...O#O.O.O.O....O..#O#O......#.#....#..OO....#O...OO.O..O#.#O#OO.#...O.O.O..#....##.O +..#..O#..##.O...O..O....O.O.#.....#.O.OO..#.#.#...OO#....#..#O........OO.OO#.#..#..##O...O.OO.O#.#.. +...#O..OO.O#OO..O#O#O....#O....#.O#.OO.##.......O.O.OO##.......#.O.O.#O.##.......O..O....##.#O..#..# +#....##...O.......#..#OO.........#O.O.O.O#...O......O.......OO##....O....#.#O.#.#.....O.OO..#...O.O. +.O..O#.....O.#..O..#..#O..O.O...O..O.#....#.....#.#.O...O..#.O.O.O....O....O.O..O#O.....O.O....O...O +.......O...O.O....O.O.OO...##..OOOOOO.O...O.##.O...#...O.O#O...#.O.O.O.........O..#..O.....##.#....# +...O..........#.O...O.O.OOO...##.....#OO.......O....#.....O.......#.#O.........O.##O#..##...O.O.#.O. +..OO#O.....O.##......O..#...#..OO.....#O#.O..#.O..#.O.#......O.#O.#..O.#.#.O..#.O......#.....O..#.O. +..O#...O..O.O.OO....#..O.......#..#.O.#....O..OO#.OO.O..#.#O..O.#.......O...O#.OO.......O#........O# +...OO....#..O...O.#....#.#OOO...O.#.#.#.O....OO...OO...#......#O..#..O.#O....#O...#..O..OO#....OO..O +.O.OO##....OO.#...O.......O...O.O.O.O.O.....O.O#..O#O......##O..#..###.O....O...O..#O.##O#....#..##. +.#..O.O....O#O..OO#........#O#.OOO...##.....O#..#.#..O#.OOO.O...#.....O#........O#O......##...O..... +..O#O.....O.#O...O...#.O.........#OO..#O.O.##..##O..OO....O...O.#.#...#..O...O#.O...O......#OO...#.# +O.........##.....###O.O.#.O##..##.O.#.O.OO......OOO...O#.O#..OO..........##.##O.O#O.O...##.O....O..O +O..##OO.OO.......#......O.#..#.....O...O#.O#..#...OO...#.O...#O.#.O..O..OOO#....O..O...#....O...#.O. +..#.....#OOO#..O..O.....O........#...O..OO.O.....OO....O.###.....#O.#..O..#...#.###O..O#...O.#...O.. +O.O..#O..###.#....O#......###..#....O..OOO..O..O..OO..OO#O.....O..#..O##.#.#..OO..##..#.....O....O.. +..O......O..#.......##..#O..OO.O.O....#..OOO..OO.#..#..OO.O...O.....#.OO#OOO.O.##.....O...O##.#...O. +O.OO.#O..#.#...#...##...O.#O....O##O...O##.OO#.O.###.#OO..OO#O......OOO..#OOOO...OO....#.O#......... +.#O#O#O.O.O....O.O.O...###O.O#.#OO#.O......#O#.O#O#........O...#OOO..OOO.........O...#O#...O......#. +.OO.O.#..#.#..O.....OO.O.O#.....O...O..O.#.OO.O.....#.....O.......O.OO......O...OO..O..O....#..O.#.O +.#...O#..O..#.#O...#O...O..O.....#.#.O#.OO#.#..#.....OO.#....O.#OO.....OO.O.O..O.O....#......O..OO.. +...OO.##.O...#.#....##O.O..#.............#.#...#.#.#.OOO.##.......O#.O.O...OO....O..#....#.....O...O +.O...OO............#....O.#OO.#....#O....O###.#.....O.O.......#.O....#O...O...O.O.#..OO....#....#... +#.#......#O.....O..#.O.OO.........O...##....O....#..##OO..##O#..O.O......O....#.O..O......#..##....O +OOO....#.#.O.#.O#..#.....#O.O.O..O.OO.......#O.#......O##.O.#.#...#.#..OO#O.O...O..O..O.O.....#..#O. +##..O##..O...#.#..#..............#O..#O.O.O..#..O....O...#..#OO...O##....O...O...#O....OO...#.O..O.. +.#...OO#.#O.......##..#O...O.#..#......O#O....#..OO..OO.#O..O...O.#.O.........O........#.##.##...O.# +...O.O..O...#O..O.O..OO.OO##O.O.O.#..#...O..##...O##O#O..#.#.O#.#O#..#..OO..O....O..OOOO..##..O..... +##........#............#....O.....O.#.O.#.O.O.#O...#O......O.#....O...#O#...OO#.#O##........O....... +OOO#O.OO..#.......OO.OO...O...##.O.##..O....OO...O.O.............##O......#.#..O..#O..##..#....#...O +.#O.......#O...O..#O......O#OO.O.O#.O......#O#......#..###.O.#.#..O#.....##.O.O...O..O.O.#..OO#.#.#O +.....O.O...O.#.....OO###..##.O.....O....#...O.O...........#O#..#O..#....#.O.......###...#..O.#....#O +#O.OO.........O#..#O.....O....O#......#....O..OOO..O.#.OO#...O..O##....##O..#..O#..O......O.OOO.O.OO +.#...O.#.#O....O..OO....#O..#...#O........#....O...O.....O..O..O.##.......O#...#..#O..#.#...O..O.O.O +......O..O...#....O....#OO#....#.#..##OO.....O.O.#......OO.O#..........#..O..#.O##.........###..O... +.O.O#..#O.#..#....O......O..OO.#O.O....O.#......#O.O.....O..OO#..O#.O.#....#...O.O...#O..#......##.O +......O.....O.#...#....#....O#O.##...O.......O.#.##.###O#.#........O.#O.O..#...#...#.O.....O.O.O#... +#.#O#.#..#.#.......#..#..#.....##O#.......#.#..##OO.........O.O....#O...O....O....#..##..#OO..O...OO +.O..#.O.O.......O....#.O........O...#O.#.....O#....#.......O.O...O...O#..##....O...#.OOO...O.#O##... +.......#..O..O...O....O#...O#....#O..O....##..#....##O....OO..#......#....O..#O.....O.O#OO.......... +..O#......O.O......#.....#O.#.#.#.O...#OO.....O.O#.....O#.O.#.....OO.O.O.O....O..OO..##.#.....OO#..O +#O...O.#..#...OOO#...O......O#..O..O....###....OO..O#.#O..##.O#.O##....#.#.O#.##.....O.............O +.O.##.........#O....#..O#.O.#...O......O#.O#.OO#...O.O..........##.##O....O.#O#O..O.#...OO.O#.O.O... +#OO.O.OO.....O#....O.OO..###...O.....#..O##...O..###.O...#.O..#.O...O...OOO.#.O.#..O...O#..OO.#O...O +.###..#.OO#O..O.#..#.O..O...#....#.#..#O.O....#O....O#..#......#...#..O.....O.O.#O...O..O.....O#..#. +..###....OOOO......#O....O.....O.O.O..O.#O..##...#O#..#..#....#.O..#..O.......O...#.O#.#..#...O....# +......#O.#.......O..OO#O#....#.....OO#..#.OO..O.#....#...#O....#...O....O#.......O..#.O.#...#.#.O...