-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18.java
98 lines (86 loc) · 3.91 KB
/
Day18.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//package org.mrbadaxe.AdventOfCode2022;
import java.util.List;
import java.util.ArrayList;
public class Day18{
private static int CUBE_EDGE = 20;
public static int getPart01(List<String> input){
List<TriPoint> droplets = new ArrayList<TriPoint>();
for(String line : input){
String[] split = line.split(",");
droplets.add(new TriPoint(Integer.parseInt(split[0]),Integer.parseInt(split[1]),Integer.parseInt(split[2])));
}
int maxSurfaceArea = 6 * droplets.size();
for(TriPoint droplet : droplets){
if(droplets.contains(new TriPoint(droplet.getX()-1,droplet.getY(),droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX()+1,droplet.getY(),droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY()-1,droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY()+1,droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY(),droplet.getZ()-1))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY(),droplet.getZ()+1))){ maxSurfaceArea--; }
}
return maxSurfaceArea;
}
public static int getPart02(List<String> input){
List<TriPoint> droplets = new ArrayList<TriPoint>();
for(String line : input){
String[] split = line.split(",");
droplets.add(new TriPoint(Integer.parseInt(split[0]),Integer.parseInt(split[1]),Integer.parseInt(split[2])));
}
List<TriPoint> steam = new ArrayList<TriPoint>();
steam.add(new TriPoint(0,0,0));
int newSteamCount = 1;
int steamCount = steam.size();
while(newSteamCount > 0){
//System.out.println(steam.size() + ":" + steam.toString());
List<TriPoint> newSteam = new ArrayList<TriPoint>();
for(TriPoint t : steam){
int x = t.getX();
int y = t.getY();
int z = t.getZ();
List<TriPoint> neighbors = new ArrayList<TriPoint>();
if(x > 0) { neighbors.add(new TriPoint(x-1,y,z)); }
if(x < CUBE_EDGE-1) { neighbors.add(new TriPoint(x+1,y,z)); }
if(y > 0) { neighbors.add(new TriPoint(x,y-1,z)); }
if(y < CUBE_EDGE-1) { neighbors.add(new TriPoint(x,y+1,z)); }
if(z > 0) { neighbors.add(new TriPoint(x,y,z-1)); }
if(z < CUBE_EDGE-1) { neighbors.add(new TriPoint(x,y,z+1)); }
for(TriPoint n : neighbors){
if(!droplets.contains(n) && !steam.contains(n)){
newSteam.add(n);
}
}
}
newSteamCount = newSteam.size();
//System.out.println(newSteamCount + ":" + newSteam.toString());
for(TriPoint t : newSteam){
if(!steam.contains(t)){
steam.add(t);
}
}
}
System.out.println(droplets.size());
System.out.println(steam.size());
for(int x=0;x<CUBE_EDGE;x++){
for(int y=0;y<CUBE_EDGE;y++){
for(int z=0;z<CUBE_EDGE;z++){
TriPoint t = new TriPoint(x,y,z);
if(!steam.contains(t)){
if(!droplets.contains(t)){
droplets.add(t);
}
}
}
}
}
int maxSurfaceArea = 6 * droplets.size();
for(TriPoint droplet : droplets){
if(droplets.contains(new TriPoint(droplet.getX()-1,droplet.getY(),droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX()+1,droplet.getY(),droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY()-1,droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY()+1,droplet.getZ()))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY(),droplet.getZ()-1))){ maxSurfaceArea--; }
if(droplets.contains(new TriPoint(droplet.getX(),droplet.getY(),droplet.getZ()+1))){ maxSurfaceArea--; }
}
return maxSurfaceArea;
}
}