Skip to content

Commit 66c2418

Browse files
committed
Solved day 6 part 1
1 parent aa6e85a commit 66c2418

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To run all solutions, simply run `./gradlew run`. If you want to run the solutio
2424
| 3 |||
2525
| 4 |||
2626
| 5 |||
27-
| 6 | ||
27+
| 6 | ||
2828
| 7 |||
2929
| 8 |||
3030
| 9 |||
@@ -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** | **5** | **5 ⭐** |
47+
| **SUM** | **6** | **5 ⭐** |
4848

49-
Total: 10
49+
Total: 11

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.2'
7+
version '0.5.3'
88

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

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

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
import java.util.List;
66

77
public class HouseDecoratingContest {
8+
private static final int FROM_X = 0;
9+
private static final int FROM_Y = 1;
10+
private static final int TO_X = 2;
11+
private static final int TO_Y = 3;
12+
813
private final List<String> input;
14+
private final boolean[][] houseDecoration = new boolean[1000][1000];
915

1016
public HouseDecoratingContest(String fileName) {
1117
input = readData(fileName);
@@ -22,13 +28,99 @@ public static int solvePart2(String fileName) {
2228
}
2329

2430
public int solvePart1() {
25-
return 0;
31+
for(String instruction : input) {
32+
if(instruction.startsWith("turn on")) {
33+
int[] values = parseInstruction(instruction,8);
34+
turnOnLight(values[FROM_X], values[TO_X], values[FROM_Y], values[TO_Y]);
35+
36+
} else if (instruction.startsWith("turn off")) {
37+
int[] values = parseInstruction(instruction,9);
38+
turnOffLight(values[FROM_X], values[TO_X], values[FROM_Y], values[TO_Y]);
39+
40+
} else if (instruction.startsWith("toggle")) {
41+
int[] values = parseInstruction(instruction,7);
42+
toggleLight(values[FROM_X], values[TO_X], values[FROM_Y], values[TO_Y]);
43+
} else {
44+
throw new IllegalArgumentException("no valid instruction: " + instruction );
45+
}
46+
}
47+
48+
return countNumberOfLitLights();
2649
}
2750

2851
public int solvePart2() {
2952
return 0;
3053
}
3154

55+
private int countNumberOfLitLights() {
56+
int numberOfLightsLit = 0;
57+
58+
for(int x = 0; x < 1000; x++) {
59+
for(int y = 0; y < 1000; y++) {
60+
if(houseDecoration[x][y]) {
61+
numberOfLightsLit++;
62+
}
63+
}
64+
}
65+
66+
return numberOfLightsLit;
67+
}
68+
69+
private int[] parseInstruction(String instruction, int offset) {
70+
int[] values = new int[4];
71+
String data = instruction.substring(offset);
72+
String[] words = data.split(" ");
73+
String[] fromWords = words[0].split(",");
74+
String[] toWords = words[2].split(",");
75+
76+
values[FROM_X] = Integer.parseInt(fromWords[0]);
77+
values[FROM_Y] = Integer.parseInt(fromWords[1]);
78+
values[TO_X] = Integer.parseInt(toWords[0]);
79+
values[TO_Y] = Integer.parseInt(toWords[1]);
80+
81+
return values;
82+
}
83+
84+
private void turnOnLight(int x, int y) {
85+
setLight(x, y, true);
86+
}
87+
88+
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);
94+
}
95+
96+
private void turnOffLight(int fromX, int toX, int fromY, int toY) {
97+
setLight(fromX, toX, fromY, toY, false);
98+
}
99+
100+
private void toggleLight(int x, int y) {
101+
setLight(x, y, !houseDecoration[x][y]);
102+
}
103+
104+
private void toggleLight(int fromX, int toX, int fromY, int toY) {
105+
for(int x = fromX; x <= toX; x++) {
106+
for(int y = fromY; y <= toY; y++) {
107+
toggleLight(x, y);
108+
}
109+
}
110+
}
111+
112+
private void setLight(int x, int y, boolean value) {
113+
houseDecoration[x][y] = value;
114+
}
115+
116+
private void setLight(int fromX, int toX, int fromY, int toY, boolean value) {
117+
for(int x = fromX; x <= toX; x++) {
118+
for(int y = fromY; y <= toY; y++) {
119+
setLight(x, y, value);
120+
}
121+
}
122+
}
123+
32124
private List<String> readData(String fileName) {
33125
return DataReader.readData(fileName, MainClass.class);
34126
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class MainClass {
77

88
public static void main(String[] args) {
99
LOGGER.info(() -> "Solution 1: " + HouseDecoratingContest.solvePart1("day06.txt"));
10-
LOGGER.info(() -> "Solution 2: " + HouseDecoratingContest.solvePart1("day06.txt"));
10+
LOGGER.info(() -> "Solution 2: " + HouseDecoratingContest.solvePart2("day06.txt"));
1111
}
1212
}

0 commit comments

Comments
 (0)