Skip to content

Commit

Permalink
AOC 2023 day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 14, 2023
1 parent 71538c4 commit 2e00ebe
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 16 deletions.
6 changes: 3 additions & 3 deletions adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@
[20222402tests]: src/test/java/org/ck/adventofcode/year2022/day24/Part2Test.java
[20222501tests]: src/test/java/org/ck/adventofcode/year2022/day25/Part1Test.java

# 2023 (26/49)
# 2023 (28/49)

| # | Name | Solution | Test |
|---------:|-------------------------------------------------------------|:------------------------------------:|:---------------------------------:|
Expand Down Expand Up @@ -1248,8 +1248,8 @@
| 20231202 | [Day 12: Hot Springs - Part 2][20231202] | ✅[💾][20231202solution] | ✅[💾][20231202tests] |
| 20231301 | [Day 13: Point of Incidence][20231301] | ✅[💾][20231301solution] | ✅[💾][20231301tests] |
| 20231302 | [Day 13: Point of Incidence - Part 2][20231302] | ✅[💾][20231302solution] | ✅[💾][20231302tests] |
| 20231401 | [Day 14: ][20231401] | [💾][20231401solution] | [💾][20231401tests] |
| 20231402 | [Day 14: - Part 2][20231402] | [💾][20231402solution] | [💾][20231402tests] |
| 20231401 | [Day 14: Parabolic Reflector Dish][20231401] | ✅[💾][20231401solution] | ✅[💾][20231401tests] |
| 20231402 | [Day 14: Parabolic Reflector Dish - Part 2][20231402] | ✅[💾][20231402solution] | ✅[💾][20231402tests] |
| 20231501 | [Day 15: ][20231501] | [💾][20231501solution] | [💾][20231501tests] |
| 20231502 | [Day 15: - Part 2][20231502] | [💾][20231502solution] | [💾][20231502tests] |
| 20231601 | [Day 16: ][20231601] | [💾][20231601solution] | [💾][20231601tests] |
Expand Down
163 changes: 153 additions & 10 deletions adventofcode/src/main/java/org/ck/adventofcode/year2023/Day14.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,174 @@
package org.ck.adventofcode.year2023;

import java.util.Scanner;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20231401,
name = "Day 14: ",
name = "Day 14: Parabolic Reflector Dish",
url = "https://adventofcode.com/2023/day/14",
category = "2023",
solved = false)
category = "2023")
@Solution(
id = 20231402,
name = "Day 14: - Part 2",
name = "Day 14: Parabolic Reflector Dish - Part 2",
url = "https://adventofcode.com/2023/day/14#part2",
category = "2023",
solved = false)
category = "2023")
public class Day14 extends AOCSolution {
@Override
protected void runPartOne(final Scanner in) {
run(in);
run(
in,
1,
(dish, cycle) -> {
tiltNorth(dish);
return 0;
});
}

@Override
protected void runPartTwo(final Scanner in) {
run(in);
final Map<String, Integer> cache = new HashMap<>();

run(
in,
1_000_000_000,
(dish, cycle) -> {
tiltNorth(dish);
tiltWest(dish);
tiltSouth(dish);
tiltEast(dish);

String key = dish.stream().map(String::new).collect(Collectors.joining());
if (cache.containsKey(key)) {
return cycle - cache.get(key);
}

cache.put(key, cycle);
return 0;
});
}

private void run(
final Scanner in,
final int cycles,
final BiFunction<List<char[]>, Integer, Integer> dishCycle) {
final List<char[]> dish = new ArrayList<>();

while (in.hasNextLine()) {
dish.add(in.nextLine().toCharArray());
}

int cycle = 0;
while (cycle < cycles) {
final int cycleLength = dishCycle.apply(dish, cycle);

if (cycleLength != 0) {
cycle += ((cycles - cycle) / cycleLength) * cycleLength;
}

++cycle;
}

print(calcLoad(dish));
}

private long calcLoad(final List<char[]> dish) {
long load = 0;

for (int rowIndex = 0; rowIndex < dish.size(); ++rowIndex) {
final char[] row = dish.get(rowIndex);

for (final char mirror : row) {
if (mirror == 'O') {
load += (dish.size() - rowIndex);
}
}
}

return load;
}

private void tiltNorth(final List<char[]> dish) {
for (int rowIndex = 0; rowIndex < dish.size(); ++rowIndex) {
final char[] row = dish.get(rowIndex);

for (int columnIndex = 0; columnIndex < row.length; ++columnIndex) {
if (row[columnIndex] == 'O') {
int rowAbove = rowIndex;

while (rowAbove > 0 && dish.get(rowAbove - 1)[columnIndex] == '.') {
--rowAbove;
}

if (rowIndex != rowAbove) {
dish.get(rowAbove)[columnIndex] = 'O';
row[columnIndex] = '.';
}
}
}
}
}

private void tiltSouth(final List<char[]> dish) {
for (int rowIndex = dish.size() - 1; rowIndex >= 0; --rowIndex) {
final char[] row = dish.get(rowIndex);

for (int columnIndex = 0; columnIndex < row.length; ++columnIndex) {
if (row[columnIndex] == 'O') {
int rowBelow = rowIndex;

while (rowBelow < dish.size() - 1 && dish.get(rowBelow + 1)[columnIndex] == '.') {
++rowBelow;
}

if (rowIndex != rowBelow) {
dish.get(rowBelow)[columnIndex] = 'O';
row[columnIndex] = '.';
}
}
}
}
}

private void tiltWest(final List<char[]> dish) {
for (int columnIndex = 0; columnIndex < dish.getFirst().length; ++columnIndex) {
for (int rowIndex = 0; rowIndex < dish.size(); ++rowIndex) {
if (dish.get(rowIndex)[columnIndex] == 'O') {
int columnToLeft = columnIndex;

while (columnToLeft > 0 && dish.get(rowIndex)[columnToLeft - 1] == '.') {
--columnToLeft;
}

if (columnIndex != columnToLeft) {
dish.get(rowIndex)[columnToLeft] = 'O';
dish.get(rowIndex)[columnIndex] = '.';
}
}
}
}
}

private void run(final Scanner in) {}
private void tiltEast(final List<char[]> dish) {
for (int columnIndex = dish.getFirst().length - 1; columnIndex >= 0; --columnIndex) {
for (int rowIndex = 0; rowIndex < dish.size(); ++rowIndex) {
if (dish.get(rowIndex)[columnIndex] == 'O') {
int columnToRight = columnIndex;

while (columnToRight < dish.getFirst().length - 1
&& dish.get(rowIndex)[columnToRight + 1] == '.') {
++columnToRight;
}

if (columnIndex != columnToRight) {
dish.get(rowIndex)[columnToRight] = 'O';
dish.get(rowIndex)[columnIndex] = '.';
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package org.ck.adventofcode.year2023;

import org.ck.adventofcode.util.BaseAOCTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class Day14Test extends BaseAOCTest {
@ParameterizedTest
@ValueSource(strings = {"01", "01a"})
@Disabled
void testOne(String name) throws Exception {
runTest(new Day14()::partOne, "day14/%s".formatted(name));
}

@ParameterizedTest
@ValueSource(strings = {"02", "02a"})
@Disabled
void testTwo(String name) throws Exception {
runTest(new Day14()::partTwo, "day14/%s".formatted(name));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
113456
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
O.O#....O.O...O..OO#OO##..O..##...#..........OO..#.OO...................O..O##.#OOO...O#.#.#.O.O#OO.
.O..OO..O.O..O......OO.#....#O#.#.#.#.#.O.OO...#.O#.OOO.#.#.#.OO..#.#O...O.O.O.O#.#.O.O....#..O#O...
O....#..O..O.OO..#OO.O.....O#OOO..#.#......O...#..#..O......#O.#.O.#.O...#.#.#.........O#.......#OO.
......O.O##......###OO#..O#.#...O...O.....##.........#.OO.#........#OO.O....###...#.##..O........O.#
#.OO.....OOO....O.....OO.OOO..#O.OO...#...O......OO###........O..#.#.O..O......O#...O.....#.#...##..
#..O.#..OO.OO.O.OOO#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.#.#..#.OO##O.
O.O..O..#......#....#O....O..O#.OOO..#..O#.OO#.........O..##...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.O..#...#..O#OOO..#.O.#O#OOO....#..OOO.#O..O..#.OOOO.........O#..O..O...O.##..O......#..
##..#..#.....O..OO###.........##..##..OO...#..#O.#......#O...O.....#........#....OO.#OO.#.O....#.O#O
.O.....O.O.O....#OO..O.##OO#..O.....O..#..O..#....O..O#....O..#.OO..O....OO..O.O..O#.....O.O.#O..OO.
O#O.##O#..................O.OO...#...#..#O....O..O..OO#.O#...##.O...##.OO#.....O.#.#...##..O.OO..O..
O.....OO..OO..#OO#.....O.O.#....OO.#...###.......O#...##....##.O..##......#......O#O..OO.OO....O....
.......O.O.#..O..O.#.O..O.O#O.#....##.OO.#...#.OO#..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..#O...........##..OO##...#...#..O#O.........O..#..#O
O...O...O..#..#OO.....OOO.#.........O..O.......#.#.#...OO##..O...O.....O#.#..O........#.#.O.O..##.#.
.###...#.....#..#O.O.......O.....O#.OO....##.OO.........O#...OO.OO#.#....#O#OO.O..#.OOOOO.....O...O.
O#.#..#..OO...O..OO.#...#..........#OO#.#..O.#...##O...#..O.O....#.O....#..##.#.#.#O.#...#..#...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....O.....#OOO.O.......O#.OO..O.....#O...O#...O...O......#.#.#....#..#OO.#OOO...#
.O.....O..#O.##OO..#OOOO..#O.O.#O....OO#OOO....O....OO...O........#..O...O...O....#.O#O.O.#O.#.O.O..
........O.O.......O...OO...###..OO.O.....OO.O..#...#OO..#....O#......##.........#.#...#.O.O....#....
.O.....##..O..#OO#OO.##...#O..#.OO..#..O.#O......#O..............O.....O.O.#O.O..#O.O...O.OO.O#..#O.
O..#.#......O#..OO..#......O.O.OOO.O...O.#....#....O#...O....##..#.....O..#....O#OOO....O.O..O......
.....O.O.....O.#..O...#O#.O..O#O..#O..##...O.#..O........O.#.O.O......OO.#....O.#.#.O..O....O#....#.
..#......#OO.O.#.#..OO...#.#..O.#....O#.#O.OOO....O###......#....O.##..O..#.O.#.#.O......O#.OO....#O
#..O...OOO..O....OO.O..#..O.OO.#OO.OOO#....OO#......OO.O....O.OO..##.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...#.##O.#.#.O.O..#..O#.O.OO.#.O#.O.OO.#.O..#......#...#..O##O..
.#.#..O...#..O.O.O.O#O..###O.O..#O..##..#.#...#OO.........O#.....##....#O..#...O...#...#...OO...O..O
..OOO..O..#OOO.O.#..#......#....OO.........O.#.O...O#......O.O...##.#..#.OO#O.....O......#........#.
O.#O....O..O..O.......O..........#..OO#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#....OOO...O#...#.....OO##..#.O.O.O..O...OOO....#.#...........OO.O#.O.#O..#..O#O#O....#..#.O.....
....#.#O.#OO..#.....#.OO.#.O..O.O.###O.O..#.#O...O..#...#..O......O...#....#..OO..........##........
......O.O..O.O..O.#.......OO.OO..##.O...........O#OO.O#.#OO.OO.......#.OO#..###O.O.........#.#O#.O.#
.OOO..#.O#O....O..#.......O...#..#.....OO...O............#O...OO........#O##.#.O.#.#.O....O#.O.OOOO.
..O..O.###OO..O.........OO.OO...#O..#...#OO.#..OO....O..O.O...O.O.....OO........#.##..#.......OO....
..OO..#O...O.O..O...O.O.O....#.O##....#..O.O..OO..#.O#..O........O.#..#......OO#O.O.O..#......##....
...O.....OO..O.OOO........O.#..OO...#........#.#...O.##O.O.O.#.#O.O...O.OOO..OO......O...#.#..O#O#..
O#...OO...O...O....O..#....#O..#...........O..........O.O..##....O..#OO.O.#........#.OO..##....OO..O
...#....#.....OOOO..#O..#...O...OO.#...OO#...##..#.#....O...O#.OO...##..O.O#...O.....#..O.OO...O..O.
#..#.O.##.O..O.O...OOO.......###..#...O.OO....#O#.....#O#O.OO..O.....#.##.#..O.#...O.OOO...O#O..O...
.OO#.......O.....##.##..#...##O..OO#O.OO.O..O...O...OO#O...#..#...O...#..#...#O..#......OO.O.....O..
O....#..OO.#...#......O.......O..OO#..O##....OO.#.#.#......#O..OO.O..O...#..#.O..O.OO##.O....O#..O..
#...#.#..#....OO...O.#..OO..O..#..#..#.O.O....#.O.O.OO.O.#.O..#.....#.#..O...#.#...#O...OO.O#.O....O
...O.#.O.....#O.OO..O..##....#O...OO........#..O..#O...O...#...#..O..O....OO..#.O.O..OO...OO...OOO#.
........#...#.##O.#..#..#......O...........O..O#.O..OO..#..O.O..#OO#......O...#.....#.##O..#....O...
.......OO#...O.O..OOO.O#O..#..#...O.O..#..#O.O.O.#..........#..#.OO...#OOOO..OO#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....#..
#....OOO....O...OO#..#..O......O#...O..O#.##.#O...O.O#..........O.#.##...........#O....OO.O#O...#.##
.#......OO.O.OO#.#.#O.#O.......O..O..#......OO..#.O.O......O..O.O.O.......#O..OO....O..#.......OO.#.
...####OO...O..O..#.....#..O....O........##.O....#...#.O.OO##..#......OO.O.#....##...#..O...O.O..O#.
.....O....O.#..O.OO##...OOO#....#O#.....O#...O.O.#.O...OOO#.O.......O......OO.#..............O.#O#..
...........O..#...#OOOO....OO.##....O.O.....O..O....O...O#.....#.O.O..O....O#..#..........#O.O....O.
.#O.O...#..#......O...........#.O.....##O...OO.O.OOO..OO...O.OO#.....OO#.OO.....#O#..#O#.O.O.#..#O..
#...O..#..#.O.....O.O.#.#..#...O#..#..#O..#.....OO#.#.OO#.....O#...O.OO.............O#OOO..O.O..O.O.
.......O#O.O.#....#...........O.............O..#..OO#......OO.O...O..........OO.#..OO#O#.O##O#..O.O.
#.#......O.....OOOOO.O#O#...#.O....#...#..O#.#O....O.O#OO....#..O.O.#.#...##.OO.#.OO#.###O.....#..O.
OO...O.#............#.#.#...#..OO......#..O.#......##..OO....OO.#.OO...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....#.O#....#.O.#..........#.O.O.OOOO.......###...O....O......O....
...O..#...#..O.#O......#O#..O.....O....O..#...##.OOOO.OOOO#..O.#..#.......#.#O#..#..#.OO#..#.#....O#
..OO.......O#.OOOO....O..#...O.#O#....##.........#O#OO#O#..#.##.......O.O......#.....#..O.O.#O.....#
#.O........O..O.#..#.O#O...#O#........O#.O#O..O.............#OO#..O#OOO..#O###..OOO...#.O........##.
....OO.......#.O..O.O........O#.#..O.O..OO.....O....O...O#.#....O.#O#.........#....OO#O#.#OO...#.#O.
.O.O..O.......#.OO..#...O.O#.O.#.O......#.O...OOO......O.O.O.#.OO#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.OO##O..#..OO#.OO..#OO
...#.O.......O.#.O..O.O.......O...##O...OO.#....O..O.........O.O...#.O..OO...O.O.O....O...#..#.#.OO.
OO.#...OO.#.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.##...#O...O.#..#..O..O..#...........O...OO.O...O#.........OO#
...O..OO....##.##O..O.##......#O.O#.OO..O#..OOO.OO.##OOO.#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.O#....O.O#..#O..O#
.#...O.###....#.......O....#...OO.....O.#.#.#.....O.#.......O..#.##.O..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...#OO#....#O.O#......O..O#.#
O...O...OO.##.#.OO#.#OOO.#.O.O.##.O#....#..O.O#O.#..#.....OO..#O...O.O.........O...#.O...O.#...O..OO
..#...OO.#..##.O.O..O.O...O##....#.O..O.OO#...OO.O.....O...#.O.##OO.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...#.##....OO.O...#........#...#..O.O#....#.##.#.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.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..OO#......O#OO..#....O....OOO.O.O....#..O.#...O#........O...O.#.#O...
.O....O....OO...O......##...#O.....#...O......O....O.##OO.......#....O....O....O.O...##O...#.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....OO.#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.#...OO#.....#....O..##...O..##O......OO.O#..........OO.
O....O...O#O...#.#O...#.OO...O.....#O#.O..OOO#.OO.....#...#O#....O#..O##..O.O.#O#O###.OO....O...O#..
#...O..O##..#O.O#OO....#O...#.O#...##...O#......O.#...#....O#....O.....OOO#....O..#O#.O#O....O.#O...
#.O....##O.#.OOO..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.#......#.#...#O........O.....O.#..##..O#.##...#OO...
.O.#...#.O.O......OOOOO..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.#O#..O...O.O.............O.....#..OOO...#O#..O...#..##...O...O........#....#..O.O...##.OO..##
..OO#.O..O.....O...O.##...OO.O...O....#..O.#O.......#.#O#.OO.......#.#...O..#......O..##..##.O..O#O.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
136
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..#....
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
118747
Loading

0 comments on commit 2e00ebe

Please sign in to comment.