diff --git a/cmd/05/input.txt b/cmd/05/input.txt new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/cmd/05/input.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 diff --git a/cmd/05/main.go b/cmd/05/main.go new file mode 100644 index 0000000..e02b4b6 --- /dev/null +++ b/cmd/05/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + + utils "git.anthonygueguen.fr/g3po/aoc2023/internal" + day05 "git.anthonygueguen.fr/g3po/aoc2023/internal/05" +) + +func main() { + content := utils.Init() + + fmt.Println("Part 1") + + result := day05.GetShortestLocation(content) + fmt.Printf("Result: %d\n", result) + fmt.Println("Part 2") + + // fmt.Printf("Result: %d\n", result[1]) +} diff --git a/internal/05/05.go b/internal/05/05.go new file mode 100644 index 0000000..37f821d --- /dev/null +++ b/internal/05/05.go @@ -0,0 +1,46 @@ +package day05 + +import ( + "fmt" + "math" + "regexp" + "strconv" + + utils "git.anthonygueguen.fr/g3po/aoc2023/internal" +) + +func GetShortestLocation(content []string) int64 { + seeds := make(map[int64]int64) + minLocation := int64(math.MaxInt64) + re := regexp.MustCompile(`\d+`) + for _, val := range re.FindAllString(content[0], -1) { + num := utils.Must(strconv.ParseInt(val, 10, 64)) + seeds[num] = num + } + + for i := 1; i < len(content); i++ { + matches := re.FindAllString(content[i], -1) + if len(matches) != 3 { + continue + } + fmt.Println(seeds) + fmt.Println(matches) + var num []int64 + for _, val := range matches { + num = append(num, utils.Must(strconv.ParseInt(val, 10, 64))) + } + + for k, v := range seeds { + if v >= num[1] && v < num[1]+num[2] { + seeds[k] = num[0] + (v - num[1]) + } + } + + } + for _, v := range seeds { + if v < minLocation { + minLocation = v + } + } + return minLocation +}