Skip to content

Instantly share code, notes, and snippets.

@msx80
Created December 11, 2020 10:20
Show Gist options
  • Save msx80/2b30bcb31a5a362d60e4ecdb19228a85 to your computer and use it in GitHub Desktop.
Save msx80/2b30bcb31a5a362d60e4ecdb19228a85 to your computer and use it in GitHub Desktop.
package aoc;
import java.awt.Point;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
public class Day11 {
enum Dirs {
N(0,-1), NE(+1,-1), E(+1,0), SE(+1,+1), S(0,+1), SW(-1,+1), W(-1, 0), NW(-1,-1);
int dx;
int dy;
private Dirs(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public Point move(Point p)
{
return new Point(p.x+dx, p.y+dy);
}
};
static int w;
static int h;
static boolean isInside(Point p)
{
return p.x>=0 && p.y>=0 && p.x<w && p.y<h;
}
static int countOccupied1(char[][] map, Point p)
{
return (int) Stream
.of(Dirs.values())
.map(d -> d.move(p))
.filter(Day11::isInside)
.filter(a -> map[a.y][a.x]=='#')
.count();
}
static char find(char[][] map, Point p, Dirs dir)
{
while(true) {
p = dir.move(p);
if(!isInside(p)) return '.';
char c = map[p.y][p.x];
if(c != '.') return c;
}
}
static int countOccupied2(char[][] map, Point p)
{
return (int) Stream
.of(Dirs.values())
.map(d -> find(map, p, d))
.filter(a -> a=='#')
.count();
}
public static boolean equal(final char[][] arr1, final char[][] arr2) {
for (int i = 0; i < arr1.length; i++) {
if (!Arrays.equals(arr1[i], arr2[i])) {
return false;
}
}
return true;
}
public static void main(String[] args) throws IOException {
part1();
part2();
}
private static void part1() throws IOException {
char[][] src = Files.readAllLines(Paths.get("input11.txt")).stream().map(m -> m.toCharArray()).toArray(char[][]::new);
char[][] dst = Files.readAllLines(Paths.get("input11.txt")).stream().map(m -> m.toCharArray()).toArray(char[][]::new);
w = src[0].length;
h = src.length;
while(true)
{
step1(src, dst);
if(equal(src,dst))
{
System.out.println(countAllOccupied(dst));
return;
}
else
{
var s = src; src = dst; dst = s;
}
}
}
private static void part2() throws IOException {
char[][] src = Files.readAllLines(Paths.get("input11.txt")).stream().map(m -> m.toCharArray()).toArray(char[][]::new);
char[][] dst = Files.readAllLines(Paths.get("input11.txt")).stream().map(m -> m.toCharArray()).toArray(char[][]::new);
w = src[0].length;
h = src.length;
while(true)
{
step2(src, dst);
if(equal(src,dst))
{
System.out.println(countAllOccupied(dst));
return;
}
else
{
var s = src; src = dst; dst = s;
}
}
}
private static int countAllOccupied(char[][] map) {
int o = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if(map[y][x]=='#') o++;
}
}
return o;
}
static void step1(char[][] src, char[][] dst) {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if(src[y][x]!='.') {
int o = countOccupied1(src, new Point( x, y ));
switch (src[y][x]) {
case 'L':
dst[y][x] = (o == 0 ? '#' : 'L');
break;
case '#':
dst[y][x] = (o >= 4 ? 'L' : '#');
break;
}
}
}
}
}
static void step2(char[][] src, char[][] dst) {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if(src[y][x]!='.') {
int o = countOccupied2(src, new Point( x, y ));
switch (src[y][x]) {
case 'L':
dst[y][x] = (o == 0 ? '#' : 'L');
break;
case '#':
dst[y][x] = (o >= 5 ? 'L' : '#');
break;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment