Code of the Advent kind, maybe in Go this year
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.1 KiB

12 months ago
package main
import (
"bufio"
"fmt"
"os"
"strings"
"strconv"
)
type maprange struct {
src uint64
dst uint64
}
type mapval struct {
cunt uint64
rnge maprange
}
type mapping struct {
src string
dst string
12 months ago
val []mapval
}
func parse(f *os.File) (seeds []uint64, maps map[string]mapping) {
12 months ago
s := bufio.NewScanner(f)
12 months ago
maps = make(map[string]mapping)
12 months ago
inmap := false
var curmap mapping
12 months ago
for s.Scan() {
12 months ago
line := s.Text()
12 months ago
switch {
case strings.HasPrefix(line, "seeds: "):
thing := strings.Split(line[7:], " ")
12 months ago
for _, v := range thing {
val, _ := strconv.ParseUint(v, 0, 64)
12 months ago
seeds = append(seeds, val)
12 months ago
}
12 months ago
case strings.HasSuffix(line, " map:"):
if inmap { maps[curmap.src] = curmap }
12 months ago
curmap = mapping{}
inmap = true
split1 := strings.Index(line, "-to-")
split2 := strings.Index(line, " ")
src := line[:split1]
dst := line[split1 + 4:split2]
curmap.src = src
curmap.dst = dst
12 months ago
case strings.Count(line, " ") >= 2 && inmap:
thing := strings.Split(line, " ")
var val mapval
val.rnge.dst, _ = strconv.ParseUint(thing[0], 0, 64)
val.rnge.src, _ = strconv.ParseUint(thing[1], 0, 64)
val.cunt, _ = strconv.ParseUint(thing[2], 0, 64)
curmap.val = append(curmap.val, val)
}
12 months ago
}
if inmap { maps[curmap.src] = curmap }
12 months ago
12 months ago
return
}
func part1(init_seeds []uint64, maps map[string]mapping) uint64 {
12 months ago
seeds := append(make([]uint64, 0, len(init_seeds)), init_seeds...)
12 months ago
12 months ago
currency := "seed"
for currency != "location" {
map_cur := maps[currency]
currency = map_cur.dst
12 months ago
for i, v := range seeds {
for _, x := range map_cur.val {
tmp := v - x.rnge.src
if tmp > 0 && tmp < x.cunt {
v += x.rnge.dst - x.rnge.src
break
}
}
seeds[i] = v
}
}
12 months ago
min := seeds[0]
12 months ago
for _, v := range seeds[1:] {
12 months ago
if v < min { min = v }
12 months ago
}
12 months ago
return min
}
12 months ago
func part2(init_seeds []uint64, maps map[string]mapping) uint64 {
12 months ago
seeds := make([]maprange, 0)
12 months ago
for i, _ := range init_seeds {
if i % 2 == 1 { continue }
var rnge maprange
rnge.src = init_seeds[i + 0]
rnge.dst = rnge.src + init_seeds[i + 1]
12 months ago
seeds = append(seeds, rnge)
12 months ago
}
12 months ago
currency := "seed"
12 months ago
for currency != "location" {
map_cur := maps[currency]
currency = map_cur.dst
12 months ago
newseedrnge := make([]maprange, 0)
12 months ago
for _, v := range seeds {
12 months ago
for _, x := range map_cur.val {
src := x.rnge.src
end := x.rnge.src + x.cunt
if src <= v.src && v.src < end && src <= v.dst && v.dst < end {
newseedrnge = append(newseedrnge, maprange{
src: v.src + x.rnge.dst - x.rnge.src,
dst: v.dst + x.rnge.dst - x.rnge.src,
})
v.src = v.dst
} else if src <= v.src && v.src < end {
newseedrnge = append(newseedrnge, maprange{
src: v.src + x.rnge.dst - x.rnge.src,
dst: x.rnge.dst + x.cunt,
})
v.src = end
} else if src <= v.dst && v.dst < end {
newseedrnge = append(newseedrnge, maprange{
src: x.rnge.dst,
dst: v.dst + x.rnge.dst - x.rnge.src,
})
v.dst = src
}
if v.src == v.dst { break }
}
if v.src != v.dst { newseedrnge = append(newseedrnge, v) }
}
12 months ago
seeds = newseedrnge
12 months ago
}
min := seeds[0].src
12 months ago
for _, v := range seeds[1:] {
if v.src < min { min = v.src }
12 months ago
}
return min
12 months ago
}
func main() {
f, _ := os.Open(os.Args[1])
seeds, maps := parse(f)
fmt.Println(part1(seeds, maps))
fmt.Println(part2(seeds, maps))
12 months ago
}