Skip to content

Commit 6573162

Browse files
committed
2024.10
1 parent ceddd99 commit 6573162

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

2024/10-map/example.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
89010123
2+
78121874
3+
87430965
4+
96549874
5+
45678903
6+
32019012
7+
01329801
8+
10456732

2024/10-map/input.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
0145677654325490845691012345621876560340100123012398
2+
1238988998216781932782121234010983401259230034563387
3+
4323589867205432801543210945125672378768341459654456
4+
1014678454102301765456901876034561769897652368776301
5+
6765521543201512350307872962120120850785677879789210
6+
0896430439810487461217965871298438941454980968654321
7+
1765012126723596543478014560167569032363211457256762
8+
2872121035434654302569123441455678121071102340145895
9+
3961230345985783211098710332334599432980098743232434
10+
4550145456676890100167000146721087641001217650141325
11+
9649056765489910651254121035890014550732309654210016
12+
8738769894676328762343236124301123669845498763227807
13+
7129787923565439651456347833210008778996787120156998
14+
1013496014567010340987656944782119211087898031343210
15+
1012345002198321232876501855693024302332196541034101
16+
1234567123023450901965432761054435678445087670123256
17+
0144898454910767845670301622169546589556764581214347
18+
4343732367867893034981243213678696765698873294309838
19+
1234351078754702121893214504589787894780987101456789
20+
0943765469843213210721303698921009683211074560167678
21+
7856874348765434785630410787652118701202983076298987
22+
6765989219210125698540521236543025654343892189347567
23+
5667867804321087582101638347432134509856782345456498
24+
1054876965691296443492789858901213212790101276012387
25+
2123965476780125356783498767654300103685230983401456
26+
3278954587821034219870567658967876234576541092560845
27+
2567543296930761006721498743478965987655612451076921
28+
1056230145945852345012345412661234554567803367987830
29+
2340167034876945123211296401780109693069954298756101
30+
7887658123985231034500787345691218782178769110343232
31+
6992349032100112985011216217885011071078978021232349
32+
5801239844301101676720105606976522362567987876541458
33+
4321023765432432369838234745983439453454376965430167
34+
3087610321396565458949549836112378321043105302301298
35+
2198565410187076327658678921001065439652234211017657
36+
3233478903216189014547664567632104508701230322928943
37+
4542369894103273223014523498549812012349821498834012
38+
7651423765764784132123210210038763676256734567765423
39+
8960314349845695041012396342121054985109875498656701
40+
4871005256736786780169487653434873014018766787567892
41+
5654196149821677893278565694565963223321051096450943
42+
6743287032120563034567684787696954101438142345321056
43+
7899180129061432125675893256787845698589233239885469
44+
3458098938778743210986710143876034787670132178596378
45+
2167347845609654331235430782932128236101056017687267
46+
6056256741012103220543521691047659145692347012570167
47+
7890165432343210110652434598798578036785498743456898
48+
6784567876758701328701223123623457629876901234347107
49+
5413218965869232499899810034510256510267892301298256
50+
4303409954978149581234745218760105100126765410789340
51+
3212567823019058670365634309451234981237898323870121
52+
4321016012108767621256565678321015676546767654965432

2024/10-map/main.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"advent-of-code/util"
5+
"bufio"
6+
"fmt"
7+
)
8+
9+
var grid [][]byte
10+
11+
func main() {
12+
file := util.FileFromArgs()
13+
scan := bufio.NewScanner(file)
14+
15+
var starts []position
16+
17+
for scan.Scan() {
18+
row := scan.Bytes()
19+
20+
for i := range row {
21+
row[i] = row[i] - '0'
22+
23+
if row[i] == 0 {
24+
starts = append(starts, position{i, len(grid)})
25+
}
26+
}
27+
28+
grid = append(grid, row)
29+
}
30+
31+
var trials, trialPoints int
32+
for i := range starts {
33+
trialheads := walkUp(starts[i])
34+
trials += len(util.Unique(trialheads))
35+
trialPoints += len(trialheads)
36+
}
37+
38+
fmt.Println(trials)
39+
fmt.Println(trialPoints)
40+
}
41+
42+
type position struct {
43+
X, Y int
44+
}
45+
46+
// walkUp starts at a position and returns all sumits it can reach with a unique path
47+
func walkUp(p position) []position {
48+
height := grid[p.Y][p.X]
49+
if height == 9 {
50+
return []position{p}
51+
}
52+
53+
var summits []position
54+
if p.X > 0 && grid[p.Y][p.X-1] == height+1 { // LEFT
55+
summits = append(summits, walkUp(position{p.X - 1, p.Y})...)
56+
}
57+
if p.X < len(grid[0])-1 && grid[p.Y][p.X+1] == height+1 { // RIGHT
58+
summits = append(summits, walkUp(position{p.X + 1, p.Y})...)
59+
}
60+
if p.Y > 0 && grid[p.Y-1][p.X] == height+1 { // UP
61+
summits = append(summits, walkUp(position{p.X, p.Y - 1})...)
62+
}
63+
if p.Y < len(grid)-1 && grid[p.Y+1][p.X] == height+1 { // DOWN
64+
summits = append(summits, walkUp(position{p.X, p.Y + 1})...)
65+
}
66+
67+
return summits
68+
}

util/slices.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,26 @@ func CountOccurrences[V comparable](s []V, value V) int {
1010

1111
return count
1212
}
13+
14+
// Unique returns a new slice with only unique values
15+
func Unique[V comparable](s []V) []V {
16+
ns := make([]V, 0, len(s))
17+
18+
var duplicate bool
19+
for i := range s {
20+
duplicate = false
21+
22+
for j := range ns {
23+
if s[i] == ns[j] {
24+
duplicate = true
25+
break
26+
}
27+
}
28+
29+
if !duplicate {
30+
ns = append(ns, s[i])
31+
}
32+
}
33+
34+
return ns
35+
}

util/slices_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package util
2+
3+
import "testing"
4+
5+
func TestUnique(t *testing.T) {
6+
s := Unique([]int{1, 2, 3, 1, 2, 3})
7+
8+
if len(s) != 3 || s[0] != 1 || s[1] != 2 || s[2] != 3 {
9+
t.Error("Expected unique array, got ", s)
10+
}
11+
}

0 commit comments

Comments
 (0)