2 changed files with 126 additions and 0 deletions
			
			
		@ -0,0 +1,81 @@ | 
				
			|||
package main | 
				
			|||
 | 
				
			|||
import ( | 
				
			|||
	"bufio" | 
				
			|||
	"fmt" | 
				
			|||
	"io" | 
				
			|||
	"strconv" | 
				
			|||
	"strings" | 
				
			|||
) | 
				
			|||
 | 
				
			|||
type day05 struct{} | 
				
			|||
 | 
				
			|||
type seedmap struct { | 
				
			|||
	original int64 | 
				
			|||
	previous int64 | 
				
			|||
	current  int64 | 
				
			|||
} | 
				
			|||
 | 
				
			|||
func readseeds(line string) []seedmap { | 
				
			|||
	seeds := make([]seedmap, 0, 10) | 
				
			|||
	scanner := bufio.NewScanner(strings.NewReader(line)) | 
				
			|||
	scanner.Split(bufio.ScanWords) | 
				
			|||
 | 
				
			|||
	for scanner.Scan() { | 
				
			|||
		val, _ := strconv.ParseInt(scanner.Text(), 10, 64) | 
				
			|||
		seeds = append(seeds, seedmap{original: val, current: val, previous: val}) | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	return seeds | 
				
			|||
} | 
				
			|||
 | 
				
			|||
func updateseeds(line string, seeds []seedmap) []seedmap { | 
				
			|||
	input := strings.SplitN(line, " ", 3) | 
				
			|||
	dst, _ := strconv.ParseInt(input[0], 10, 64) | 
				
			|||
	src, _ := strconv.ParseInt(input[1], 10, 64) | 
				
			|||
	rng, _ := strconv.ParseInt(input[2], 10, 64) | 
				
			|||
 | 
				
			|||
	for i, seed := range seeds { | 
				
			|||
		if seed.current >= src && seed.current <= src+rng && seed.current == seed.previous { | 
				
			|||
			seeds[i].current = seed.current + (dst - src) | 
				
			|||
		} | 
				
			|||
	} | 
				
			|||
	return seeds | 
				
			|||
} | 
				
			|||
 | 
				
			|||
func minint64(x, y int64) int64 { | 
				
			|||
	if x > y { | 
				
			|||
		return y | 
				
			|||
	} | 
				
			|||
	return x | 
				
			|||
} | 
				
			|||
 | 
				
			|||
func (d day05) solve1(r io.Reader) string { | 
				
			|||
	scanner := bufio.NewScanner(r) | 
				
			|||
 | 
				
			|||
	scanner.Scan() | 
				
			|||
	_, seedline, _ := strings.Cut(scanner.Text(), ": ") | 
				
			|||
	seeds := readseeds(seedline) | 
				
			|||
 | 
				
			|||
	for scanner.Scan() { | 
				
			|||
		line := scanner.Text() | 
				
			|||
		if strings.HasSuffix(line, "map:") || line == "" { | 
				
			|||
			for i := range seeds { | 
				
			|||
				seeds[i].previous = seeds[i].current | 
				
			|||
			} | 
				
			|||
			continue | 
				
			|||
		} | 
				
			|||
		seeds = updateseeds(scanner.Text(), seeds) | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	min := minint64(seeds[0].current, seeds[1].current) | 
				
			|||
	for i := 2; i < len(seeds); i++ { | 
				
			|||
		min = minint64(min, seeds[i].current) | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	return fmt.Sprintf("%d", min) | 
				
			|||
} | 
				
			|||
 | 
				
			|||
func (d day05) solve2(r io.Reader) string { | 
				
			|||
	return "incomplete" | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue