Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13c393d

Browse files
Solved Day 18 (2015) - Like a GIF For Your Yard
1 parent de686cb commit 13c393d

File tree

1 file changed

+85
-17
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+85
-17
lines changed
Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,91 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
47

58
public class Day18 extends Day2015 {
6-
public Day18() {
7-
super(18);
8-
}
9-
10-
public static void main(String[] args) {
11-
new Day18().printParts();
12-
}
13-
14-
@Override
15-
public Object part1() {
16-
return "";
17-
}
18-
19-
@Override
20-
public Object part2() {
21-
return "";
22-
}
9+
10+
private static final int GRID_SIZE = 100;
11+
private static final int STEPS = 100;
12+
private boolean[][] grid;
13+
14+
public Day18() {
15+
super(18);
16+
parseInput();
17+
}
18+
19+
public static void main(String[] args) {
20+
Day18 day = new Day18();
21+
day.printParts();
22+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 18, 1);
23+
}
24+
25+
private void parseInput() {
26+
grid = new boolean[GRID_SIZE][GRID_SIZE];
27+
String[] lines = day().split("\n");
28+
for (int i = 0; i < GRID_SIZE; i++) {
29+
String line = lines[i].trim();
30+
for (int j = 0; j < GRID_SIZE; j++) {
31+
grid[i][j] = line.charAt(j) == '#';
32+
}
33+
}
34+
}
35+
36+
@Override
37+
public Object part1() {
38+
boolean[][] currentGrid = copyGrid(grid);
39+
for (int step = 0; step < STEPS; step++) {
40+
currentGrid = nextStep(currentGrid);
41+
}
42+
return countLights(currentGrid);
43+
}
44+
45+
@Override
46+
public Object part2() {
47+
return 0; // Implement in next part
48+
}
49+
50+
private boolean[][] nextStep(boolean[][] currentGrid) {
51+
boolean[][] newGrid = new boolean[GRID_SIZE][GRID_SIZE];
52+
53+
for (int i = 0; i < GRID_SIZE; i++) {
54+
for (int j = 0; j < GRID_SIZE; j++) {
55+
int neighbors = countNeighbors(currentGrid, i, j);
56+
if (currentGrid[i][j]) {
57+
newGrid[i][j] = neighbors == 2 || neighbors == 3;
58+
} else {
59+
newGrid[i][j] = neighbors == 3;
60+
}
61+
}
62+
}
63+
64+
return newGrid;
65+
}
66+
67+
private int countNeighbors(boolean[][] grid, int row, int col) {
68+
return (int) IntStream.rangeClosed(row - 1, row + 1)
69+
.filter(r -> r >= 0 && r < GRID_SIZE)
70+
.flatMap(r -> IntStream.rangeClosed(col - 1, col + 1)
71+
.filter(c -> c >= 0 && c < GRID_SIZE)
72+
.filter(c -> !(r == row && c == col))
73+
.filter(c -> grid[r][c]))
74+
.count();
75+
}
76+
77+
private boolean[][] copyGrid(boolean[][] original) {
78+
boolean[][] copy = new boolean[GRID_SIZE][GRID_SIZE];
79+
for (int i = 0; i < GRID_SIZE; i++) {
80+
copy[i] = Arrays.copyOf(original[i], GRID_SIZE);
81+
}
82+
return copy;
83+
}
84+
85+
private long countLights(boolean[][] grid) {
86+
return Arrays.stream(grid)
87+
.flatMap(row -> IntStream.range(0, row.length).mapToObj(i -> row[i]))
88+
.filter(light -> light)
89+
.count();
90+
}
2391
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /