Skip to content

Commit bd6166f

Browse files
committed
feat: solve day 12 part 1
1 parent a44c395 commit bd6166f

File tree

5 files changed

+221
-12
lines changed

5 files changed

+221
-12
lines changed

src/main/java/com/adventofcode/flashk/common/Array2DUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public static char[][] transpose(char[][] array) {
2121
return transposedArray;
2222
}
2323

24+
/*
25+
public static <T> T[][] transpose(T[][] array) {
26+
int rows = array.length;
27+
int cols = array[0].length;
28+
char[][] transposedArray = new char[cols][rows];
29+
for(int row = 0; row < rows; row++) {
30+
for(int col = 0; col < cols; col++) {
31+
transposedArray[col][row] = array[row][col];
32+
}
33+
}
34+
return transposedArray;
35+
}*/
36+
2437
/**
2538
* Paints the given array.
2639
* @param map the array to paint
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.adventofcode.flashk.day12;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class GardenGroups {
11+
12+
private int rows;
13+
private int cols;
14+
private GardenPlot[][] map;
15+
private final Map<Integer, Integer> regionAreas = new HashMap<>();
16+
17+
public GardenGroups(char[][] inputs) {
18+
19+
rows = inputs.length;
20+
cols = inputs[0].length;
21+
map = new GardenPlot[rows][cols];
22+
23+
for(int row = 0; row < rows; row++){
24+
for(int col = 0; col < cols; col++) {
25+
map[row][col] = new GardenPlot(inputs[row][col], new Vector2(col, row));
26+
}
27+
}
28+
29+
}
30+
31+
public long solveA() {
32+
findRegions();
33+
return calculatePrice();
34+
}
35+
36+
public long solveB() {
37+
findRegions();
38+
// TODO calculate sides of each region
39+
// then calculate price
40+
return 0L;
41+
}
42+
43+
private void findRegions() {
44+
int regionId = 1;
45+
for(int row = 0; row < rows; row++) {
46+
for(int col = 0; col < cols; col++) {
47+
if(!map[row][col].hasRegion()) {
48+
int regionArea = findRegion(map[row][col], map[row][col].getPlant(), regionId);
49+
regionAreas.put(regionId, regionArea);
50+
regionId++;
51+
}
52+
53+
}
54+
}
55+
}
56+
57+
private int findRegion(GardenPlot gardenPlot, char plant, int regionId) {
58+
59+
if(gardenPlot.isVisited()) {
60+
return 0;
61+
}
62+
63+
if(gardenPlot.hasRegion()) {
64+
return 0;
65+
}
66+
67+
if(gardenPlot.getPlant() != plant) {
68+
return 0;
69+
}
70+
71+
int area = 1;
72+
gardenPlot.setVisited(true);
73+
gardenPlot.setRegionId(regionId);
74+
75+
Set<GardenPlot> adjacentPlots = getAdjacentPlots(gardenPlot);
76+
77+
for(GardenPlot adjacentPlot : adjacentPlots) {
78+
area += findRegion(adjacentPlot, plant, regionId);
79+
}
80+
81+
gardenPlot.setVisited(false);
82+
83+
return area;
84+
}
85+
86+
private long calculatePrice() {
87+
88+
long price = 0;
89+
90+
for(int row = 0; row < rows; row++) {
91+
92+
GardenPlot previousPlot = null;
93+
for(int col = 0; col < cols;col++) {
94+
GardenPlot plot = map[row][col];
95+
96+
if(previousPlot == null) {
97+
price += regionAreas.get(plot.getRegionId());
98+
} else if(previousPlot.getRegionId() != plot.getRegionId()) {
99+
price += regionAreas.get(previousPlot.getRegionId());
100+
price += regionAreas.get(plot.getRegionId());
101+
}
102+
103+
previousPlot = plot;
104+
}
105+
106+
// Add price of right-most fence
107+
price += regionAreas.get(previousPlot.getRegionId());
108+
109+
}
110+
111+
112+
for(int col = 0; col < cols; col++) {
113+
114+
GardenPlot previousPlot = null;
115+
for(int row = 0; row < rows; row++) {
116+
GardenPlot plot = map[row][col];
117+
118+
if(previousPlot == null) {
119+
price += regionAreas.get(plot.getRegionId());
120+
} else if(previousPlot.getRegionId() != plot.getRegionId()) {
121+
price += regionAreas.get(previousPlot.getRegionId());
122+
price += regionAreas.get(plot.getRegionId());
123+
}
124+
125+
previousPlot = plot;
126+
}
127+
128+
// Add price of right-most fence
129+
price += regionAreas.get(previousPlot.getRegionId());
130+
}
131+
132+
return price;
133+
134+
}
135+
136+
private Set<GardenPlot> getAdjacentPlots(GardenPlot gardenPlot) {
137+
138+
Set<GardenPlot> adjacentPlots = new HashSet<>();
139+
140+
Vector2 initialPos = gardenPlot.getPosition();
141+
142+
Vector2 nextPos = Vector2.transform(initialPos, Vector2.left());
143+
if(isInbounds(nextPos)) {
144+
adjacentPlots.add(map[nextPos.getY()][nextPos.getX()]);
145+
}
146+
147+
nextPos = Vector2.transform(initialPos, Vector2.right());
148+
if(isInbounds(nextPos)) {
149+
adjacentPlots.add(map[nextPos.getY()][nextPos.getX()]);
150+
}
151+
152+
nextPos = Vector2.transform(initialPos, Vector2.up());
153+
if(isInbounds(nextPos)) {
154+
adjacentPlots.add(map[nextPos.getY()][nextPos.getX()]);
155+
}
156+
157+
nextPos = Vector2.transform(initialPos, Vector2.down());
158+
if(isInbounds(nextPos)) {
159+
adjacentPlots.add(map[nextPos.getY()][nextPos.getX()]);
160+
}
161+
162+
return adjacentPlots;
163+
}
164+
165+
private boolean isInbounds(Vector2 pos) {
166+
return (pos.getY() >= 0 && pos.getY() < rows && pos.getX() >= 0 && pos.getX() < cols);
167+
}
168+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.adventofcode.flashk.day12;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@Getter
8+
public class GardenPlot {
9+
10+
private char plant;
11+
12+
@Setter
13+
private int regionId = 0;
14+
private Vector2 position;
15+
16+
@Setter
17+
private boolean visited = false;
18+
19+
public GardenPlot(char plant, Vector2 position) {
20+
this.plant = plant;
21+
this.position = position;
22+
}
23+
24+
public boolean hasRegion() {
25+
return regionId != 0;
26+
}
27+
}

src/test/java/com/adventofcode/flashk/day12/Day12Test.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public class Day12Test extends PuzzleTest {
3434
public void testSolvePart1Sample() {
3535

3636
// Read input file
37-
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
37+
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
3838

39-
assertEquals(0L,0L);
39+
GardenGroups gardenGroups = new GardenGroups(inputs);
40+
assertEquals(1930L, gardenGroups.solveA());
4041
}
4142

4243
@Test
@@ -47,10 +48,10 @@ public void testSolvePart1Sample() {
4748
public void testSolvePart1Input() {
4849

4950
// Read input file
50-
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
51-
52-
System.out.println("Solution: ");
53-
assertEquals(0L,0L);
51+
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE);
52+
GardenGroups gardenGroups = new GardenGroups(inputs);
53+
System.out.println("Solution: "+gardenGroups.solveA());
54+
assertEquals(1424472L,gardenGroups.solveA());
5455

5556
}
5657

@@ -62,9 +63,9 @@ public void testSolvePart1Input() {
6263
public void testSolvePart2Sample() {
6364

6465
// Read input file
65-
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
66-
67-
assertEquals(0L,0L);
66+
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
67+
GardenGroups gardenGroups = new GardenGroups(inputs);
68+
assertEquals(1206L,0L);
6869
}
6970

7071
@Test
@@ -75,8 +76,8 @@ public void testSolvePart2Sample() {
7576
public void testSolvePart2Input() {
7677

7778
// Read input file
78-
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
79-
79+
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE);
80+
GardenGroups gardenGroups = new GardenGroups(inputs);
8081
System.out.println("Solution: ");
8182
assertEquals(0L,0L);
8283

src/test/resources/inputs

0 commit comments

Comments
 (0)