|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "github.com/terminalnode/adventofcode2024/common"
|
| 6 | + "github.com/terminalnode/adventofcode2024/common/util" |
5 | 7 | )
|
6 | 8 |
|
7 | 9 | 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)) |
9 | 53 | }
|
0 commit comments