|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type coord struct { |
| 10 | + R int |
| 11 | + C int |
| 12 | +} |
| 13 | + |
| 14 | +func findNodes(coords []coord, nodes *[][]bool) { |
| 15 | + for i := range len(coords) { |
| 16 | + for j := i + 1; j < len(coords); j++ { |
| 17 | + coord1 := coords[i] |
| 18 | + coord2 := coords[j] |
| 19 | + rDiff := coord2.R - coord1.R |
| 20 | + cDiff := coord2.C - coord1.C |
| 21 | + n1 := coord{ |
| 22 | + R: coord1.R - rDiff, |
| 23 | + C: coord1.C - cDiff, |
| 24 | + } |
| 25 | + n2 := coord{ |
| 26 | + R: coord2.R + rDiff, |
| 27 | + C: coord2.C + cDiff, |
| 28 | + } |
| 29 | + if n1.R >= 0 && n1.R < len(*nodes) && n1.C >= 0 && n1.C < len((*nodes)[0]) { |
| 30 | + (*nodes)[n1.R][n1.C] = true |
| 31 | + } |
| 32 | + if n2.R >= 0 && n2.R < len(*nodes) && n2.C >= 0 && n2.C < len((*nodes)[0]) { |
| 33 | + (*nodes)[n2.R][n2.C] = true |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func gcd(a, b int) int { |
| 40 | + for b != 0 { |
| 41 | + a, b = b, a%b |
| 42 | + } |
| 43 | + return a |
| 44 | +} |
| 45 | + |
| 46 | +// Function to simplify the ratio |
| 47 | +func simplifyRatio(a, b int) (int, int) { |
| 48 | + divisor := gcd(a, b) |
| 49 | + return a / divisor, b / divisor |
| 50 | +} |
| 51 | + |
| 52 | +func findNodes2(coords []coord, nodes *[][]bool) { |
| 53 | + for i := range len(coords) { |
| 54 | + for j := i + 1; j < len(coords); j++ { |
| 55 | + coord1 := coords[i] |
| 56 | + coord2 := coords[j] |
| 57 | + rDiff := coord2.R - coord1.R |
| 58 | + cDiff := coord2.C - coord1.C |
| 59 | + rDiff, cDiff = simplifyRatio(rDiff, cDiff) |
| 60 | + |
| 61 | + start := coord1 |
| 62 | + for start.R >= 0 && start.R < len(*nodes) && start.C >= 0 && start.C < len((*nodes)[0]) { |
| 63 | + (*nodes)[start.R][start.C] = true |
| 64 | + start.R -= rDiff |
| 65 | + start.C -= cDiff |
| 66 | + } |
| 67 | + start = coord1 |
| 68 | + for start.R >= 0 && start.R < len(*nodes) && start.C >= 0 && start.C < len((*nodes)[0]) { |
| 69 | + (*nodes)[start.R][start.C] = true |
| 70 | + start.R += rDiff |
| 71 | + start.C += cDiff |
| 72 | + } |
| 73 | + |
| 74 | + //n1 := coord{ |
| 75 | + // R: coord1.R - rDiff, |
| 76 | + // C: coord1.C - cDiff, |
| 77 | + //} |
| 78 | + //n2 := coord{ |
| 79 | + // R: coord2.R + rDiff, |
| 80 | + // C: coord2.C + cDiff, |
| 81 | + //} |
| 82 | + //if n1.R >= 0 && n1.R < len(*nodes) && n1.C >= 0 && n1.C < len((*nodes)[0]) { |
| 83 | + // (*nodes)[n1.R][n1.C] = true |
| 84 | + //} |
| 85 | + //if n2.R >= 0 && n2.R < len(*nodes) && n2.C >= 0 && n2.C < len((*nodes)[0]) { |
| 86 | + // (*nodes)[n2.R][n2.C] = true |
| 87 | + //} |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func part1() { |
| 93 | + //raw, _ := os.ReadFile("test.txt") |
| 94 | + raw, _ := os.ReadFile("input.txt") |
| 95 | + data := string(raw) |
| 96 | + lines := strings.Split(data, "\n") |
| 97 | + |
| 98 | + grid := [][]string{} |
| 99 | + nodes := [][]bool{} |
| 100 | + for _, line := range lines { |
| 101 | + row := make([]string, len(line)) |
| 102 | + boolRow := make([]bool, len(line)) |
| 103 | + for i, char := range strings.Split(line, "") { |
| 104 | + row[i] = char |
| 105 | + boolRow[i] = false |
| 106 | + } |
| 107 | + grid = append(grid, row) |
| 108 | + nodes = append(nodes, boolRow) |
| 109 | + } |
| 110 | + |
| 111 | + // map from a frequency to coordinates of the towers with that frequency |
| 112 | + freqs := map[string][]coord{} |
| 113 | + for r := range len(grid) { |
| 114 | + for c := range len(grid[0]) { |
| 115 | + val := grid[r][c] |
| 116 | + if val != "." { |
| 117 | + freqs[val] = append(freqs[val], coord{R: r, C: c}) |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + for _, coords := range freqs { |
| 124 | + findNodes(coords, &nodes) |
| 125 | + } |
| 126 | + |
| 127 | + count := 0 |
| 128 | + for i := range len(nodes) { |
| 129 | + for j := range len(nodes[0]) { |
| 130 | + if nodes[i][j] { |
| 131 | + count++ |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + fmt.Println(count) |
| 136 | +} |
| 137 | + |
| 138 | +func part2() { |
| 139 | + //raw, _ := os.ReadFile("test.txt") |
| 140 | + raw, _ := os.ReadFile("input.txt") |
| 141 | + data := string(raw) |
| 142 | + lines := strings.Split(data, "\n") |
| 143 | + |
| 144 | + grid := [][]string{} |
| 145 | + nodes := [][]bool{} |
| 146 | + for _, line := range lines { |
| 147 | + row := make([]string, len(line)) |
| 148 | + boolRow := make([]bool, len(line)) |
| 149 | + for i, char := range strings.Split(line, "") { |
| 150 | + row[i] = char |
| 151 | + boolRow[i] = false |
| 152 | + } |
| 153 | + grid = append(grid, row) |
| 154 | + nodes = append(nodes, boolRow) |
| 155 | + } |
| 156 | + |
| 157 | + // map from a frequency to coordinates of the towers with that frequency |
| 158 | + freqs := map[string][]coord{} |
| 159 | + for r := range len(grid) { |
| 160 | + for c := range len(grid[0]) { |
| 161 | + val := grid[r][c] |
| 162 | + if val != "." { |
| 163 | + freqs[val] = append(freqs[val], coord{R: r, C: c}) |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + for _, coords := range freqs { |
| 170 | + findNodes2(coords, &nodes) |
| 171 | + } |
| 172 | + |
| 173 | + count := 0 |
| 174 | + for i := range len(nodes) { |
| 175 | + for j := range len(nodes[0]) { |
| 176 | + if nodes[i][j] { |
| 177 | + count++ |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + fmt.Println(count) |
| 182 | +} |
| 183 | + |
| 184 | +func main() { |
| 185 | + part1() |
| 186 | + part2() |
| 187 | +} |
0 commit comments