add day 10

This commit is contained in:
2024-12-10 09:29:26 +01:00
parent f6228a4fd8
commit 57b849016a
3 changed files with 266 additions and 0 deletions

53
day10/data.txt Normal file
View File

@@ -0,0 +1,53 @@
45678701021256787218432154301232100012334301023456789
32569892430543298909845067210145621965421012310545869
01210743549612187610756778143296534874310123458930978
12323651258703066525643889050387546789210234567821567
01434210367012178434512918761236695694391349650131054
12544303438124569232101109678945784321487658743232343
43695496549433450143001234532034653210156961234589787
94786987834342100154519873541128763165432870234671096
85677889929854343267610565690639454076501210165692345
76012870010767256998701234788748348987432101156787654
01043961171258107887898345659654239858901101089810123
32154552987349016576987454564980108765432232123209874
43960143476987657607876523875676501678921349854112365
54871232564890548510965014934189432578900458963012453
69890121465431239423454876821054329657811467874501412
78781230656322102347623945498765018746324320189432303
45610945567212121098510130340121201235435410234534564
44327876438901010101498321233290345110346761809621875
34387654323432129812367321044789876011289898918760976
45297890012343456703455433445602345895670767823451987
56187781201278914567526932563211056734321296744589854
67096654302107803498017801074787654321234585430076763
78945109213456012332101301985698543210987676121125892
21032238376788768945432452394987650121789678032434981
32561247487699854876983345401276345430678549140123470
23470056794521003123476236982345036781565432101210565
14980129873430412001569107810034129092634307870389874
05691234562541343432018098941125678104321216921010123
06788765101632234589127657832103543219450325432167012
12109453210762103678934566543012354308765014703458983
43898344789899872100129875414983989412894327812565410
56701235692198561091223014305894876543781016945678320
12345106541085432782014323216765321789692345238769801
01416787632176306654105450125601450694547654199654432
12109898501201217653296961234702364543498703080123569
01234549654323898741787870149810676032107012678034078
67899678760015677230765487654321980121978710569985127
58908707871234982101896398923450890120879623450276434
43211216910123878982363210110961051234566542141105589
52890345034987965985476545607872340345651033032234676
01761212125675654876983432787401456978762122345897655
10354305430234503210012301294301967869887831056798587
23487416521105012342121345385210876778896990987123496
96596547012276109653010256106321236569045781234012345
87432108983489298764560187287810345652134650965421004
76545017698548345675078894396923456743221045874540218
89632123087632210982189123405410567892100038973234389
56749834128901043983458016512321098754321122980198476
43898765439456712276567087695632347665430101076567567
32109650169329803125690198787541056578920122345445898
78980543278019874034787239645670567810110233410336765
65211230165216565129876543532789436921898398561221234
34302321254305653210210123401890125432765487652310123

104
day10/ratings.go Normal file
View File

@@ -0,0 +1,104 @@
package main
import (
"os"
"fmt"
"strconv"
"strings"
)
// data types
type Path struct {
start_x int
start_y int
end_x int
end_y int
}
// general util
func check(e error) {
if e != nil { panic(e) }
}
func get_next_steps(field [][]byte, path *Path) []Path {
paths := make([]Path, 0, 4)
x := path.end_x
y := path.end_y
val := field[y][x]
if y < len(field) - 1 {
down := field[y + 1][x]
if down == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x, end_y: y + 1 })
}
}
if y > 0 {
up := field[y - 1][x]
if up == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x, end_y: y - 1 })
}
}
if x < len(field[0]) - 1 {
right := field[y][x + 1]
if right == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x + 1, end_y: y })
}
}
if x > 0 {
left := field[y][x - 1]
if left == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x - 1, end_y: y })
}
}
return paths
}
func get_all_paths(field [][]byte) []Path {
paths := make([]Path, 0)
// init
for _y, line := range field {
for _x, val := range line {
if val == 0 {
paths = append(paths, Path{start_x: _x, end_x: _x, start_y: _y, end_y: _y})
}
}
}
// step by step
for i := 0; i < 9; i++ {
new_paths := make([]Path, 0, len(paths))
for _, path := range paths {
new_paths = append(new_paths, get_next_steps(field, &path)...)
}
paths = new_paths
}
return paths
}
// main loop
func main() {
// read in file
dat, err := os.ReadFile("data.txt")
check(err)
dat_str := strings.TrimSpace(string(dat))
// contruct input as byte matrix
lines := strings.Split(dat_str, "\n")
input := make([][]byte, 0, len(lines))
for _, line := range lines {
matrix_line := make([]byte, 0, len(line))
for _, char := range []rune(line) {
num, err := strconv.Atoi(string(char))
check(err)
matrix_line = append(matrix_line, byte(num))
}
input = append(input, matrix_line)
}
// calc rating
fmt.Println(len(get_all_paths(input)))
}

109
day10/scores.go Normal file
View File

@@ -0,0 +1,109 @@
package main
import (
"os"
"fmt"
"strconv"
"strings"
)
// data types
type Path struct {
start_x int
start_y int
end_x int
end_y int
}
// general util
func check(e error) {
if e != nil { panic(e) }
}
func get_next_steps(field [][]byte, path *Path) []Path {
paths := make([]Path, 0, 4)
x := path.end_x
y := path.end_y
val := field[y][x]
if y < len(field) - 1 {
down := field[y + 1][x]
if down == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x, end_y: y + 1 })
}
}
if y > 0 {
up := field[y - 1][x]
if up == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x, end_y: y - 1 })
}
}
if x < len(field[0]) - 1 {
right := field[y][x + 1]
if right == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x + 1, end_y: y })
}
}
if x > 0 {
left := field[y][x - 1]
if left == val + 1 {
paths = append(paths, Path{ start_x: path.start_x, start_y: path.start_y, end_x: x - 1, end_y: y })
}
}
return paths
}
func get_all_paths(field [][]byte) []Path {
paths := make([]Path, 0)
// init
for _y, line := range field {
for _x, val := range line {
if val == 0 {
paths = append(paths, Path{start_x: _x, end_x: _x, start_y: _y, end_y: _y})
}
}
}
// step by step
for i := 0; i < 9; i++ {
new_paths := make([]Path, 0, len(paths))
for _, path := range paths {
new_paths = append(new_paths, get_next_steps(field, &path)...)
}
paths = new_paths
}
return paths
}
// main loop
func main() {
// read in file
dat, err := os.ReadFile("data.txt")
check(err)
dat_str := strings.TrimSpace(string(dat))
// contruct input as byte matrix
lines := strings.Split(dat_str, "\n")
input := make([][]byte, 0, len(lines))
for _, line := range lines {
matrix_line := make([]byte, 0, len(line))
for _, char := range []rune(line) {
num, err := strconv.Atoi(string(char))
check(err)
matrix_line = append(matrix_line, byte(num))
}
input = append(input, matrix_line)
}
// calc scores
m := make(map[Path]bool)
for _, path := range get_all_paths(input) {
m[path] = true
}
fmt.Println(len(m))
}