Skip to content

Commit

Permalink
Solve 2023 day 14 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 14, 2023
1 parent 6399e04 commit f82f64f
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/main/resources/eu/sim642/adventofcode2023/day14.txt
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
98 changes: 98 additions & 0 deletions src/main/scala/eu/sim642/adventofcode2023/Day14.scala
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)))
}
}
27 changes: 27 additions & 0 deletions src/test/scala/eu/sim642/adventofcode2023/Day14Test.scala
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)
}
}

0 comments on commit f82f64f

Please sign in to comment.