adventofcode/2021/day07/main.go

80 lines
1.4 KiB
Go

package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := strings.Split(h.StdinToString(), ",")
part1(inp)
fmt.Println()
part2(inp)
}
func part1(inp []string) {
fmt.Println("# Part 1")
var crabs []int
min := h.MAX_INT
max := h.MIN_INT
crabMap := make(map[int]int)
for i := range inp {
c := h.Atoi(inp[i])
if c < min {
min = c
}
if c > max {
max = c
}
crabMap[c] = crabMap[c] + 1
crabs = append(crabs, c)
}
fuelMap := make(map[int]int)
var minFuelMov int
minFuelVal := h.MAX_INT
for i := min; i <= max; i++ {
for c := range crabMap {
fuelMap[i] += crabMap[c] * h.Abs(i-c)
}
if fuelMap[i] < minFuelVal {
minFuelMov = i
minFuelVal = fuelMap[i]
}
}
fmt.Println("Minimum Fuel Requirement:", minFuelVal, "at", minFuelMov)
}
func part2(inp []string) {
fmt.Println("# Part 2")
var crabs []int
min := h.MAX_INT
max := h.MIN_INT
crabMap := make(map[int]int)
for i := range inp {
c := h.Atoi(inp[i])
if c < min {
min = c
}
if c > max {
max = c
}
crabMap[c] = crabMap[c] + 1
crabs = append(crabs, c)
}
fuelMap := make(map[int]int)
var minFuelMov int
minFuelVal := h.MAX_INT
for i := min; i <= max; i++ {
for c := range crabMap {
fuelMap[i] += crabMap[c] * h.Fact(h.Abs(i-c))
}
if fuelMap[i] < minFuelVal {
minFuelMov = i
minFuelVal = fuelMap[i]
}
}
fmt.Println("Minimum Fuel Requirement:", minFuelVal, "at", minFuelMov)
}