From f82f64f013d54933fb34cc556fcbe1fde8010e83 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Thu, 14 Dec 2023 09:02:13 +0200 Subject: [PATCH] Solve 2023 day 14 part 1 --- .../eu/sim642/adventofcode2023/day14.txt | 100 ++++++++++++++++++ .../eu/sim642/adventofcode2023/Day14.scala | 98 +++++++++++++++++ .../sim642/adventofcode2023/Day14Test.scala | 27 +++++ 3 files changed, 225 insertions(+) create mode 100644 src/main/resources/eu/sim642/adventofcode2023/day14.txt create mode 100644 src/main/scala/eu/sim642/adventofcode2023/Day14.scala create mode 100644 src/test/scala/eu/sim642/adventofcode2023/Day14Test.scala diff --git a/src/main/resources/eu/sim642/adventofcode2023/day14.txt b/src/main/resources/eu/sim642/adventofcode2023/day14.txt new file mode 100644 index 00000000..e301e58c --- /dev/null +++ b/src/main/resources/eu/sim642/adventofcode2023/day14.txt @@ -0,0 +1,100 @@ +.O..OO#......O...O...#.O.#....O..#....O..O#..O##O#.....OO##.O.....#.....O.....O.####.##O##.O.O.#.O.O +.#O#OO.....#O.....#....OO####.OO..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....#O.........O....O.OOO.#.#...O###.#.#O..O...#.#.#....O...O.O..O.#O#.O.#.O....O#.#...OO#...# +O.O.O#.#....##O..#..O.OO.#....O#O.O..OOO..O#O......#...#.#..O......#......O.OOO##.......O#.O.#...... +O.......OO..O..O.O..OO...#OO.O#....O..##O.O.OO....O.O....#......O..O.OO.O....OO...OOO.OO.OO.#.O...OO +OOO..O.#.O.......O....O...O.#O.......O.O.......##.#..OOO#..#...#...O............O.O...O#.O...OO.#O## +.......##.O.O..#.#O..#O..O.##.#...........##.##O.O#.OO#..O......O..............#...O..O#...OO..O#..# +.....#.....#....O....O...#.....O#.O.#..O#O.O.O..#.#...O...O..OO...OO..........OO#...##...O..#.O..#O. +OO..#O.....O#..#..OO....O.OO..#....#.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.....O... +..O.......O#O.#O..O.#.....#.OO##OO...##...O.O..#..OO.....#.O.#OOO.O#O....OOOOO.O.O....OO#..#...OOOO. +...O..O....#.#..####..OO...O...#.O.O#O#O..#.....#.O#........OO.O##...#...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..OO.....#O.. +.#O.#.#...O#....O..##..OO..#.....#..O...O#O#O...##OO..#.O.O......O.#O.......OO..O#.O#O...O.......... +.O.O......#O.#OO.O.....#...........O....OO#O.O.....O...#..O...O...#......#.OO...#.#.#..O.#O......OO# +..#O..#.#.O...OOO.O.O..##..OO......O.O......O.##.....O......#OO...O.O.#.#..##.OOOOO.#.#.....OO..O... +....O..#.O.O...#..O#..#...............O...O..................#.O.#...#OOOOO#.......#.#...#.O.#..#.#O +..#.#O....#..#O.O#.....OO#.........O...O.O...#..O#.#..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.O.#.O.#.....O#..#O#OO..##...#.##..........#OOOO..OO#..O...OO#...O.....##..O. +.##..O..O.O#.O........O.#.O..#O...O.....#............O.O.O.....#.......OO.#.##O........#.OO.##OO..#. +.#..O#O..###O#.#.OOO........#...#.O....OO...O..O##...#.#..#.O.......OO.#....O.#..#..###O.OOO..#...#. +......O...OO.#O.....O..O......OO..###..###...O..##..#.............O..OO#..#.....O..#...#.##..O#O..O. +......O.##O..O#..O..O....#..O#..#.....#O##...O..........O....O##...#...O..O....#.O.O..O..OO..O....O. +O#.#..O.OO.O...O..#OOOOO#.OOO..#O#O..OO.O#....OO..#.##.##..#.O#............O.....#..#..OO.O....#O.#. +..#.O.O.....O.O#..O.#.O#.O.#..O...O..........O#...O....#..O#.O#.OOO...OO..#O..#O.O..#.#O#OO.....O... +O..O#O.....#.#O.......#.#OO...O#...O..O.O..O..O.#.O##O.#.....O#.#O....##...##.O..O.O...............O +OO...##..O.#......O.....#O...OO.#.O.O....O.#.#.OO..OO.#....OOO...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.O#..O......OO....#..#...O.O..#.O....#O.#.O.#..OOO#..O.O.O.O##.O....OO.O..O.O###.#O.O...OO.O +...........OO.O.#...O..O#..OO.....O.O..O.#.#.OOO#.O..OO#.O....O..O..#.OO#O.#.##O.....#...#.......... +..O.#..OO.O.O#.....O.O.#.O##O#O.O.O#........#..O#.......O.O..#.##....OO..#.O...O..#.......O.OOOO...O +....O.#..OOO#..#.#..#.........#..#.#.O...OO..........O#O...O..OO..#.O..O.....O.....O....#..#.....O.. +OO#.#O..OO...O..O#....#O...O.##...##..#.#..#.O.##..#..#......O.#.O.O.##..OO........OO...O.OO#....O.. +O#.#......O.O..O#.#...OO#O......O#OO.OO.....O..O#.......#...O###..#..O#O..#........#O..OOOO#.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....#.....O...#O.....O.#.....O +#.....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.OO##...O#......O.#.#.#.........O +.O.O..O...O..O#.O......#O.#O....#..OO.O.O..............#O....O....O....#..#.O.....#OO...O..#.O..O#.O +O.#.#..O.OO..O.....O#O.O.#..O..#..OO...OO.....O.........#O...........##.O.....#...O#O.#....OO.O.O.O# +.O.O.#OO#OO.O.OOOO.#...OO.OO.OO#O.O..O#...O....##.#..#....O.....#..#......O..O.....OO....O.O.O#.OO#. +..OO...#..#.O.OO..#......O#.#.O...O.OO..#O..O..#..O.##..O...##.O.....#.#.#.#..###O.......#..##..O.O# +...O...O.O...O#...#....O.OOO...OOOO.OO#....O#..O#.##...##O.O...#O.O......#....#O.O..O...#O..OO...OO. +.##...OO.##..O.........#.......#.#.....O.O.#....O.#....OO..##O..O#.....O#O....OO..O....O....O...O... +O........O##...#...#.O....O...#OOOO##..O...OO.....##......#..O.O#.O..O#O..#O...#.......#......O.OO.. +.OOO..#O.O#O.O..O...O..OO.O.....#...#.O#OOO..#.OOO.#...#....OO.OO.O.#...#.O....O.....#..O.O#.O...OO. +O.......#...OO..O.#..#O..#...OO.##O..#O.O...#.O..#..O#.....O..#...#OO...##..#............O.....O#... +#OO...OO.O.OO...#OO.#......O...#..#....O.#....#O.#O.#.O.##....O..O..O.#O.O.....#..........#....OO.O. +..O......O.#.O.OO...O.#...O.O#.##......OO.....#O.#...O.OO..#O..#...#..#.O......O#.#...O....O###O.O.. +.#...OO.#.#.O...#...O...O..#..#..#..#...OO...#.OO......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##.OO#.O.. +.#......O.....O.....#....#..O.....#.....#O..O#.........#OO#...O##.O.....O.#..OO.........O.....#..O.. +..O....##.O..O..O..#.O#...O......OO.OO....##..#..#...#.##O....O#..O.O....#.O##.....#O#...OO...#O.O.. +OO...#.O...O#..O..O.....O...#..O..#.#..OO#O.###.#...OO..#.....O....O#.O..#OO..##..#O..O.OO.....##.O. +.#.........#..#O......O..OO...#...O#..OO.O#....O#........OO.#......O..#..O##O.O.....OOO.OO.........O +#..OOO...O..OO#..O......#O......#..O.OOO#..OO...O.OOO##.O...O.O.O..#...O...#...#..O.OOOO..O..O...... +...#..#O....O.O#O.OOOO..O.O...#.O.#..OO....OOOO....O.O#...............O#......O.........#.#..O..O..# +..#.....OO..#.#.O#O..O.O#O...#.....O.#....#.O#O#..#.#.....##..O..O#.OO...OO....O.OO.O.O#O..###....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....#.#O#.#.O...#.....O...O##O##. +.O.....O...O#O....OO#.O#...#......#...O...O..#.#........#.#.##.........O.##.O.OO#.#.#..#.....O#..... +OO...........#O.O#.O...#...OO....#.#....#O..OO...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.....#.....OOO.#.O....#.O#...#....O## +....O.O.#..O.##.#.O.#.O..O....O..O#.#..#...OO....OO#O#....###.#OO..#.....##.....#.###.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....#O.O...#O.#OO...O..O....#O..O......#.. +....O.....O.....#...#O.O.#OO..#.OO#O...##..O#...##..........O..O.#..OOO#..O...#..#O.O......#OOO.O..O +.#.O..O.........OOO........#O.OOO.......O.O..O.#O......#..#O#...O...#O....O.##.O....#.OO...O.OO.#... +..#..#O..O#O....#..#.##O..#..O...#....#O........O.O.......O..OOOO#...#.O.O..O..O.#..#O.....#..O.O... +..#....O.......OO.....O......O.#O###.OOO.##OO.OO...#..O.OO.OO..#...O.......O.O.O..#O...#.#........O. +..#...O..#...O..#.##......OO.O..#..O.OOO.##O.OO.O..##.##.OO#.OO...#...#.....#O.O...O...O..#...#...OO +#....##OO..#.......O..O.OO.....OO..#...#.O##..O.O.......#.##.OO.O...#..#.#.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#.O..#..O.#..#O....O..O#........O...#....O....#..#........O..O........O.O....#.. +...O..OO.O....O..OO#.O#O..OO#....O#.##...#.....O#..#O.#..#..#.#....O#.##...#...#O...###...###....... +..O.O#..#.OO.#..O........O..O#...O..#..O.OO...#O..##O...#.#....O.#..#......O.O..#O...#O.#...#O...OO. +..O...O#..O#.O.#O.OOO..#.#......OO#.#..#......#O.......O..O.#O....#.##......#....O#..###.O......O..O +...#.O....#O..##...#............O.O......##..O..#.O..#.O.O.O...OO.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.....#.........#.#...OO +###OO.....O##.##.O.O.O..#.....O..#O.###.O.#..O...##O##...#O....#......#.O##...#.O...O#O#..#...O.#O.O +....O....#.OOO.#..O..#.O..O....O....#...O.............#O##OO...OO....#.OO...#OO......O..O..OO#.O...# +..O.#..##.....O.#O.#O.#...O#O.#O.O#OO#.O.O.#....##.O..#OOO....##OO.##..OO.O#..O....#.#......#......# +O...#..OO.O..O#.........O##..O....#....#OO#....#O....O..O..#..O...##.....#.....OO...#.O.#O...#.....# +OO.O.#...#.O#.O.O.....#..#...OO.##.O##.OO.....O...#..O.......OO#.O.......#OO.#O..#.O...........#.##. +.O...O##...O.O.OO......O#..O.###O...........O...#.O....OOO.#.....OOOO.#......O.....O..O.....#....... +.O..#O.OOO...#.O.#.O##....##.....#....#.O..#.O......O.....O...O..#O#..#O.#.O......O....O..#O.#...... +.#.O...#...O..#..#.....O..O.......#....O.#...O#.#OO..#.O#....O.O...#....O..#OO..#.......O.##...O..O. +#.OO.#...#....O#.....#...O.....#.#OO...#..O..OO.#......#.....#.O#...#.O.......#....O.#..#..O...O#..# +#...#O..##.#...#...O...##.#...#.#.#....##.O....O.O##..#O...#.OO...#O.....#O..#..#.O##...OO..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..#....#.O.O.O......OO....##....#.O.O.O....O..#...O. +.OO....#.....O....##...O.......#...#.OO.O#....OO#.#OOO#.#O#...O.O#....O#..O..O.#O...##.....OO..O.... +O........#....#OOO...O..###..O...O.....O#...#.O.O..O.........OO....O..#O...OOO#.#.......O...#..O.#.. +.O#..OO.##.O.....O.....#..#..#OO...#.....#O.....#..O...O..OOO...O.#.#...O..O.....O.#O..OO.O##....OO. +O..O#..#...#.O...#O.........O.#.........O..#O......#..O.O.O.#..#O.##.#...#.......#...###O#..O...O... +##..O#.OO.#.O#O.O#......#.....O......O...OO....O..O..O....O..O.O.O.........O.#.O.....OO...##....#O.O diff --git a/src/main/scala/eu/sim642/adventofcode2023/Day14.scala b/src/main/scala/eu/sim642/adventofcode2023/Day14.scala new file mode 100644 index 00000000..189d6182 --- /dev/null +++ b/src/main/scala/eu/sim642/adventofcode2023/Day14.scala @@ -0,0 +1,98 @@ +package eu.sim642.adventofcode2023 + +import eu.sim642.adventofcodelib.Grid +import eu.sim642.adventofcodelib.cycle.NaiveCycleFinder + +object Day14 { + + def rollColumn(column: Vector[Char]): Vector[Char] = { + + /*def rollOne(column: Vector[Char]): Vector[Char] = { + ('#' +: column :+ '.') + .sliding(3) + .map({ + case Seq(_ , '#', _ ) => '#' + + case Seq(_, '.', '#') => '.' + case Seq('#', 'O', _) => 'O' + case Seq(_, '.', 'O') => 'O' + + case Seq('.', 'O', 'O') => 'O' + case Seq('O', 'O', 'O') => 'O' + + case Seq(_, '.', '.') => '.' + + case Seq('O', 'O', '.') => 'O' + case Seq('.', 'O', '.') => '.' + case Seq('.', 'O', '#') => '.' + //case Seq('#', 'O', 'O') => 'O' + //case Seq('O', 'O', '.') => 'O' + //case Seq('O', '.', 'O') => 'O' + //case Seq('.', 'O', '.') => '.' + //case Seq('O', '.', '.') => '.' + //case Seq('.', '.', '#') => '.' + //case Seq('.', '#', '#') => '#' + //case Seq('#', '#', '.') => '#' + //case Seq('O', 'O', 'O') => 'O' + //case Seq('.', '.', '.') => '.' + //case Seq('#', '.', '.') => '.' + //case Seq('.', '.', 'O') => 'O' + //case Seq('.', 'O', 'O') => 'O' + //case Seq('#', '.', 'O') => 'O' + //case Seq('.', '#', 'O') => '#' + //case Seq('#', 'O', '.') => 'O' + //case Seq('O', '.', '#') => '.' + //case Seq('.', '#', '.') => '#' + //case Seq('#', '.', '#') => '.' + //case Seq('.', 'O', '#') => '.' + //case Seq('O', '#', '.') => '#' + case x => throw new IllegalArgumentException(x.toString()) + }) + .toVector + }*/ + + def rollOne(column: Vector[Char]): Vector[Char] = { + + def helper(column: List[Char]): List[Char] = column match { + case Nil => Nil + case '#' :: Nil => '#' :: Nil + case '.' :: Nil => '.' :: Nil + case 'O' :: Nil => 'O' :: Nil + case 'O' :: 'O' :: newColumn => 'O' :: helper('O' :: newColumn) + case 'O' :: '.' :: newColumn => 'O' :: helper('.' :: newColumn) + case '.' :: 'O' :: newColumn => 'O' :: helper('.' :: newColumn) + case '.' :: '.' :: newColumn => '.' :: helper('.' :: newColumn) + case '.' :: '#' :: newColumn => '.' :: helper('#' :: newColumn) + case '#' :: '#' :: newColumn => '#' :: helper('#' :: newColumn) + case '#' :: 'O' :: newColumn => '#' :: helper('O' :: newColumn) + case '#' :: '.' :: newColumn => '#' :: helper('.' :: newColumn) + case 'O' :: '#' :: newColumn => 'O' :: helper('#' :: newColumn) + case _ => throw new IllegalArgumentException(column.toString()) + } + + helper(column.toList).toVector + } + + NaiveCycleFinder.find(column, rollOne).cycleHead + } + + def totalLoad(grid: Grid[Char]): Int = { + grid.transpose + .map(rollColumn) + .tapEach(println) + .map(_.reverse.zipWithIndex.map({ + case ('O', i) => i + 1 + case _ => 0 + }).sum) + .sum + } + + + def parseGrid(input: String): Grid[Char] = input.linesIterator.map(_.toVector).toVector + + lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day14.txt")).mkString.trim + + def main(args: Array[String]): Unit = { + println(totalLoad(parseGrid(input))) + } +} diff --git a/src/test/scala/eu/sim642/adventofcode2023/Day14Test.scala b/src/test/scala/eu/sim642/adventofcode2023/Day14Test.scala new file mode 100644 index 00000000..2e0f0c08 --- /dev/null +++ b/src/test/scala/eu/sim642/adventofcode2023/Day14Test.scala @@ -0,0 +1,27 @@ +package eu.sim642.adventofcode2023 + +import Day14._ +import org.scalatest.funsuite.AnyFunSuite + +class Day14Test extends AnyFunSuite { + + private val exampleInput = + """O....#.... + |O.OO#....# + |.....##... + |OO.#O....O + |.O.....O#. + |O.#..O.#.# + |..O..#O..O + |.......O.. + |#....###.. + |#OO..#....""".stripMargin + + test("Part 1 examples") { + assert(totalLoad(parseGrid(exampleInput)) == 136) + } + + test("Part 1 input answer") { + assert(totalLoad(parseGrid(input)) == 112048) + } +}