File tree Expand file tree Collapse file tree 4 files changed +112
-0
lines changed
Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 1+ input :
2+ http " https://adventofcode.com/2024/day/10/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+ "fmt"
6+ "os"
7+ )
8+
9+ const Size = 60
10+
11+ type P struct { x , y int }
12+
13+ func (p P ) Neighbors () []P {
14+ return []P {
15+ {p .x - 1 , p .y }, //N
16+ {p .x + 1 , p .y }, //S
17+ {p .x , p .y - 1 }, //W
18+ {p .x , p .y + 1 }, //E
19+ }
20+ }
21+
22+ func inMap (p P ) bool {
23+ return p .x >= 0 && p .x < Size && p .y >= 0 && p .y < Size
24+ }
25+
26+ func main () {
27+ h := parseMap ()
28+
29+ sum := 0
30+ for i := 0 ; i < Size ; i ++ {
31+ for j := 0 ; j < Size ; j ++ {
32+ if h [P {i , j }] == 0 {
33+ sum += score (h , P {i , j })
34+ }
35+ }
36+ }
37+
38+ fmt .Println (sum )
39+ }
40+
41+ func parseMap () map [P ]uint8 {
42+ scanner := bufio .NewScanner (os .Stdin )
43+
44+ h := map [P ]uint8 {}
45+ for i := 0 ; scanner .Scan (); i ++ {
46+ line := scanner .Text ()
47+ for j := 0 ; j < len (line ); j ++ {
48+ h [P {i , j }] = line [j ] - '0'
49+ }
50+ }
51+ return h
52+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ // find peaks
4+ func score (h map [P ]uint8 , p P ) int {
5+ peaks := map [P ]struct {}{}
6+ findPeaksRecursive (h , p , peaks )
7+ return len (peaks )
8+ }
9+
10+ func findPeaksRecursive (h map [P ]uint8 , p P , peaks map [P ]struct {}) {
11+ if h [p ] == 9 {
12+ peaks [p ] = struct {}{}
13+ return
14+ }
15+
16+ for _ , adj := range p .Neighbors () {
17+ if inMap (adj ) {
18+ if h [adj ] == h [p ]+ 1 {
19+ findPeaksRecursive (h , adj , peaks )
20+ }
21+ }
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ // find paths
4+ func score (h map [P ]uint8 , p P ) int {
5+ if h [p ] == 9 {
6+ return 1
7+ }
8+
9+ paths := 0
10+ for _ , adj := range p .Neighbors () {
11+ if inMap (adj ) {
12+ if h [adj ] == h [p ]+ 1 {
13+ paths += score (h , adj )
14+ }
15+ }
16+ }
17+ return paths
18+ }
You can’t perform that action at this time.
0 commit comments