-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/resources/eu/sim642/adventofcode2023/day14.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |