-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
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,64 @@ | ||
package no.rodland.advent_2023 | ||
|
||
import no.rodland.advent.Day | ||
|
||
// template generated: 14/12/2023 | ||
// Fredrik Rødland 2023 | ||
|
||
class Day14(val input: List<String>) : Day<Int, Long, List<String>> { | ||
|
||
private val grid = input.parse() | ||
|
||
override fun partOne(): Int { | ||
return grid.map { line -> | ||
val collapsed = collapsed(line) | ||
val weight = collapsed.foldIndexed(0) { index: Int, acc: Int, c: Char -> | ||
if (c == 'O') { | ||
acc + (line.length - index) | ||
} else { | ||
acc | ||
} | ||
} | ||
weight | ||
}.sum() | ||
|
||
} | ||
|
||
fun collapsed(line: String): String { | ||
var previousLine = line | ||
var newLine = hopOne(line) | ||
while (newLine != previousLine) { | ||
previousLine = newLine | ||
newLine = hopOne(newLine) | ||
} | ||
return newLine | ||
} | ||
|
||
fun hopOne(line: String): String { | ||
val array = line.toCharArray() | ||
line.forEachIndexed { idx, c -> | ||
if (line[idx] == '.' && (idx + 1) < line.length && line[idx + 1] != '#') { | ||
val tmp = line[idx] | ||
array[idx] = line[idx + 1] | ||
array[idx + 1] = tmp | ||
} | ||
} | ||
return array.joinToString("") | ||
} | ||
|
||
override fun partTwo(): Long { | ||
return 2 | ||
} | ||
|
||
override fun List<String>.parse(): List<String> { | ||
val width = first().length | ||
val height = size | ||
return (0..<width).map { j -> | ||
(0..<height).map { i -> | ||
get(i)[j] | ||
}.joinToString("") | ||
} | ||
} | ||
|
||
override val day = "14".toInt() | ||
} |
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
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,105 @@ | ||
package no.rodland.advent_2023 | ||
|
||
import no.rodland.advent.* | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import readFile | ||
|
||
// | ||
// run: download_aoc_input.sh to download input | ||
// | ||
|
||
@Suppress("ClassName") | ||
@DisableSlow | ||
internal class Day14Test { | ||
private val data14 = "2023/input_14.txt".readFile() | ||
private val test14 = "2023/input_14_test.txt".readFile() | ||
|
||
private val resultTestOne = 136 | ||
private val resultTestTwo = 2L | ||
private val resultOne = 108144 | ||
private val resultTwo = 2L | ||
|
||
val test = defaultTestSuiteParseOnInit( | ||
Day14(data14), | ||
Day14(test14), | ||
resultTestOne, | ||
resultOne, | ||
resultTestTwo, | ||
resultTwo, | ||
{ Day14(data14) }, | ||
{ Day14(test14) }, | ||
) | ||
|
||
@Nested | ||
inner class Init { | ||
@Test | ||
fun `14,-,example,1`() { | ||
report(AOCTest({ "123".toInt() }, Unit, 123, 5, "14".toInt(), Part.TWO, false, "example")) | ||
} | ||
|
||
@Test | ||
fun `14,-,example,2`() { | ||
report(test.initTest.copy()) | ||
} | ||
|
||
@Test | ||
fun `14,-,test,init`() { | ||
report(test.initTest) | ||
} | ||
|
||
@Test | ||
fun `14,-,live,init`() { | ||
report(test.initLive) | ||
} | ||
} | ||
|
||
|
||
@Nested | ||
inner class Parsing { | ||
private val day = Day14(listOf("")) | ||
private val test1 = AOCTest({ day.collapsed("") }, Unit, listOf(""), 1, "12".toInt(), Part.ONE, false, "collapsing") | ||
|
||
@Test | ||
fun `12,-,example,collapse_1`() { | ||
test("OO.OO", "OOOO.") | ||
} | ||
|
||
@Test | ||
fun `12,-,example,collapse_2`() { | ||
test(".O", "O.") | ||
} | ||
|
||
private fun test(input: String, expected: String) { | ||
report(test1.copy(function = { day.collapsed(input) }, expected = expected)) | ||
} | ||
|
||
} | ||
|
||
|
||
@Nested | ||
inner class `Part 1` { | ||
@Test | ||
fun `14,1,test`() { | ||
report(test.testPart1) | ||
} | ||
|
||
@Test | ||
fun `14,1,live,1`() { | ||
report(test.livePart1) | ||
} | ||
} | ||
|
||
@Nested | ||
inner class `Part 2` { | ||
@Test | ||
fun `14,2,test`() { | ||
report(test.testPart2) | ||
} | ||
|
||
@Test | ||
fun `14,2,live,1`() { | ||
report(test.livePart2) | ||
} | ||
} | ||
} |
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##.....O.#...##...#OO.....##...#O...O..........OO......#..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#..#.O.O......#.O#..O....#.#..#...#.O.#....#....O.#...#O#OOO.##...O............OO#.#..# | ||
.OO.................#.#.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.OO...#...#O####...##.O.#..O......#O.O..O......O.......O....O....#..O...O.....OOO..#...O.....OO | ||
OO.#O.....#.O.#O.#OOO...OO#.........#..O.#O.#O..##O...#.##..#.....#...#.....O...O...............O.OO | ||
...OO..#....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...#....#......#.....#..O....#..O | ||
OOOO#..##...##.....OO..O##.O.OOO#..O...O.#....O..OO##......O..#.O.#.....O.O#.#.#.O....#..........#O. | ||
#.OO#O.O..#.O....#..O.OO#..OO...O.O...O.#.O...##.O.....O..OO.#O..#...O.##..#......O#O#OO#...##O..... | ||
..#..##...O#.OO#.O.O....###O.O......#....#O..O..O.....O#O.#.....O#....O...#OO.....O###O.....O.OOO.O# | ||
O.........OO..O...#.O.#O.#..O...O#.....O#.O...O.OO#.#.#...#...##...O#.OO.OO....O.OOO........#..OO.## | ||
.....O..O........OO...OO#O..O.................#...#..#....#.O..#..O#..O..#....O##..#...#O.O.OO#O...# | ||
....OO.#OO...O.O.OO.##..OO....O#....O......O..O....O......O...OOO.OO...#.......O.OO....O...OOO#...#. | ||
....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.O#...O....O..O###..#..OO.O..O.....O##..#...#.##.............OOO...OO. | ||
O....##.#OOO...#....#...#....#.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......#......##..##....OO......OO..#..O#.O#O#O.#..#... | ||
...#O...O..O......O.#......OO..O..#O..#O#O...O.......OO#.#OO..#O....O.......#O.....#....OO....O....# | ||
...O.OOOOO..#..O.O.O#..#....O#...##.#..#.O.O.O.......O.....#....#O.O.#O......#.OO...OOO...#....##O.. | ||
.#.......#.O...##..O.OO....O..#.#..#..O..O...##O.OO#.#.........O........O#.#..#O.....OOO##.#..O...OO | ||
...O.....#..O#..##OOO....O.#..O.....OO.##..#OO.#...#O#..O#.OO..#.O....#OO.......O.OO.O.#O##..O.O.O#. | ||
...#...###..#..#....#O#..#O......##.O.####.#.....O.O.......O#.#.O#.O..#..#.#...O...#...#O.OO...O.... | ||
#OO.#O..........#.OOO.....O...##..O.O..O#O.O##O...#...#....#....O.O.#.#O...O.#..O#.OOO..#..#.O...O.. | ||
.O....#OOO..O..#.#.O.#OOO.......O.#..#...#OO.##.#..OOOO...#.#..#....#..#....O.....#.#.OO....O....... | ||
.OO...O#..#............O...#.#.OO.O#OO....#O..#....O.#O.OO......O.O.....O...O.#.....OO.##..#...#O.O. | ||
..O..O#O.#OOO....#.OO....O....O..#.#.......#...............O...O....OO.O..#O...OO...#OO........OO... | ||
#OO.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...OO..O..O.O.O...#.......#.....O.O....#...#..O....#.......##..O.O#.....O........#.....#..O | ||
.#....O..O.........#..O...#....#O#..#...O.O.OOO...O...#.....##...##.#...OO#O..O....O..O...O#........ | ||
...O...O.O..#.#..#O........#####.O#....#O##OOO.O..O.O#.O.......OOOOO....#.##O..#...#.........O..O.OO | ||
..#..#...OO#.OO..O....##.O..OO.O.O.#.#O#.O#...O...O.O.#O......OO..O.#.#..O.OO.......###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.#..#.OOO....OO#...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.OO.O##.O..O...O...#..#.OO.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....OO#O......O#.#.OO#.....#.OO.O...OO##.O....O.O##...#.............#O.. | ||
..#.#..###.....O.O..O......O....O....#O.OO.O....OO..#.#O#......O..#..O.##.#.##..O.OO...O......##...# | ||
...#.O..#.OO.........O..O.#O......#O..#..O#.O#......O#..OOO.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..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.O...##..OO..O.#..#..#.#...#.#...........OO. | ||
....O#.#.O..#....O....#O.O.OO##O.........##O..#O.OO.OO..#....O.O#..###.O......##.O..#.#O..#.....#... | ||
....O.O.#.O....#O.....##....#.O......O.......#..#.O.#..#O.#.OO.#.OO..##..O..#O...O.O.OO......O#O.... | ||
OO..O..O...#.......##OO.........#....O.#.OOO.O..OO..O.....#...O..#..O.....O..#...O.OO.....O#.O.O.O.# | ||
.OO#......#O.#......O....#.#..#O..O...#.#.........OO..........O..O.....O.O..O##.O...O.O...#......... | ||
..#.#OO.OO........#O..O...#O#OO...#..O..#.##.OO.#.....O..O.OOO..#..#.#....OO...##.OO..O#....O....O.O | ||
.#.#.#..O.OO..O......O...O#O.OO.......#...O...O.#O...OO..O#.O.#.##............##.OOO#.OOO..#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..OO#O...#.........#...OO.O##..#..##....#.# | ||
##O.#O......O..#.#...##.##.O##.#.....#OO#..#.........#O.O...#......#..O..O.O##..O.OO......O#O#O#..#. | ||
......#.O#O.O...#O.OO.....O...O##.#.OO#..OO....#O..#....#.....O.#O.O.#O#..OO..O..#O.#.....O#......## | ||
....###.......OO#O...O..#..#.#.....#.#...O..OO#.O..#OO#.O..O....OO.O.#.##O..#..O...OO.#.O.#......#.. | ||
#O.O....#...#.O..O..####....O#........O....O#..O..O..OO..OO...OOO..O..OOO......#.#....O..##.O#..#O## | ||
..........O.#.O.O#..#.O..O#.#....#...#.##OO..OOO.....O##O#....#O.#....#.O...#O#.O.#.##..#O.O#....... | ||
.....#...#O#......O#.O..........#O.O.OO..O..OO.#.............O#.#........O#.OOO##O...O.O#.#...#..... | ||
O..O..O.........OO#...O.O...O....#.#.#...OO#...OOO...O........#.#O.OO#.O.....O.O..O#.OOO.O...O.OO... | ||
OO#....#..O.#.O.O##O##.........#..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.....#......OO...#OO..O......#.#..........O#..O..O..O.O.O....O...O...#O..# | ||
..##.#O.#.OOO..O...O..O.O..O...##.....OOO#..O......#..O...........#O......O..#OO#..O.O.##....OO..O#. | ||
.#..............O#..#..O#O...OO###.##....O.#.##..###.#O.#....#...#.OO.O#....O..OO..#OO#.O..........O | ||
...#.#...#OO........#.O..#.O...#.O.O.#.....#......O#......#....#.##O.###.O....##.....O#OO.O....O##.. | ||
.O.O......O..#.....O......##O#..#..OO..#.#..O...#.O..#O.O#OO..#..O#.O....#...#O..O..#..O#.O.....O.OO | ||
O.#.O.OO.OO.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##..#OO.....O..OO#.O##...#....#..##..OO.OO#..O...OO.....#.#.# | ||
O#.OOO.#O..O.O#.O#O......OO...#O....O............#..#........#.O##..#O.O###.OO..#...#.....O........O | ||
O......O.OO..#.#..#.......OOO.#.#........................O#O.#..O..........##.#..O#.......#.#....O.. | ||
O.#O...O#.#O.......O##O..#..O.#.#O.O.O...#OO..O.O##O.....O....OOO...##..OO....#.#..##...#..O.#.O.#.O | ||
#..#.#O.#..##....OOO.....O......O.#O..#O..O.#O.O.#....O...O#.OO##..#...O###....###..#..O.#.O...#.... | ||
...O.......O.O.......#.#OOO....O.......#..O...O.........##..#.O.#O.O......#..OO.O.O......#.....OO... | ||
O.....#O.#.#....#..#.O.OOO...#.O###......OO...O.O.O.###...O.O.......##..#......#..OO.O.....OO...O.OO | ||
O.O.....#.#.#....O#OOO.....O.....#O...##O..#OO.O.......O.O.......#.O.#.........#..........O...#.OOO. | ||
O....OO.#.#O##.O....#O#.OO..O...OO.#.#O.#.#.O...O#O...OO.#.O...##...OO..#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..OO.O.#....#..#.......OO..OOO.#O#...O. | ||
.#.....O......O..O...O....#...OOO#...O#.#.#.#.OO.O.O...#..O..O#......#.O.....O.O.O....#...O#.....#.O | ||
...O#........O......OOO..#.OO#.O.#.O..##......#.#O...O.O...O.#O.OO.OO..#..O...O.........#.O##..OO##. | ||
..O..O.#O..#O..##....O.O.#O...O#.O.....##.....O......#...#.....OOO..O##.O#..#.#.O.......#O...O#...## | ||
...O.OO..O.#.OO#....#.O#O..O...#O.O.###...##...O.O..#..O..#.O....#........#...#....O...O....#O#O.O.# | ||
...OO.#...O...OOO..O.....#..#.O.#OO...O...O..#..O..O.O...O..#.#O.#OO.O.....O.O..#...O..O..O#.O#O...# | ||
O...#...##.OO..OO.......#..O.O..OO.OO.O#......#.O#O.O.#..O.#.O.O.O#OO...#.....##O.OO.#OO....O...###. | ||
O.............#.#..#.O....#....O...#..O..O#.O...OO.O.O........O#.O........OO..#....O...#O#O#.O.#OO.O | ||
OO.........O...#.O.O...#.....O...#.OO#O.##............#....O....##.##..#.O..O....OO...OOO..O........ | ||
O.....#....O.#O..OO.O#.......#.....#....#.....##OO#..O..#OO.....#.....#..OO#..O#..O.O#OO.O.O...O.OO. | ||
OO#....O....O#....#O#.....#.....O.#O#..OO.OO.O..#..O....#O..O#.#O#..OOO...##...#..#...O..O...O#..O#. | ||
O......O#...O.#......O.O..#..O.#.....O...O...OO###...#.OO......O..O....#..O#.O....#..#.#..#.OO..O... | ||
...#...#O#O.......#.O#.O....OOO.O..OO#.O.O..O#....O....#OO..O.......O.#..#...#.....O...O.O..O..O.#.. | ||
.O#..#.O#.#O#.O#..O..#....#O#..#.O.##.O.OOOO.#O..O#....O..OOO.......OO.#...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### | ||
O.......O#..#......#....OO.O.#O..##O...OOO.O....O..#O....#.#..##OOO.OO#.....#..O.O...#O...O..O.O..OO | ||
...O..O#O.O..O...##.....O.O.#O........O.###O#..#...#.OO.#.O.....OOO.#.......O..O.OO..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.OOO..#...OO...#.#. | ||
.O.O..#OO..O..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..OO.#O..O..O.#......OO...#...OO#O | ||
..###O.........O..#.#.#.#.O..O.#....#...#..O##O#..#O#.O#OO.O.O....O.....#O...#..O..#O..O....#.....#. | ||
....O#.O..#......#..OOO..#.O.O...O.#O.......O.O.#####..#.....#.#.#....#...O#..O.O.O.O...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,10 @@ | ||
O....#.... | ||
O.OO#....# | ||
.....##... | ||
OO.#O....O | ||
.O.....O#. | ||
O.#..O.#.# | ||
..O..#O..O | ||
.......O.. | ||
#....###.. | ||
#OO..#.... |