From 4b8c15876e85ce637976968f57600c6975288b5c Mon Sep 17 00:00:00 2001 From: Ang Chin Han Date: Wed, 4 Dec 2024 00:11:27 +0800 Subject: [PATCH] feat(2023-05): initial brute force --- angch/2023-05/main.go | 126 +++++++++++++++++++++++++++++++++++++++++ angch/2023-05/test.txt | 33 +++++++++++ 2 files changed, 159 insertions(+) create mode 100644 angch/2023-05/main.go create mode 100644 angch/2023-05/test.txt diff --git a/angch/2023-05/main.go b/angch/2023-05/main.go new file mode 100644 index 0000000..3c93463 --- /dev/null +++ b/angch/2023-05/main.go @@ -0,0 +1,126 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strconv" + "strings" +) + +type Range struct { + Dst int + Src int + Range int +} +type Records []Range +type MapRecord struct { + From string + To string + Records Records +} + +func (r *Records) Translate(i int) int { + for _, v := range *r { + // log.Println("Check", v.Src, v.Src+v.Range, i) + if i >= v.Src && i < v.Src+v.Range { + return v.Dst + i - v.Src + } + } + return i +} + +func findLocation(maps map[string]MapRecord, v int) int{ + curType := "seed" + for curType != "location" { + // fmt.Printf("%s %d, ", curType, v) + r := maps[curType].Records + v = r.Translate(v) + curType = maps[curType].To + } + return v + // fmt.Printf("%s %d, ", curType, v) + // fmt.Println() +} + +func day5(file string) (part1, part2 int) { + f, _ := os.Open(file) + defer f.Close() + scanner := bufio.NewScanner(f) + + seeds := []int{} + maps := make(map[string]MapRecord) + scanner.Scan() + t := scanner.Text() + for _, v := range strings.Fields(t) { + if v != "seeds:" { + a, err := strconv.Atoi(v) + if err == nil { + seeds = append(seeds, a) + } + } + } + + for scanner.Scan() { + t := scanner.Text() + if strings.HasSuffix(t, " map:") { + t = strings.TrimSuffix(t, " map:") + from, to, ok := strings.Cut(t, "-to-") + if !ok { + continue + } + r := Records{} + for scanner.Scan() { + t := scanner.Text() + if t == "" { + break + } + rng := Range{} + fmt.Sscanf(t, "%d %d %d", &rng.Dst, &rng.Src, &rng.Range) + r = append(r, rng) + } + // log.Printf("%+v\n", r) + maps[from] = MapRecord{ + From: from, + To: to, + Records: r, + } + } + } + _ = maps + + lowest := -1 + for _, v := range seeds { + v =findLocation(maps, v) + if lowest == -1 || v < lowest { + lowest = v + } + // log.Println(lowest) + } + part1 = lowest + + lowest = -1 + for i := 0; i < len(seeds); i+=2 { + start := seeds[i] + rng := seeds[i+1] + for j := start; j < start+rng; j++ { + v := findLocation(maps, j) + if lowest == -1 || v < lowest { + lowest = v + } + } + } + part2 = lowest + + return +} + +func main() { + log.SetFlags(log.Lshortfile | log.LstdFlags) + part1, part2 := day5("test.txt") + if part1 != 35 || part2 != 46 { + log.Fatal("Test failed ", part1, part2) + } + fmt.Println(day5("input.txt")) +} diff --git a/angch/2023-05/test.txt b/angch/2023-05/test.txt new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/angch/2023-05/test.txt @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file