10 "adventofcode2023/utils"
35 func Part1(input string) int {
36 seeds, almanac, _ := parseInput1(input)
40 minLoc := 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)
62 minLocMap := Map{dest_range_start: utils.MaxInt}
63 for _, x := range almanac.humid2loc {
64 if x.dest_range_start< minLocMap.dest_range_start {
68 fmt.Println(minLocMap.dest_range_start)
70 for i:=minLocMap.src_range_start;i<minLocMap.src_range_start+minLocMap.range_len;i++ {
71 temp := lookup_src(i, almanac.temp2humid)
75 // minSeedMap := SeedMap{}
76 // minLoc := utils.MaxInt
77 // for _, seedmap := range seedsmap {
78 // for _, i := range []int{seedmap.seed, seedmap.seed+seedmap.range_len} {
79 // soil := lookup_dest(i, almanac.seed2soil)
80 // fert := lookup_dest(soil, almanac.soil2fert)
81 // water := lookup_dest(fert, almanac.fert2water)
82 // light := lookup_dest(water, almanac.water2light)
83 // temp := lookup_dest(light, almanac.light2temp)
84 // humid := lookup_dest(temp, almanac.temp2humid)
85 // loc := lookup_dest(humid, almanac.humid2loc)
87 // minSeedMap = seedmap
93 // fmt.Println(minSeedMap)
94 // minLoc = utils.MaxInt
95 // for i:=minSeedMap.seed;i<minSeedMap.seed+minSeedMap.range_len;i++ {
96 // soil := lookup_dest(i, almanac.seed2soil)
97 // fert := lookup_dest(soil, almanac.soil2fert)
98 // water := lookup_dest(fert, almanac.fert2water)
99 // light := lookup_dest(water, almanac.water2light)
100 // temp := lookup_dest(light, almanac.light2temp)
101 // humid := lookup_dest(temp, almanac.temp2humid)
102 // loc := lookup_dest(humid, almanac.humid2loc)
110 func lookup_dest(entity int, data []Map) int {
111 for _, dataMap := range data {
112 if entity >= dataMap.src_range_start && entity <= dataMap.src_range_start + dataMap.range_len {
113 return dataMap.dest_range_start + entity - dataMap.src_range_start
119 func lookup_src(entity int, data []Map) int {
120 for _, dataMap := range data {
121 if entity >= dataMap.dest_range_start && entity <= dataMap.dest_range_start + dataMap.range_len {
122 return dataMap.src_range_start + entity - dataMap.dest_range_start
128 func parseInput1(input string) ([]int, Almanac, error) {
133 lines := strings.Split(input, "\n")
134 seedLine := string(lines[0])
135 tokens := strings.Fields(seedLine)
136 for _, token := range tokens[1:] {
137 seeds = append(seeds, utils.MustAtoi(token))
140 blocks := strings.Split(input, "\n\n")
141 for i, block := range blocks {
142 lines := strings.Split(block, "\n")
147 almanac.seed2soil = getMap(lines[1:])
149 almanac.soil2fert = getMap(lines[1:])
151 almanac.fert2water = getMap(lines[1:])
153 almanac.water2light = getMap(lines[1:])
155 almanac.light2temp = getMap(lines[1:])
157 almanac.temp2humid = getMap(lines[1:])
159 almanac.humid2loc = getMap(lines[1:])
162 return seeds, almanac, nil
165 func parseInput2(input string) ([]SeedMap, Almanac, error) {
170 lines := strings.Split(input, "\n")
171 seedLine := string(lines[0])
172 tokens := strings.Fields(seedLine)
173 for i:=1;i<len(tokens);i=i+2 {
174 seedStart := utils.MustAtoi(tokens[i])
175 seedRange := utils.MustAtoi(tokens[i+1])
176 seeds = append(seeds, SeedMap{seedStart, seedRange})
179 blocks := strings.Split(input, "\n\n")
180 for i, block := range blocks {
181 lines := strings.Split(block, "\n")
186 almanac.seed2soil = getMap(lines[1:])
188 almanac.soil2fert = getMap(lines[1:])
190 almanac.fert2water = getMap(lines[1:])
192 almanac.water2light = getMap(lines[1:])
194 almanac.light2temp = getMap(lines[1:])
196 almanac.temp2humid = getMap(lines[1:])
198 almanac.humid2loc = getMap(lines[1:])
201 return seeds, almanac, nil
204 func getMap(input []string) []Map {
206 for _, line := range input {
207 tokens := strings.Fields(line)
208 out = append(out, Map{utils.MustAtoi(tokens[0]), utils.MustAtoi(tokens[1]),utils.MustAtoi(tokens[2])})