Skip to content

Commit f52d927

Browse files
committed
Solution for 2024, day 8
1 parent aac6de6 commit f52d927

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

2024/8/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/8/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input

2024/8/common.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
const Size = 50
9+
10+
type P struct {
11+
X, Y int
12+
}
13+
14+
func (p P) Valid() bool {
15+
return p.X >= 0 && p.X < Size && p.Y >= 0 && p.Y < Size
16+
}
17+
18+
func parseInput() map[byte][]P {
19+
scanner := bufio.NewScanner(os.Stdin)
20+
21+
freqs := map[byte][]P{}
22+
for i := 0; scanner.Scan(); i++ {
23+
line := scanner.Text()
24+
25+
for j, c := range line {
26+
if c == '.' {
27+
continue
28+
}
29+
30+
ch := byte(c)
31+
32+
freqs[ch] = append(freqs[ch], P{i, j})
33+
}
34+
}
35+
36+
return freqs
37+
}

2024/8/main1.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
freqs := parseInput()
7+
8+
antiNodes := map[P]struct{}{}
9+
for _, locs := range freqs {
10+
for a := 0; a < len(locs)-1; a++ {
11+
for b := a + 1; b < len(locs); b++ {
12+
delta := P{locs[b].X - locs[a].X, locs[b].Y - locs[a].Y}
13+
14+
anti1 := P{locs[a].X - delta.X, locs[a].Y - delta.Y}
15+
if anti1.X >= 0 && anti1.X < Size && anti1.Y >= 0 && anti1.Y < Size {
16+
antiNodes[anti1] = struct{}{}
17+
}
18+
19+
anti2 := P{locs[b].X + delta.X, locs[b].Y + delta.Y}
20+
if anti2.X >= 0 && anti2.X < Size && anti2.Y >= 0 && anti2.Y < Size {
21+
antiNodes[anti2] = struct{}{}
22+
}
23+
}
24+
}
25+
}
26+
27+
fmt.Println(len(antiNodes))
28+
}

2024/8/main2.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
freqs := parseInput()
7+
8+
antiNodes := map[P]struct{}{}
9+
for _, locs := range freqs {
10+
for a := 0; a < len(locs)-1; a++ {
11+
for b := a + 1; b < len(locs); b++ {
12+
delta := P{locs[b].X - locs[a].X, locs[b].Y - locs[a].Y}
13+
14+
outOfBound := 0
15+
for period := 0; outOfBound < 2; period++ {
16+
outOfBound = 0
17+
18+
anti1 := P{locs[a].X - period*delta.X, locs[a].Y - period*delta.Y}
19+
if anti1.Valid() {
20+
antiNodes[anti1] = struct{}{}
21+
} else {
22+
outOfBound++
23+
}
24+
25+
anti2 := P{locs[b].X + period*delta.X, locs[b].Y + period*delta.Y}
26+
if anti2.Valid() {
27+
antiNodes[anti2] = struct{}{}
28+
} else {
29+
outOfBound++
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
fmt.Println(len(antiNodes))
37+
}

0 commit comments

Comments
 (0)