diff --git a/README.md b/README.md index 102b2b2..77d5211 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ _My solutions to the 2023 edition of [Advent of Code](https://adventofcode.com/2 | **[11](https://adventofcode.com/2023/day/11)** | [solution](src/main/scala/adventofcode/solutions/Day11.scala) | | **[12](https://adventofcode.com/2023/day/12)** | [solution](src/main/scala/adventofcode/solutions/Day12.scala) | | **[13](https://adventofcode.com/2023/day/13)** | [solution](src/main/scala/adventofcode/solutions/Day13.scala) | -| **[14](https://adventofcode.com/2023/day/14)** | [](src/main/scala/adventofcode/solutions/Day14.scala) | +| **[14](https://adventofcode.com/2023/day/14)** | [solution](src/main/scala/adventofcode/solutions/Day14.scala) | | **[15](https://adventofcode.com/2023/day/15)** | [](src/main/scala/adventofcode/solutions/Day15.scala) | | **[16](https://adventofcode.com/2023/day/16)** | [](src/main/scala/adventofcode/solutions/Day16.scala) | | **[17](https://adventofcode.com/2023/day/17)** | [](src/main/scala/adventofcode/solutions/Day17.scala) | diff --git a/input/14.txt b/input/14.txt new file mode 100644 index 0000000..7b2595b --- /dev/null +++ b/input/14.txt @@ -0,0 +1,100 @@ +..O....O....O..#O#OOOO...O..O...#.O.#.OO...O.##O#.#..#...OOO.#....O#....#...OO..O.OO..#.OOO.O..O.O.O +.OO.##....#.#..#O...........##O..#..O..O.....#.O.##.O.O............#......##........#.OO..#OO..#.... +##........O.O.#O...##.#...#.#.O...#..#O...#....#O...OOOO..OO.OOO.O...O.#........OO.O.O....O...O##O.. +...O..O#..O.O..O.O..OO..#.....O..OO....O#..O.##..O..#O.#.OO....####.OOO.OO..O.#..OO.O....O...#.#.O.. +...#.O.OOO..#O...O#...O....O...#.O.OO.O.O...O...#...#.#O...#....#....#..O....#..O..#...#..O.....O... +.OO...#...#O#....O.O#.O#.O#.OO.OO.O.O#..........O#....O........O.O.....#...O.#....#........#........ +.#.#.#.OOO....#.O.#....#.#..##O.........O.#OO.OO..OO#.#....##.#.##O#.#....O.#.........#...O.#O.O.... +..O.#.#.O.....O....O.#..#..O#.#OO..OO........#O.#OO.O..OO.O.......#...#.#..##...........O...#.O.#.#. +...#....#...O....##O......O...#...#....O.#...OOO#..#..O....##.OO.......#O.#.....OO....#.###O#O#..... +O..#O..O..OO.OO.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.....#.....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..##.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#..OO.#..#. +##....O...O###.O.....O#.OOO.O##.##....O.....O...O..O...O..#...O....##.....O........##...#.O...##O... +...O....###......#...O.OO........#.#.....OOO........O.#......O.O#....O.O.....#..##..O..#.#O#..O..O#O +.O..#.##O#...O.OO#...#OO.#..##.##..O.#.#O.O.#...OOOO...#.........O...........#..O.......#.##.O...##. +.OOO#.......OO....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..OO......#....OO#..#..#..#OOO...#..#.....O#OO..O.O..#.O#..OO.##...O#.#.. +.O...OOO.O.#........#..#..O.O.......O...#...#OO#O.....O...O......O...OO.O#.O.O#.OO.#......O.O....#O. +O.##O.....OO....O..##..O....#......O...O...O....####..#.....O.....O..O.#.O..#OO.#..#..O#O..O...#...O +..O...#O#...#.#..O...O#.O......O.#..#...#..#O..#...#..O.....O...OO.O#....OO.......#..###.OO..#..OOO. +.#..##O..O....O...O....O..O###.O.O......O#OO#O#O#O#...#...##.O.......#OOO#.##.#.O.....O.O.OO..#...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..#....OOO..#...OOO....O. +O#O#.....O...OO##.....O#...O.....OO#.#...OO.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........OOO.#O.....#.#....O..O..###....#.#..#.#O#.#.....#..#..#O##..#..OO.##.O....O +.O..O.O.....O.O...O#.#...##O....O.O.O...#....#O....#.O..OO...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....#...##O..O..O..#...O..#OO.#..O..#OO..O.........#O....#.#..O#O.O.#.O.....##O. +.......#.O.O.O..O##.............OO...O#O.O#.O..OO#.......O#...OOO.....#...O.......#.#..O#..O..OO#... +O...OO#.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.#...#.....O..O....OO#OO.O.O.O..........#O..O#.O#..O... +......#O.....O.....OO.O..O#..#O###..O..#OO.#OO.....O.OOO..#.O...O....#O..#OO.#O...O.O....O#.....O.## +O.....O.O..#....O....##.....#.......OO#O.#...O..##...#....#.O#O..OO#.##.#.O.O.O.O#...#.OO.#O..OOOO#. +...OOO.#..#OO.O...#.....O#......#..O.O..OO#.O.O#.#.#........#.O.....#..O..O..#O..#O.#..#....#..#.O.. +..O..O.....#.##.OO#.......O..OOO....OOO...#.O.#O...O...OO....O#OO...O.##....O.....#..O......#......O +.#..O..OO#O..#.##.O.O...##OOO.#...O......#O...O..#..#..#..O........#...O..#..#.OOO#.O.O......#.##O.# +#..#...O..#.O.#.....O..#.OO.#.....O.....#....O.OO.#O..#.#.#O..O....O.OO..O.......#O##..#O.#.OO...O.. +O...O....OO..#OOOOO##.......##....#........O..O......O#O.#.......O.#.....OO........###..#........#.O +O.....O.......#OO.O.OO..O.O##.....O....O##......OO...O#O...O.....O.O....OOO#.O....#O.##...#.#..O##.. +..O...#.....##.#O...#.OO.O#......O...O....OO..O.##O..OO#.O.#.O.O..#..#.O.O..O...O..#.OO.O.O....O.... +O.OO.###.O..OO..##O....#.O......O.....O..OO......O.O.#OO...OO.#.....O#..O..#O.O........O.OO#O.OO.... +O.....OO.....#O..O...O.#.OO#O.O##O...OO#..##O#.O.O#OO....#.#..#...##.#...#..O.O....O..O....#O.#.O... +...#.......OO.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#.#OOO.O.O.....O#O.....O +.....O...O.O...#.#....#....##.OO..#..O.O.OO...O#.O.....#..#...O...O......#.#....O..O......OO.O.O...# +.O.#..#.O..O...#.O.....OOO.O.#.OOO..O.........#.OO#....#.O.....#O.....#..#...O......#.#O.O...O.OOO.O +O#OO...O..#OO.OOO##..O........###..O....O#.O...O....O..#..........#...OO#O.....O.OO.O..#....O.#..#.. +............O.O..O#...O.#......O....#.............O......#.O..O...O.#OO...#.....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..O#..O.O....O..#....O#.O##O..O..#.#OOO#O.O.##.O##O..........#.O.O#..#O.O.OO.#..OO...###O..#.O.## +.......O..........##..#.#.......#.O.O..O......O.O.OO.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..O..OO##.....##OO..#..#.O#O....O...#.....O#.O.#.......#.....O#..O..O.#. +.O..#...O..#.....O.#O#O....OOO..#..#.O..O.O.#......O.#OO#O..#.O#..#....O..#.O.O#......O#..##...OO..# +##.#...##.O...OOO....##O.O..OO#OO.O#...O.OO........#......OO...#.#..O#.#........O....OOO..O..O..O.#. +#.OOO....O......O....#.#.OO..#.....O...O.OO..O..OO.....OO........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..OOO..........O.....#O....O......#..OO...O..O#O.#....#.O..........#O.OOO...##.. +#..O#...OO#.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..#.....#.#....#......#...O.OO.#OO....#..##.O..O#O...OO..#...O.#.#.O..##.#....OO.O..... +#.....O......O#.#........#....O..#O.#O.OO..O.OO.O..O##.#.#.#OO....O.#....O.O..#.....#.....O.OO...OOO +..OO.##OO#O.............O.OO...O#.##...#.#O...#O.....#...#.......#OOO.#.O#OO.....#..O#..#...OO....#. +.#....OO.#.O##...#.O#.....OO#O...OOO..O##....OO.#.#.#......O#...O#OO....#......OOOOO#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..#.OO.#O#...O......#....#.......## +.O.###......#........##.....#...#...#O.O#O..........#..O.....O.OOO#O.....#...#.OO#O.O##O.OOO........ +.#..O.......O....O...#.........#O##.OOO#O..#.#...O....O#O..O....#..O#..#....#O#......O#O#..#.....#.# +.###O.O..OO.O..O....O#.##O.O..O..O..#O#.#O#.O....O..O#..O..####.O...#..O..#OO....OO.......#..#.O..#. +O...O#...#.O....OO..##..O..O#O#.#OOO...OO...#..#....#O..#.#....#O...OO.O...O.#.O..#.#..O.O..O#.O#.#. +#.O..#..O##O..OOO.O...O.OO#.#...OO.OO.......O..#.O#OO....O.#O...........#...O..O...###O#..##..#.O.#. +.O.O.#.....O#...OO....#.....#.OOO.#..#O.#O.#O...#..O.....##..O#............O....O#...#.O#.....#OO.#. +##O.O...OO.O.#.#O..O....#.#.O............#O..O.....O..#..#.#.O..#.......#.O.........O.OO...#..#####. +.O##..####...#...........#.O..#..O#..OO........O..O...#O.##OO..#..O.O.O..#..##.O.OO#O#...#....#O.O.O +O.....O.O#....O#..#...O....OOO.OO..#..#......O...OOO.....O...O#..O..........####............#.O...OO +O#..O...#......#...#....#.#.O.........O...O....##O......#...O#.O.OO....#O.......#.O.O.OO.#O.#O...#OO +.O.....O...#.#..O#...O#.OO##O...#..#....#.O.O....O......#.O...##..O.##.....O#.O#..OO#.#.........#... +O....O.#O......O#O..OO#.#..#.OOO...#.#...#..#..OO..#........#.O..O..O##..OO#.#.O#OO.....#.O.##....O. +.OO............O....OO..#.#...O.#..O.......O.O......#....O..O..O#.......#.....OOO##...#.....O...O.#. +.#...O..OO.O....O...#.#...#OO#.O...#...##.O.......O.......#.....O..##O..O..OO..O#.OO..O.O...O.#...#. +..OO#.#O..#OO..O#O#O.O....O..O.......OO##.O..#.#O....O.#.....O..O.O....O##.#....#.#....O..#..#..OOO. +....#..O#..#...##.....#.#O.O......#....O##............#.....##O...#..O..##.#O..#O...O....O...##OO... +#O...O......O.O.#.OOO.....O##...#..O..OO...#..O#...O....O.O....#..O#...OOO..#.O..O....#..##.#.O.O... +##..#O#.O.#O...O#....OO...#..#.O##.O...#.O.#.O.O....O.#...O#.....####....OO.O..#.OO#....O...#..#.... +O#..#...##.#.###.O##..#..##OO...#O#.........O.....O.OO#..OO..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...#.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..O...O.OO....#..#..#.O#....#.#..O##.#..#.#.OOO#.#.O..#..O##......O.....OO....O#.#...O +#O.....O........##OO#..OO..O..O.#...O#......O..O..OO..OO....##.....#OOO.#OO.......O....O#.O...##..#. +.O..OO.O.....OOO...##O....##O....#.#.#O...OO.O...#..#......O.O.O..O.O..##....O....O.O.##....OO.#O.O. +..........O..O#...#...#..#..O..O...O#..O#OOO#......OOO#O..O.O.#O...O#..#O.OO..O##..O##O...O.O#.....# +#...#O..O.......O.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......OOO.#....#O#.O..O..O#O#.##.#.#.#.............#O##OO.#.#O.#....O.O.#...... diff --git a/output/14-1.txt b/output/14-1.txt new file mode 100644 index 0000000..2593de4 --- /dev/null +++ b/output/14-1.txt @@ -0,0 +1 @@ +108614 \ No newline at end of file diff --git a/output/14-2.txt b/output/14-2.txt new file mode 100644 index 0000000..87cc7dc --- /dev/null +++ b/output/14-2.txt @@ -0,0 +1 @@ +96447 \ No newline at end of file diff --git a/src/main/scala/adventofcode/solutions/Day14.scala b/src/main/scala/adventofcode/solutions/Day14.scala new file mode 100644 index 0000000..9a2e1de --- /dev/null +++ b/src/main/scala/adventofcode/solutions/Day14.scala @@ -0,0 +1,56 @@ +package adventofcode.solutions + +import adventofcode.Definitions.* + +@main def Day14 = Day(14) { (input, part) => + + case class Vec(i: Int, j: Int) + + val mapWithRocks = input.toLines.map(_.map { + case '.' => None + case '#' => Some(true) + case 'O' => Some(false) + }.toIndexedSeq) + + val map = mapWithRocks.map(_.map { + case Some(true) => true + case _ => false + }) + + val initialRocks = mapWithRocks.zipWithIndex.flatMap((row, i) => row.zipWithIndex.collect { + case (Some(false), j) => Vec(i, j) + }).toSet + + def slide(map: IndexedSeq[IndexedSeq[Boolean]], rocks: Set[Vec]): Set[Vec] = + rocks.toSeq.sortBy(_.i).foldLeft(Set.empty[Vec])((rocks, rock) => { + rocks + (0 to rock.i).reverse.view.map(i => Vec(i, rock.j)).takeWhile(next => !map(next.i)(next.j) && !rocks.contains(next)).last + }) + + def rotate(map: IndexedSeq[IndexedSeq[Boolean]], rocks: Set[Vec]): (IndexedSeq[IndexedSeq[Boolean]], Set[Vec]) = + val newMap = map.transpose.map(_.reverse) + val newSet = rocks.map(rock => Vec(rock.j, map.size - 1 - rock.i)) + (newMap, newSet) + + def revolution(rocks: Set[Vec]): Set[Vec] = + (0 until 4).foldLeft((map, rocks)) { case ((map, rocks), _) => + val slided = slide(map, rocks) + rotate(map, slided) + }._2 + + def load(rocks: Set[Vec]): Int = rocks.toSeq.map(rock => map.size - rock.i).sum + + part(1) = load(slide(map, initialRocks)) + + def iterate(rocks: Set[Vec], history: Map[Set[Vec], Int], i: Int): Int = + if i > 0 then + iterate( + revolution(rocks), + history + (rocks -> i), + history.get(rocks).map(_ - i).map(i % _).getOrElse(i) - 1 + ) + else + load(rocks) + + part(2) = iterate(initialRocks, Map.empty, 1000000000) + +}