Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 294e424

Browse files
committed
Add solution for day 8 part 1
1 parent 259cde9 commit 294e424

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ automatically rebuilt and redeployed ever time the `common` module or their own
5252
| 05 | ⭐ ⭐ | 18 | |
5353
| 06 | ⭐ ⭐ | 19 | |
5454
| 07 | ⭐ ⭐ | 20 | |
55-
| 08 | | 21 | |
55+
| 08 | | 21 | |
5656
| 09 | | 22 | |
5757
| 10 | | 23 | |
5858
| 11 | | 24 | |

solutions/day08/main.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
6+
"github.com/terminalnode/adventofcode2024/common/util"
57
)
68

79
func main() {
8-
common.Setup(8, nil, nil)
10+
common.Setup(8, part1, nil)
11+
}
12+
13+
func part1(
14+
input string,
15+
) string {
16+
m, err := util.NewCharMatrix(input)
17+
if err != nil {
18+
return fmt.Sprintf("Failed to parse input: %v", err)
19+
}
20+
21+
coords := make(map[uint8][]util.Coordinate)
22+
for x := 0; x <= m.MaxX; x++ {
23+
for y := 0; y <= m.MaxY; y++ {
24+
char, _ := m.Get(x, y)
25+
if char == '.' {
26+
continue
27+
}
28+
coords[char] = append(coords[char], util.Coordinate{X: x, Y: y})
29+
}
30+
}
31+
32+
antiNodeSet := make(map[string]bool)
33+
for c, charCoords := range coords {
34+
for i, thisCoord := range charCoords {
35+
for _, otherCoord := range charCoords[i+1:] {
36+
xDiff := thisCoord.X - otherCoord.X
37+
yDiff := thisCoord.Y - otherCoord.Y
38+
antiNodes := []util.Coordinate{
39+
{X: thisCoord.X + xDiff, Y: thisCoord.Y + yDiff},
40+
{X: otherCoord.X - xDiff, Y: otherCoord.Y - yDiff},
41+
}
42+
for _, antiNode := range antiNodes {
43+
if m.IsInMatrix(antiNode.X, antiNode.Y) {
44+
key := fmt.Sprintf("%d,%d", antiNode.X, antiNode.Y)
45+
antiNodeSet[key] = true
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
return fmt.Sprintf("Unique anti nodes: %d", len(antiNodeSet))
953
}

0 commit comments

Comments
 (0)