Skip to content

Commit 06264d9

Browse files
committed
day16 part 1 start
1 parent b769e5f commit 06264d9

File tree

6 files changed

+340
-26
lines changed

6 files changed

+340
-26
lines changed

build.gradle

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
plugins {
2-
id 'java'
3-
id 'org.springframework.boot' version '3.2.0'
4-
id 'io.spring.dependency-management' version '1.1.4'
5-
id 'org.graalvm.buildtools.native' version '0.9.28'
6-
id 'com.intershop.gradle.jaxb' version "6.0.0"
2+
id 'java'
3+
id 'org.springframework.boot' version '3.3.5'
4+
id 'io.spring.dependency-management' version '1.1.6'
75
}
86

97
group = 'net.leibi'
108
version = '0.0.1-SNAPSHOT'
11-
129
java {
13-
sourceCompatibility = '21'
10+
toolchain {
11+
languageVersion = JavaLanguageVersion.of(23)
12+
}
1413
}
1514

15+
1616
configurations {
17-
compileOnly {
18-
extendsFrom annotationProcessor
19-
}
17+
compileOnly {
18+
extendsFrom annotationProcessor
19+
}
2020
}
2121

2222
repositories {
23-
mavenCentral()
23+
mavenCentral()
2424
}
2525

2626
dependencies {
2727

28-
implementation 'org.springframework.boot:spring-boot-starter'
29-
implementation 'com.google.guava:guava:31.1-jre'
30-
compileOnly 'org.projectlombok:lombok'
31-
annotationProcessor 'org.projectlombok:lombok'
32-
testImplementation 'org.springframework.boot:spring-boot-starter-test'
28+
implementation 'org.springframework.boot:spring-boot-starter'
29+
implementation 'com.google.guava:guava:31.1-jre'
30+
compileOnly 'org.projectlombok:lombok'
31+
annotationProcessor 'org.projectlombok:lombok'
32+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
3333
}
3434

3535
test {
36-
useJUnitPlatform()
37-
minHeapSize = "8g"
38-
maxHeapSize = "64g"
36+
useJUnitPlatform()
37+
minHeapSize = "8g"
38+
maxHeapSize = "64g"
39+
jvmArgs(['--enable-preview'])
40+
}
41+
42+
43+
tasks.withType(JavaCompile).each {
44+
it.options.compilerArgs.add('--enable-preview')
3945
}

src/main/java/net/leibi/adventofcode2021/day8/Display.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package net.leibi.adventofcode2021.day8;
22

3-
import static java.util.Objects.isNull;
3+
import lombok.Getter;
4+
import lombok.extern.slf4j.Slf4j;
45

5-
import java.rmi.UnexpectedException;
66
import java.util.ArrayList;
77
import java.util.Arrays;
88
import java.util.HashMap;
@@ -11,9 +11,8 @@
1111
import java.util.Map.Entry;
1212
import java.util.Objects;
1313
import java.util.stream.Collectors;
14-
import lombok.Getter;
15-
import lombok.SneakyThrows;
16-
import lombok.extern.slf4j.Slf4j;
14+
15+
import static java.util.Objects.isNull;
1716

1817
@Getter
1918
@Slf4j
@@ -197,7 +196,6 @@ private String[] convertPattern(String[] inputPattern) {
197196
return result;
198197
}
199198

200-
@SneakyThrows
201199
private String getCharForIndex(int i) {
202200
return switch (i) {
203201
case 0 -> "a";
@@ -207,7 +205,7 @@ private String getCharForIndex(int i) {
207205
case 4 -> "e";
208206
case 5 -> "f";
209207
case 6 -> "g";
210-
default -> throw new UnexpectedException(String.valueOf(i));
208+
default -> "";
211209
};
212210
}
213211
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package net.leibi.adventofcode2023.day16;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.leibi.helpers.InputHelper;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
10+
@Slf4j
11+
public class Day16 {
12+
13+
public static final int ANSWERDAY1SMALL = 46;
14+
private static final Map<MovingDecisionKey, MovingDirection> movingDirectionsMap = getMovingDirectionsMap();
15+
private static char[][] charMatrixFromInput;
16+
private static char[][] energizedCharMatrix;
17+
18+
public static long day1(String input) {
19+
20+
charMatrixFromInput = InputHelper.getCharMatrixFromInput(input);
21+
InputHelper.printCharArray(charMatrixFromInput);
22+
var width = charMatrixFromInput.length;
23+
var hight = charMatrixFromInput[0].length;
24+
25+
log.info("The array is {} by {}", width, hight);
26+
27+
energizedCharMatrix = new char[width][hight];
28+
29+
final var start = new Point(0, 0);
30+
followBeam(new DirectedPoint(start, MovingDirection.RIGHT));
31+
32+
int sum = 0;
33+
for (char[] charMatrix : energizedCharMatrix) {
34+
for (char element : charMatrix) {
35+
if (element == '#') sum++;
36+
}
37+
}
38+
InputHelper.printCharArray(energizedCharMatrix);
39+
log.info("sum: {}", sum);
40+
41+
42+
return ANSWERDAY1SMALL;
43+
}
44+
45+
static Map<MovingDecisionKey, MovingDirection> getMovingDirectionsMap() {
46+
Map<MovingDecisionKey, MovingDirection> movingDirectionsMap = new HashMap<>();
47+
48+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '|'), MovingDirection.UPANDDOWN);
49+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '|'), MovingDirection.UPANDDOWN);
50+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '|'), MovingDirection.UP);
51+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '|'), MovingDirection.DOWN);
52+
53+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '/'), MovingDirection.UP);
54+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '/'), MovingDirection.DOWN);
55+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '/'), MovingDirection.RIGHT);
56+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '/'), MovingDirection.LEFT);
57+
58+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '\\'), MovingDirection.DOWN);
59+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '\\'), MovingDirection.UP);
60+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '\\'), MovingDirection.LEFT);
61+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '\\'), MovingDirection.RIGHT);
62+
63+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.RIGHT, '-'), MovingDirection.RIGHT);
64+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.LEFT, '-'), MovingDirection.LEFT);
65+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.UP, '-'), MovingDirection.LEFTANDRIGHT);
66+
movingDirectionsMap.put(new MovingDecisionKey(MovingDirection.DOWN, '-'), MovingDirection.LEFTANDRIGHT);
67+
return movingDirectionsMap;
68+
}
69+
70+
static MovingDirection getMovingDirection(MovingDirection direction, Character device) {
71+
log.info("Getting moving direction on device {} with direction {}", device, direction);
72+
return switch (device) {
73+
case '.' -> direction;
74+
default -> movingDirectionsMap.get(new MovingDecisionKey(direction, device));
75+
};
76+
}
77+
78+
private static void followBeam(DirectedPoint directedPoint) {
79+
if (directedPoint == null || directedPoint.point == null) return;
80+
log.info("Following beam: {}", directedPoint);
81+
var nextMovingDirection = getNewMovingDirection(directedPoint);
82+
final var nextPoints = directedPoint.point.getNextPoint(nextMovingDirection);
83+
84+
followBeam(nextPoints.p1);
85+
followBeam(nextPoints.p2);
86+
87+
}
88+
89+
private static MovingDirection getNewMovingDirection(DirectedPoint directedPoint) {
90+
if (directedPoint.point == null) return directedPoint.movingDirection;
91+
var device = getDevice(directedPoint);
92+
return getMovingDirection(directedPoint.movingDirection, device);
93+
}
94+
95+
private static char getDevice(DirectedPoint directedPoint) {
96+
energizedCharMatrix[directedPoint.point.x][directedPoint.point.y] = '#';
97+
return charMatrixFromInput[directedPoint.point.x][directedPoint.point.y];
98+
}
99+
100+
enum MovingDirection {
101+
RIGHT,
102+
LEFT,
103+
UP,
104+
DOWN,
105+
LEFTANDRIGHT,
106+
UPANDDOWN
107+
}
108+
109+
record MovingDecisionKey(MovingDirection movingDirection, Character Device) {
110+
}
111+
112+
record Point(int x, int y) {
113+
Point {
114+
if (x < 0 || y < 0 || x >= charMatrixFromInput.length || y >= charMatrixFromInput[0].length) {
115+
throw new IllegalArgumentException();
116+
}
117+
}
118+
119+
NextDirections getNextPoint(MovingDirection movingDirection) {
120+
return switch (movingDirection) {
121+
case RIGHT -> new NextDirections(new DirectedPoint(x , y+1, MovingDirection.RIGHT), null);
122+
case LEFT -> new NextDirections(new DirectedPoint(x , y-1, MovingDirection.LEFT), null);
123+
case UP -> new NextDirections(new DirectedPoint(x-1, y , MovingDirection.UP), null);
124+
case DOWN -> new NextDirections(new DirectedPoint(x+1, y , MovingDirection.DOWN), null);
125+
case UPANDDOWN ->
126+
new NextDirections(new DirectedPoint(x+1, y , MovingDirection.DOWN), new DirectedPoint(x-1, y , MovingDirection.UP));
127+
case LEFTANDRIGHT ->
128+
new NextDirections(new DirectedPoint(x , y+1, MovingDirection.LEFT), new DirectedPoint(x , y+1, MovingDirection.RIGHT));
129+
};
130+
}
131+
132+
133+
}
134+
135+
record NextDirections(DirectedPoint p1, DirectedPoint p2) {
136+
}
137+
138+
record DirectedPoint(Point point, MovingDirection movingDirection) {
139+
public DirectedPoint(int x, int y, MovingDirection movingDirection) {
140+
Point p = null;
141+
try {
142+
p = new Point(x, y);
143+
} catch (IllegalArgumentException e) {
144+
log.info("Point hitting a wall: {},{}", x,y);
145+
}
146+
this(p, movingDirection);
147+
}
148+
}
149+
150+
}
151+
152+
153+

0 commit comments

Comments
 (0)