From: Alan R Evans Date: Tue, 5 Dec 2023 21:20:05 +0000 (+0000) Subject: day05 X-Git-Url: https://fbox.kageds.com/gitweb/adventofcode.git/commitdiff_plain/52105f4958d89d7d68630b9f2a41e08f6d5b31b1?ds=sidebyside;hp=56571eadf0ea7e737cec64c0691098ba70f77979 day05 --- diff --git a/2023/go/day05/__debug_bin1342718261 b/2023/go/day05/__debug_bin1342718261 new file mode 100755 index 0000000..ea895ae Binary files /dev/null and b/2023/go/day05/__debug_bin1342718261 differ diff --git a/2023/go/day05/day05.go b/2023/go/day05/day05.go new file mode 100644 index 0000000..902eb43 --- /dev/null +++ b/2023/go/day05/day05.go @@ -0,0 +1,211 @@ +package day05 + +import ( + + _ "regexp" + "strings" + + "fmt" + + "adventofcode2023/utils" + +) + +type Map struct { + dest_range_start int + src_range_start int + range_len int +} + +type Almanac struct { + seed2soil []Map + soil2fert []Map + fert2water []Map + water2light []Map + light2temp []Map + temp2humid []Map + humid2loc []Map +} + +type SeedMap struct { + seed int + range_len int +} + +func Part1(input string) int { + seeds, almanac, _ := parseInput1(input) + fmt.Println(seeds) + fmt.Println(almanac) + + minLoc := utils.MaxInt + for _, seed := range seeds { + soil := lookup_dest(seed, almanac.seed2soil) + fert := lookup_dest(soil, almanac.soil2fert) + water := lookup_dest(fert, almanac.fert2water) + light := lookup_dest(water, almanac.water2light) + temp := lookup_dest(light, almanac.light2temp) + humid := lookup_dest(temp, almanac.temp2humid) + loc := lookup_dest(humid, almanac.humid2loc) + if loc < minLoc { + minLoc = loc + } + fmt.Println(loc) + } + + return minLoc +} + +func Part2(input string) int { + seedsmap, almanac, _ := parseInput2(input) + fmt.Println(seedsmap) + fmt.Println(almanac) + minLocMap := Map{dest_range_start: utils.MaxInt} + for _, x := range almanac.humid2loc { + if x.dest_range_start< minLocMap.dest_range_start { + minLocMap = x + } + } + fmt.Println(minLocMap.dest_range_start) + + for i:=minLocMap.src_range_start;i= dataMap.src_range_start && entity <= dataMap.src_range_start + dataMap.range_len { + return dataMap.dest_range_start + entity - dataMap.src_range_start + } + } + return entity +} + +func lookup_src(entity int, data []Map) int { + for _, dataMap := range data { + if entity >= dataMap.dest_range_start && entity <= dataMap.dest_range_start + dataMap.range_len { + return dataMap.src_range_start + entity - dataMap.dest_range_start + } + } + return entity +} + +func parseInput1(input string) ([]int, Almanac, error) { + + var almanac Almanac + var seeds []int + + lines := strings.Split(input, "\n") + seedLine := string(lines[0]) + tokens := strings.Fields(seedLine) + for _, token := range tokens[1:] { + seeds = append(seeds, utils.MustAtoi(token)) + } + + blocks := strings.Split(input, "\n\n") + for i, block := range blocks { + lines := strings.Split(block, "\n") + switch i { + case 0: + continue + case 1: + almanac.seed2soil = getMap(lines[1:]) + case 2: + almanac.soil2fert = getMap(lines[1:]) + case 3: + almanac.fert2water = getMap(lines[1:]) + case 4: + almanac.water2light = getMap(lines[1:]) + case 5: + almanac.light2temp = getMap(lines[1:]) + case 6: + almanac.temp2humid = getMap(lines[1:]) + case 7: + almanac.humid2loc = getMap(lines[1:]) + } + } + return seeds, almanac, nil +} + +func parseInput2(input string) ([]SeedMap, Almanac, error) { + + var almanac Almanac + var seeds []SeedMap + + lines := strings.Split(input, "\n") + seedLine := string(lines[0]) + tokens := strings.Fields(seedLine) + for i:=1;i