10 "adventofcode2023/utils"
35 func Part1(input string) int {
36 seeds, almanac, _ := parseInput1(input)
40 minLoc := int(utils.MaxInt)
41 for _, seed := range seeds {
42 soil := lookup_dest(seed, almanac.seed2soil)
43 fert := lookup_dest(soil, almanac.soil2fert)
44 water := lookup_dest(fert, almanac.fert2water)
45 light := lookup_dest(water, almanac.water2light)
46 temp := lookup_dest(light, almanac.light2temp)
47 humid := lookup_dest(temp, almanac.temp2humid)
48 loc := lookup_dest(humid, almanac.humid2loc)
58 func Part2(input string) int {
59 seedsmap, almanac, _ := parseInput2(input)
61 for i:=0;i<utils.MaxInt;i++{
62 humid := lookup_src(int(i), almanac.humid2loc)
63 temp := lookup_src(humid, almanac.temp2humid)
64 light := lookup_src(temp, almanac.light2temp)
65 water := lookup_src(light, almanac.water2light)
66 fert := lookup_src(water, almanac.fert2water)
67 soil := lookup_src(fert, almanac.soil2fert)
68 seed := lookup_src(soil, almanac.seed2soil)
70 for _, seedmap := range seedsmap {
71 if seed >= seedmap.seed && seed < seedmap.seed+seedmap.range_len {
79 func lookup_dest(entity int, data []Map) int {
80 for _, dataMap := range data {
81 if entity >= dataMap.src_range_start && entity <= dataMap.src_range_start + dataMap.range_len {
82 return dataMap.dest_range_start + entity - dataMap.src_range_start
88 func lookup_src(entity int, data []Map) int {
89 for _, dataMap := range data {
90 if entity >= dataMap.dest_range_start && entity <= dataMap.dest_range_start + dataMap.range_len {
91 return dataMap.src_range_start + entity - dataMap.dest_range_start
97 func parseInput1(input string) ([]int, Almanac, error) {
102 lines := strings.Split(input, "\n")
103 seedLine := string(lines[0])
104 tokens := strings.Fields(seedLine)
105 for _, token := range tokens[1:] {
106 seeds = append(seeds, int(utils.MustAtoi(token)))
109 blocks := strings.Split(input, "\n\n")
110 for i, block := range blocks {
111 lines := strings.Split(block, "\n")
116 almanac.seed2soil = getMap(lines[1:])
118 almanac.soil2fert = getMap(lines[1:])
120 almanac.fert2water = getMap(lines[1:])
122 almanac.water2light = getMap(lines[1:])
124 almanac.light2temp = getMap(lines[1:])
126 almanac.temp2humid = getMap(lines[1:])
128 almanac.humid2loc = getMap(lines[1:])
131 return seeds, almanac, nil
134 func parseInput2(input string) ([]SeedMap, Almanac, error) {
139 lines := strings.Split(input, "\n")
140 seedLine := string(lines[0])
141 tokens := strings.Fields(seedLine)
142 for i:=1;i<len(tokens);i=i+2 {
143 seedStart := int(utils.MustAtoi(tokens[i]))
144 seedRange := int(utils.MustAtoi(tokens[i+1]))
145 seeds = append(seeds, SeedMap{seedStart, seedRange})
148 blocks := strings.Split(input, "\n\n")
149 for i, block := range blocks {
150 lines := strings.Split(block, "\n")
155 almanac.seed2soil = getMap(lines[1:])
157 almanac.soil2fert = getMap(lines[1:])
159 almanac.fert2water = getMap(lines[1:])
161 almanac.water2light = getMap(lines[1:])
163 almanac.light2temp = getMap(lines[1:])
165 almanac.temp2humid = getMap(lines[1:])
167 almanac.humid2loc = getMap(lines[1:])
170 return seeds, almanac, nil
173 func getMap(input []string) []Map {
175 for _, line := range input {
176 tokens := strings.Fields(line)
177 out = append(out, Map{int(utils.MustAtoi(tokens[0])), int(utils.MustAtoi(tokens[1])),int(utils.MustAtoi(tokens[2]))})