Day 10. part 1.

This commit is contained in:
Borna Rajković 2024-12-10 10:13:42 +01:00
parent f84401e805
commit 778ed08680
9 changed files with 228 additions and 0 deletions

View File

@ -30,3 +30,6 @@ var Day08Data string
//go:embed day_09/data.txt
var Day09Data string
//go:embed day_10/data.txt
var Day10Data string

56
day_10/data.txt Normal file
View File

@ -0,0 +1,56 @@
12340101878761023456545454342165345676769343212898101234
01253298999452310897102367443078756789878552100347610945
10765387888341456798211078954569865498765667891256329876
67876456672210878654302234567054654328654308760345434565
58960145521008969543033123898123521019603219874321294423
87654234432023457652123087878931438908712387665230783212
94543454342112348956542198963210567125654498540145632101
23612763543201032347899017654231436034675643489876543234
18703892698715101051058720140145621894589732106789012105
09543211789876510962345638230898730783289801875498721076
67657600692105401873496549821769845670170789964329630387
78998776543212392398787056765654301234561276353010541298
12389985430124985407652145654510210019654305442126752987
01276578921003476014550234783323212128763012330145893456
10345667654312564323761221092104303499892100123232123454
67812456783239875019854332112365694781012349874123010763
54903441092128756998543345001436785632509854365032109894
43786532361045677877632356916512345683408765256743238876
32697895678738988988901987887402398796512120129854567905
01521004569827432234870676590341456587543098218769871014
10435410458216541123766783411250107079650187601256712323
28976321326502100054355692101763298128756234532345603478
67689100015489232360544543439874380137897878943210436569
86543212326376541071239876528545673249856967657654347878
98743265417217634289140345012789654356745667898943256981
01650178108908987123021236109878766325633458987001107210
32210989832212456014123987216765645414722189076122098723
43345676541003327865474532125456736703810012165433489654
56934307687654018976789643034143821892954187234454967503
67855218998987652189888752102012960911063296545567818912
12346783219878941079892167811023451103478103456765401876
01656594100568932016783078912110167012569812343890322345
10789015601457657825654189801256778929658751012701210154
03498323712310146934568789221349877878743567801662567030
12567487893401235476549870130210918969812478945673498121
03456596987632123389034765245601101255702301234989187432
12456745076541034232125634310781056344321030123231016589
12349832125765015121034567323892347897812345678192354678
01901221034894176076543278498787323458900156569087463210
10876320676783289089432109586565510067851287438196578987
76505410787690987128761012675676432104560399821045678876
85414125698541056298456783254986549023870456734430019565
98323034567232345347321292123877678016901960109821023432
86018601256123453456510398001678987655432871218783456501
67889543345034762387105467210900178746945982345692167898
56977602358903891093456554367810269637876672107898056982
43868911567212987784367893456524341023776543206561154321
32154320126303456101210210347433452014789432015210067010
41032101015454543289010101298934563128128721324332198011
32349876522387635376521032876129654789013210458949876523
21456910431497656785438945965098765658134502167653210435
10237801230598547898747876234876666543265653057894387654
90198212347654632336656760143965455698678784567765896023
83285675498103201345698601107834564789549894398756895112
74234786987230110218767652216723873210432765212347763203
65101697896541222109856543345010960123101656101078954512

8
day_10/example_data.txt Normal file
View File

@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

View File

@ -0,0 +1,7 @@
...0...
...1...
...2...
6543456
7.....7
8.....8
9.....9

View File

@ -0,0 +1,7 @@
10..9..
2...8..
3...7..
4567654
...8..3
...9..2
.....01

View File

@ -0,0 +1,7 @@
..90..9
...1.98
...2..7
6543456
765.987
876....
987....

81
day_10/solution.go Normal file
View File

@ -0,0 +1,81 @@
package day_10
import (
"strings"
)
const Empty = '.'
type Point struct {
x, y int
height rune
}
type TopographicMap [][]rune
func (t TopographicMap) InMap(x, y int) bool {
return x >= 0 && x < len(t[0]) && y >= 0 && y < len(t)
}
func (t TopographicMap) At(x, y int) rune {
if t.InMap(x, y) {
return t[y][x]
}
return Empty
}
// task:https://adventofcode.com/2024/day/10
func SolveBasic(input string) int {
topographicMap := loadMap(input)
solutions := 0
for y, line := range topographicMap {
for x, pos := range line {
if pos == '0' {
trails := score(topographicMap, x, y)
solutions += trails
}
}
}
return solutions
}
// task:https://adventofcode.com/2024/day/10#part2
func SolveComplex(input string) int {
return 0
}
func score(tMap TopographicMap, x int, y int) int {
queue := []Point{{x, y, '0'}}
trails := map[Point]bool{}
for len(queue) > 0 {
p := queue[0]
queue = queue[1:]
if p.height == '9' {
trails[p] = true
} else {
if tMap.At(p.x-1, p.y) == p.height+1 {
queue = append(queue, Point{p.x - 1, p.y, p.height + 1})
}
if tMap.At(p.x, p.y-1) == p.height+1 {
queue = append(queue, Point{p.x, p.y - 1, p.height + 1})
}
if tMap.At(p.x+1, p.y) == p.height+1 {
queue = append(queue, Point{p.x + 1, p.y, p.height + 1})
}
if tMap.At(p.x, p.y+1) == p.height+1 {
queue = append(queue, Point{p.x, p.y + 1, p.height + 1})
}
}
}
return len(trails)
}
func loadMap(input string) TopographicMap {
input = strings.ReplaceAll(input, "\r", "")
topographicMap := TopographicMap{}
for _, line := range strings.Split(input, "\n") {
topographicMap = append(topographicMap, []rune(line))
}
return topographicMap
}

54
day_10/solution_test.go Normal file
View File

@ -0,0 +1,54 @@
package day_10_test
import (
"advent_of_code_2024/day_10"
_ "embed"
"testing"
)
//go:embed example_data.txt
var ExampleData string
//go:embed example_data_01.txt
var ExampleData01 string
//go:embed example_data_02.txt
var ExampleData02 string
//go:embed example_data_03.txt
var ExampleData03 string
func TestBasicSolutionExample(t *testing.T) {
result := day_10.SolveBasic(ExampleData)
if result != 36 {
t.Fatalf("Expected 36 received %d", result)
}
}
func TestBasicSolutionExample01(t *testing.T) {
result := day_10.SolveBasic(ExampleData01)
if result != 2 {
t.Fatalf("Expected 2 received %d", result)
}
}
func TestBasicSolutionExample02(t *testing.T) {
result := day_10.SolveBasic(ExampleData02)
if result != 3 {
t.Fatalf("Expected 3 received %d", result)
}
}
func TestBasicSolutionExample03(t *testing.T) {
result := day_10.SolveBasic(ExampleData03)
if result != 4 {
t.Fatalf("Expected 4 received %d", result)
}
}
func TestComplexSolutionExample(t *testing.T) {
result := day_10.SolveComplex(ExampleData)
if result != 2858 {
t.Fatalf("Expected 2858 received %d", result)
}
}

View File

@ -10,6 +10,7 @@ import (
"advent_of_code_2024/day_07"
"advent_of_code_2024/day_08"
"advent_of_code_2024/day_09"
"advent_of_code_2024/day_10"
_ "embed"
"fmt"
"os"
@ -55,6 +56,10 @@ func main() {
fmt.Printf("Day 09. Basic: %d\n", day_09.SolveBasic(Day09Data)) // 20665830408335
case "09-complex":
fmt.Printf("Day 09. Complex: %d\n", day_09.SolveComplex(Day09Data)) // 20665830408335
case "10-basic":
fmt.Printf("Day 10. Basic: %d\n", day_10.SolveBasic(Day10Data)) // 20665830408335
case "10-complex":
fmt.Printf("Day 10. Complex: %d\n", day_10.SolveComplex(Day10Data)) // 20665830408335
}
}