Skip to content

Commit ab1ae89

Browse files
committed
day 8
1 parent befced2 commit ab1ae89

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

8/input.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.....................................O..V.........
2+
..................................................
3+
................................O.........Z.......
4+
....W....................................V....v...
5+
........................m................8........
6+
.....................................n........Z..v
7+
.............F.....3...n....5m....................
8+
................................................V.
9+
................3............iv....Z.............V
10+
...........................O..n..i........p......H
11+
......W..6..............................i.........
12+
......................................b...........
13+
..................................n........p......
14+
........M.......c...........m..5......1...........
15+
...M............................L..5..A...........
16+
...w...........9.............F5..................q
17+
.W.....................................q....p.....
18+
.......W........r.......H.....LA......q...........
19+
................4.F....................A..........
20+
........3.......a.....F...................A..L....
21+
....ME...............................Q..........q.
22+
.E..................ih...................Z........
23+
................E...H...........h.................
24+
.........m.........X..............................
25+
..................0......C.................h......
26+
.M......l.................Q.h.....................
27+
..........C..............0........................
28+
.............lX............3.c....................
29+
......8.X.........c....r..a......H.....9..........
30+
.................QE.....C.........................
31+
..R................a........Q...................7.
32+
...........................a......................
33+
l..........X.R............1..I..........9.........
34+
.................0R..............b.....z......x...
35+
.......l.....w....r..........................b....
36+
.8..........0...................P1z...............
37+
.............c.........................L..........
38+
.................C..N............o............9...
39+
...........e..f..N................................
40+
8.............................B...................
41+
...........4...............................x......
42+
....w....RY..........4.......................P....
43+
.........yw.....Y.............o2...............7..
44+
..6y........4..............fo..............7......
45+
.........Y..6............o......................x.
46+
.....Y....e.....y..I.r...........2................
47+
....e.............................P.......z.bB....
48+
.............6.................B........7......x..
49+
..y.N........f...........1....I....z....B.........
50+
.....e....f.............I.................2.......

8/main.go

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}

8/test.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

0 commit comments

Comments
 (0)