fix missing
This commit is contained in:
103
2024/go/day10/day10.go
Normal file
103
2024/go/day10/day10.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package day10
|
||||
|
||||
import (
|
||||
"adventofcode2024/utils"
|
||||
"adventofcode2024/utils/grid2d"
|
||||
"adventofcode2024/utils/inputs"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Position struct {
|
||||
height int
|
||||
summit bool
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
score := 0
|
||||
i := 0
|
||||
grid := inputs.ToGrid2D(input, "\n", "", Position{height: -1}, func(c string) Position { return Position{summit: false, height: utils.MustAtoi(c)} })
|
||||
fmt.Println(grid.StringWithFormatter(formatter))
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
position := grid.Get(x, y)
|
||||
if position.height != 0 {
|
||||
continue
|
||||
}
|
||||
wipe_summit(grid)
|
||||
score += getScore(x, y, 0, 0, grid)
|
||||
fmt.Printf("Trailhead %d (%d,%d) paths: %d\n", i, x, y, score)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
score := 0
|
||||
i := 0
|
||||
grid := inputs.ToGrid2D(input, "\n", "", Position{height: -1}, func(c string) Position { return Position{summit: false, height: utils.MustAtoi(c)} })
|
||||
fmt.Println(grid.StringWithFormatter(formatter))
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
position := grid.Get(x, y)
|
||||
if position.height != 0 {
|
||||
continue
|
||||
}
|
||||
wipe_summit(grid)
|
||||
score += getScore2(x, y, 0, 0, grid)
|
||||
fmt.Printf("Trailhead %d (%d,%d) paths: %d\n", i, x, y, score)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func formatter(p Position, x int, y int) string {
|
||||
return fmt.Sprintf("%v", p.height)
|
||||
}
|
||||
|
||||
func wipe_summit(grid *grid2d.Grid[Position]) {
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
pos := grid.Get(x, y)
|
||||
if pos.height == 9 && pos.summit {
|
||||
pos.summit = false
|
||||
grid.Set(x, y, pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getScore(x, y, level, score int, grid *grid2d.Grid[Position]) int {
|
||||
directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
|
||||
for _, dir := range directions {
|
||||
x1 := x + dir[0]
|
||||
y1 := y + dir[1]
|
||||
nposition := grid.Get(x1, y1)
|
||||
if level == 8 && nposition.height == 9 {
|
||||
score++
|
||||
}
|
||||
if nposition.height != level+1 {
|
||||
continue
|
||||
}
|
||||
score = getScore(x1, y1, nposition.height, score, grid)
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func getScore2(x, y, level, score int, grid *grid2d.Grid[Position]) int {
|
||||
directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
|
||||
for _, dir := range directions {
|
||||
x1 := x + dir[0]
|
||||
y1 := y + dir[1]
|
||||
nposition := grid.Get(x1, y1)
|
||||
if level == 8 && nposition.height == 9 {
|
||||
score++
|
||||
}
|
||||
if nposition.height != level+1 {
|
||||
continue
|
||||
}
|
||||
score = getScore2(x1, y1, nposition.height, score, grid)
|
||||
}
|
||||
return score
|
||||
}
|
||||
31
2024/go/day10/day10_test.go
Normal file
31
2024/go/day10/day10_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package day10
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732`)
|
||||
require.Equal(t, 36, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732`)
|
||||
require.Equal(t, 81, r)
|
||||
}
|
||||
56
2024/go/day10/input.txt
Normal file
56
2024/go/day10/input.txt
Normal file
@@ -0,0 +1,56 @@
|
||||
43217654309879876104563234589896761012345656543098901001
|
||||
34108903212368123233472105676787851299856567832187812652
|
||||
45677814301457014312986345589876940387765678943046543743
|
||||
56789325100876525800345276430105432456566589858956769821
|
||||
65438456912963436901236187621234501223455410767349874430
|
||||
74321067803451107898547093412012982016764323101210143561
|
||||
89412210987430210789698892102123673107895214589723652678
|
||||
78601521856521345678721743089034543212345605679854781789
|
||||
66789430545678934505430654898745672212236765434765690878
|
||||
55676545038943213216765989601654981300129870121012345969
|
||||
46543216127657804389834808762347690456789983498141456452
|
||||
65454307233456934870126712354878765565210012507230987321
|
||||
54567898332110125961015645403969234674323215616546576010
|
||||
65658983021001876854324106912452128789321308723455678901
|
||||
76543212137892965348933217832141089873410419678964987652
|
||||
89862901236543501267018363101033210565566566569873788543
|
||||
89871876544345652106529854892123521454479877654012699830
|
||||
78100703454278743212434763763014672343287778943210581021
|
||||
65210212565189858906763212654985785650198961212101432010
|
||||
54321056876012567875898708783476698763267890303215678321
|
||||
87012567988703489874345679692676543294986725456574329478
|
||||
96543498589876530365210189501987650187675216987687610569
|
||||
01234567651010921234501076401236501010564307678596789876
|
||||
10389830532329854376542345321545692323403018789455430965
|
||||
21456721013456765289031234980098783410912129670320121054
|
||||
92505432565421010109120345671107654567823451061210120123
|
||||
87615443478302341018750134543234541050765962552121234984
|
||||
34322342189219650129665234789432132021894873443010965673
|
||||
45451056077828743234574343276543032134703210523457876532
|
||||
51069987456943104545987650167898749865612309610765454101
|
||||
32678678345652210698092156756547056764303458779890363232
|
||||
43010589232781306787183045876532178965210569888211274321
|
||||
56923432101090458989254234923421369878934678898302989430
|
||||
87889211078764567876360143010030450767125986567401276589
|
||||
96676305669653478985478652102141341458076803456564345676
|
||||
45435434734522780340349760123456232349883712678178737894
|
||||
80127821821011091211299854354987101016792103549069016323
|
||||
92346940910329654304587121267807652345013401232108925412
|
||||
81055432101458765643671010871018947654324589543987432101
|
||||
76567789023467010782532346965425638945695678654986540012
|
||||
05498654110567821891047897212334721032786014345678901098
|
||||
12387013223489932346156598101549889821012823216765212387
|
||||
03456323016576542345692367210678710701296954907854323456
|
||||
12345465437895431016781450123467623654387867878985401501
|
||||
21089870124326528701670101874345634565676541045621032012
|
||||
32189210065017019632543210965236730120545632456734548743
|
||||
43498349876298903545450143050159821321234012349895699654
|
||||
34567658389101232123469052101567634489234510106786789985
|
||||
99876501276788943016578769872498105672105621215021058876
|
||||
87035432365897654107689898763343234321678789334134567655
|
||||
70129641034781089898791099854232145690569245493254321567
|
||||
63238701123654178718982387763156056781410126787655010498
|
||||
54345652321073265001073456012047189872328901098546710327
|
||||
34568543434589874132569895145438976987437812361239891210
|
||||
21879654898678013203456701236327805456546521450967890123
|
||||
30968745467654320112345210987610112345545430567856543234
|
||||
Reference in New Issue
Block a user