|
7 | 7 | )
|
8 | 8 |
|
9 | 9 | func main() {
|
10 |
| - common.Setup(8, part1, nil) |
| 10 | + common.Setup(8, part1, part2) |
11 | 11 | }
|
12 | 12 |
|
13 | 13 | func part1(
|
@@ -51,3 +51,69 @@ func part1(
|
51 | 51 |
|
52 | 52 | return fmt.Sprintf("Unique anti nodes: %d", len(antiNodeSet))
|
53 | 53 | }
|
| 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