Skip to content

Commit c42bb3b

Browse files
committed
feat: day 04 part 2 solution
1 parent 3b22e02 commit c42bb3b

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

2024/04/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,49 @@ func solvePart1(input string) int {
3333
return len(words)
3434
}
3535

36+
func solvePart2(input string) int {
37+
characterGrid, err := inputs.ExtractCharacterGrid(input)
38+
if err != nil {
39+
log.Fatalf("ERROR: %v", err)
40+
}
41+
42+
// Thats wayyy too many nested slices but it workd :-)
43+
subgrids := [][][]byte{}
44+
subgridCount := 0
45+
46+
subgrids = append(subgrids, [][]byte{
47+
{'M', '.', 'M'},
48+
{'.', 'A', '.'},
49+
{'S', '.', 'S'},
50+
})
51+
subgrids = append(subgrids, [][]byte{
52+
{'M', '.', 'S'},
53+
{'.', 'A', '.'},
54+
{'M', '.', 'S'},
55+
})
56+
subgrids = append(subgrids, [][]byte{
57+
{'S', '.', 'M'},
58+
{'.', 'A', '.'},
59+
{'S', '.', 'M'},
60+
})
61+
subgrids = append(subgrids, [][]byte{
62+
{'S', '.', 'S'},
63+
{'.', 'A', '.'},
64+
{'M', '.', 'M'},
65+
})
66+
67+
for _, subgrid := range subgrids {
68+
subgridLocationList := grid.FindSubgridsInGrid(characterGrid, subgrid, '.')
69+
subgridCount += len(subgridLocationList)
70+
}
71+
72+
return subgridCount
73+
}
74+
3675
func main() {
3776
part1Solution := solvePart1(input)
77+
part2Solution := solvePart2(input)
3878

3979
fmt.Println("Day 04 Part 1 solution:", part1Solution)
80+
fmt.Println("Day 04 Part 2 solution:", part2Solution)
4081
}

2024/04/main_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ var test1 string
1111
//go:embed test2.txt
1212
var test2 string
1313

14+
//go:embed test3.txt
15+
var test3 string
16+
17+
//go:embed test4.txt
18+
var test4 string
19+
1420
func TestSolution(t *testing.T) {
1521
t.Run("Day 04 part 1 test 1", func(t *testing.T) {
1622
want := 4
@@ -29,4 +35,22 @@ func TestSolution(t *testing.T) {
2935
t.Errorf("Incorrect solution, got %d want %d", got, want)
3036
}
3137
})
38+
39+
t.Run("Day 04 part 2 test 1", func(t *testing.T) {
40+
want := 1
41+
got := solvePart2(test3)
42+
43+
if got != want {
44+
t.Errorf("Incorrect solution, got %d want %d", got, want)
45+
}
46+
})
47+
48+
t.Run("Day 04 part 2 test 2", func(t *testing.T) {
49+
want := 9
50+
got := solvePart2(test4)
51+
52+
if got != want {
53+
t.Errorf("Incorrect solution, got %d want %d", got, want)
54+
}
55+
})
3256
}

2024/04/test3.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
M.S
2+
.A.
3+
M.S

2024/04/test4.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.M.S......
2+
..A..MSMS.
3+
.M.S.MAA..
4+
..A.ASMSM.
5+
.M.S.M....
6+
..........
7+
S.S.S.S.S.
8+
.A.A.A.A..
9+
M.M.M.M.M.
10+
..........

internal/grid/subgrid.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package grid
2+
3+
// SubgridLocation represents the coordinates of a found subgrid in the grid
4+
type SubgridLocation struct {
5+
Subgrid [][]byte // Subgrid is the subgrid that was found in the grid
6+
StartX int // StartX is the starting x-coordinate of the subgrid in the grid
7+
StartY int // StartY is the starting y-coordinate of the subgrid in the grid
8+
EndX int // EndX is the ending x-coordinate of the subgrid in the grid
9+
EndY int // EndY is the ending y-coordinate of the subgrid in the grid
10+
}
11+
12+
// FindSubgridsInGrid searches for subgrids in a given grid
13+
//
14+
// '.' could be any character in the grid. All other characters need an exact match.
15+
// wildcardCharacter in subgrid can match any character in grid
16+
func FindSubgridsInGrid(grid, subgrid [][]byte, wildcardCharacter byte) []SubgridLocation {
17+
var result []SubgridLocation
18+
19+
for i, row := range grid {
20+
for j := range row {
21+
sgl := checkIndexForSubgrid(i, j, grid, subgrid, wildcardCharacter)
22+
if len(sgl.Subgrid) != 0 {
23+
result = append(result, sgl)
24+
}
25+
}
26+
}
27+
return result
28+
}
29+
30+
func checkIndexForSubgrid(i, j int, grid, subgrid [][]byte, wildcardCharacter byte) SubgridLocation {
31+
var result SubgridLocation
32+
33+
for si, row := range subgrid {
34+
for sj := range row {
35+
// Check whether subgrid exceeds the bounds of grid
36+
if i+len(subgrid) > len(grid) || j+len(subgrid[0]) > len(grid[0]) {
37+
return result
38+
}
39+
40+
// Check whether the character matches or is the wildcard character
41+
if !(subgrid[si][sj] == wildcardCharacter || subgrid[si][sj] == grid[i+si][j+sj]) {
42+
return result
43+
}
44+
}
45+
}
46+
47+
result.Subgrid = subgrid
48+
result.StartX = j
49+
result.StartY = i
50+
result.EndX = j + len(subgrid[0]) - 1
51+
result.EndY = i + len(subgrid) - 1
52+
53+
return result
54+
}

0 commit comments

Comments
 (0)