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

Commit 0e52788

Browse files
committed
Add solution for day 8 part 2
1 parent 294e424 commit 0e52788

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

solutions/day08/main.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func main() {
10-
common.Setup(8, part1, nil)
10+
common.Setup(8, part1, part2)
1111
}
1212

1313
func part1(
@@ -51,3 +51,69 @@ func part1(
5151

5252
return fmt.Sprintf("Unique anti nodes: %d", len(antiNodeSet))
5353
}
54+
55+
func part2(
56+
input string,
57+
) string {
58+
m, err := util.NewCharMatrix(input)
59+
if err != nil {
60+
return fmt.Sprintf("Failed to parse input: %v", err)
61+
}
62+
63+
coords := make(map[uint8][]util.Coordinate)
64+
for x := 0; x <= m.MaxX; x++ {
65+
for y := 0; y <= m.MaxY; y++ {
66+
char, _ := m.Get(x, y)
67+
if char == '.' {
68+
continue
69+
}
70+
coords[char] = append(coords[char], util.Coordinate{X: x, Y: y})
71+
}
72+
}
73+
74+
antiNodeSet := make(map[string]bool)
75+
for _, charCoords := range coords {
76+
for i, thisCoord := range charCoords {
77+
if len(charCoords) > 1 {
78+
addCoordinateToSet(antiNodeSet, thisCoord.X, thisCoord.Y)
79+
}
80+
81+
for _, otherCoord := range charCoords[i+1:] {
82+
xDiff := thisCoord.X - otherCoord.X
83+
yDiff := thisCoord.Y - otherCoord.Y
84+
85+
for n := 1; true; n++ {
86+
newX := thisCoord.X + (xDiff * n)
87+
newY := thisCoord.Y + (yDiff * n)
88+
if !m.IsInMatrix(newX, newY) {
89+
break
90+
}
91+
92+
addCoordinateToSet(antiNodeSet, newX, newY)
93+
}
94+
95+
for n := 1; true; n++ {
96+
newX := otherCoord.X - (xDiff * n)
97+
newY := otherCoord.Y - (yDiff * n)
98+
if !m.IsInMatrix(newX, newY) {
99+
break
100+
}
101+
102+
addCoordinateToSet(antiNodeSet, newX, newY)
103+
}
104+
105+
}
106+
}
107+
}
108+
109+
return fmt.Sprintf("Unique anti nodes (with resonance): %d", len(antiNodeSet))
110+
}
111+
112+
func addCoordinateToSet(
113+
set map[string]bool,
114+
x int,
115+
y int,
116+
) {
117+
key := fmt.Sprintf("%02d,%02d", x, y)
118+
set[key] = true
119+
}

0 commit comments

Comments
 (0)