Skip to content

Commit 4a09c56

Browse files
committed
Solved day 6 part 2
1 parent 3daa772 commit 4a09c56

File tree

10 files changed

+60
-20
lines changed

10 files changed

+60
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
4444
| 23 |||
4545
| 24 |||
4646
| 25 |||
47-
| **SUM** | **6 ⭐** | **5** |
47+
| **SUM** | **6 ⭐** | **6** |
4848

49-
Total: 11
49+
Total: 12

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.5.3'
7+
version '0.5.4'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day06/src/main/java/de/havox_design/aoc2015/day06/HouseDecoratingContest.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class HouseDecoratingContest {
1212

1313
private final List<String> input;
1414
private final boolean[][] houseDecoration = new boolean[1000][1000];
15+
private final int[][] brightness = new int[1000][1000];
1516

1617
public HouseDecoratingContest(String fileName) {
1718
input = readData(fileName);
@@ -49,7 +50,8 @@ public int solvePart1() {
4950
}
5051

5152
public int solvePart2() {
52-
return 0;
53+
solvePart1();
54+
return countBrightness();
5355
}
5456

5557
private int countNumberOfLitLights() {
@@ -66,6 +68,18 @@ private int countNumberOfLitLights() {
6668
return numberOfLightsLit;
6769
}
6870

71+
private int countBrightness() {
72+
int currentBrightness = 0;
73+
74+
for(int x = 0; x < 1000; x++) {
75+
for(int y = 0; y < 1000; y++) {
76+
currentBrightness += brightness[x][y];
77+
}
78+
}
79+
80+
return currentBrightness;
81+
}
82+
6983
private int[] parseInstruction(String instruction, int offset) {
7084
int[] values = new int[4];
7185
String data = instruction.substring(offset);
@@ -81,24 +95,16 @@ private int[] parseInstruction(String instruction, int offset) {
8195
return values;
8296
}
8397

84-
private void turnOnLight(int x, int y) {
85-
setLight(x, y, true);
86-
}
87-
8898
private void turnOnLight(int fromX, int toX, int fromY, int toY) {
89-
setLight(fromX, toX, fromY, toY, true);
90-
}
91-
92-
private void turnOffLight(int x, int y) {
93-
setLight(x, y, false);
99+
setLight(fromX, toX, fromY, toY, true, 1);
94100
}
95101

96102
private void turnOffLight(int fromX, int toX, int fromY, int toY) {
97-
setLight(fromX, toX, fromY, toY, false);
103+
setLight(fromX, toX, fromY, toY, false, -1);
98104
}
99105

100106
private void toggleLight(int x, int y) {
101-
setLight(x, y, !houseDecoration[x][y]);
107+
setLight(x, y, !houseDecoration[x][y], 2);
102108
}
103109

104110
private void toggleLight(int fromX, int toX, int fromY, int toY) {
@@ -109,14 +115,19 @@ private void toggleLight(int fromX, int toX, int fromY, int toY) {
109115
}
110116
}
111117

112-
private void setLight(int x, int y, boolean value) {
118+
private void setLight(int x, int y, boolean value, int brightnessChange) {
113119
houseDecoration[x][y] = value;
120+
brightness[x][y] += brightnessChange;
121+
122+
if(brightness[x][y] < 0) {
123+
brightness[x][y] = 0;
124+
}
114125
}
115126

116-
private void setLight(int fromX, int toX, int fromY, int toY, boolean value) {
127+
private void setLight(int fromX, int toX, int fromY, int toY, boolean value, int brightnessChange) {
117128
for(int x = fromX; x <= toX; x++) {
118129
for(int y = fromY; y <= toY; y++) {
119-
setLight(x, y, value);
130+
setLight(x, y, value, brightnessChange);
120131
}
121132
}
122133
}

day06/src/test/java/de/havox_design/aoc2015/day06/Day06Test.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ void testMainClass() {
1818

1919
@ParameterizedTest
2020
@MethodSource("getDataForPart1")
21-
void testPart1(String fileName, int expectedNumberOfNiceStrings) {
22-
Assertions.assertEquals(expectedNumberOfNiceStrings, HouseDecoratingContest.solvePart1(fileName));
21+
void testPart1(String fileName, int expectedNumberOfLights) {
22+
Assertions.assertEquals(expectedNumberOfLights, HouseDecoratingContest.solvePart1(fileName));
2323
}
2424

2525
private static Stream<Arguments> getDataForPart1() {
@@ -31,4 +31,21 @@ private static Stream<Arguments> getDataForPart1() {
3131
Arguments.of("part1sample5.txt", 998996)
3232
);
3333
}
34+
35+
@ParameterizedTest
36+
@MethodSource("getDataForPart2")
37+
void testPart2(String fileName, int expectedBrightness) {
38+
Assertions.assertEquals(expectedBrightness, HouseDecoratingContest.solvePart2(fileName));
39+
}
40+
41+
private static Stream<Arguments> getDataForPart2() {
42+
return Stream.of(
43+
Arguments.of("part2sample1.txt", 1),
44+
Arguments.of("part2sample2.txt", 0),
45+
Arguments.of("part2sample3.txt", 2),
46+
Arguments.of("part2sample4.txt", 1),
47+
Arguments.of("part2sample5.txt", 2000000),
48+
Arguments.of("part2sample6.txt", 2000003)
49+
);
50+
}
3451
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
turn on 0,0 through 0,0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
turn off 0,0 through 0,0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
toggle 0,0 through 0,0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
toggle 0,0 through 0,0
2+
turn off 0,0 through 0,0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
toggle 0,0 through 999,999
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
turn on 0,0 through 0,0
2+
turn off 0,0 through 0,0
3+
toggle 0,0 through 0,0
4+
toggle 0,0 through 0,0
5+
turn off 0,0 through 0,0
6+
toggle 0,0 through 999,999

0 commit comments

Comments
 (0)