File tree Expand file tree Collapse file tree 4 files changed +121
-0
lines changed
Expand file tree Collapse file tree 4 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments