From 1aa90ce2504fa5ca46dbc78218aa14915da96004 Mon Sep 17 00:00:00 2001 From: Ang Chin Han Date: Tue, 10 Dec 2024 14:50:55 +0800 Subject: [PATCH] feat(angch/2024-10): Initial --- angch/2024-10/main.go | 72 ++++++++++++++++++++++++++++++++++++++++++ angch/2024-10/test.txt | 8 +++++ 2 files changed, 80 insertions(+) create mode 100644 angch/2024-10/main.go create mode 100644 angch/2024-10/test.txt diff --git a/angch/2024-10/main.go b/angch/2024-10/main.go new file mode 100644 index 0000000..956b691 --- /dev/null +++ b/angch/2024-10/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "time" +) + +func day10(file string) (part1, part2 int) { + f, err := os.Open(file) + if err != nil { + log.Fatal(err) + } + defer f.Close() + scanner := bufio.NewScanner(f) + + mm := map[[2]int]int{} + mm2 := map[int]map[[2]int]bool{} + + y := 0 + for scanner.Scan() { + t := scanner.Text() + + for x, k := range t { + c := int(k - '0') + mm[[2]int{x, y}] = c + _, ok := mm2[c] + if !ok { + mm2[c] = make(map[[2]int]bool) + } + mm2[c][[2]int{x, y}] = true + } + y++ + } + + for v, _ := range mm2[0] { + paths := map[[2]int]int{v: 1} + for h := 1; h <= 9; h++ { + npaths := map[[2]int]int{} + for v, _ := range paths { + for p, _ := range mm2[h] { + dx := max(p[0], v[0]) - min(p[0], v[0]) + dy := max(p[1], v[1]) - min(p[1], v[1]) + if dx+dy == 1 { + npaths[p] += paths[v] + } + } + } + paths = npaths + } + part1 += len(paths) + for _, v := range paths { + part2 += v + } + } + + return +} + +func main() { + log.SetFlags(log.Lshortfile | log.LstdFlags) + t1 := time.Now() + part1, part2 := day10("test.txt") + fmt.Println(part1, part2) + if part1 != 36 || part2 != 81 { + log.Fatal("Test failed ", part1, part2) + } + fmt.Println(day10("input.txt")) + fmt.Println("Elapsed time:", time.Since(t1)) +} diff --git a/angch/2024-10/test.txt b/angch/2024-10/test.txt new file mode 100644 index 0000000..7bb1248 --- /dev/null +++ b/angch/2024-10/test.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 \ No newline at end of file