day06
This commit is contained in:
Binary file not shown.
@@ -12,9 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Map struct {
|
type Map struct {
|
||||||
dest_range_start int
|
dest_range_start uint64
|
||||||
src_range_start int
|
src_range_start uint64
|
||||||
range_len int
|
range_len uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Almanac struct {
|
type Almanac struct {
|
||||||
@@ -28,16 +28,16 @@ type Almanac struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SeedMap struct {
|
type SeedMap struct {
|
||||||
seed int
|
seed uint64
|
||||||
range_len int
|
range_len uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func Part1(input string) int {
|
func Part1(input string) uint64 {
|
||||||
seeds, almanac, _ := parseInput1(input)
|
seeds, almanac, _ := parseInput1(input)
|
||||||
fmt.Println(seeds)
|
fmt.Println(seeds)
|
||||||
fmt.Println(almanac)
|
fmt.Println(almanac)
|
||||||
|
|
||||||
minLoc := utils.MaxInt
|
minLoc := uint64(utils.MaxInt)
|
||||||
for _, seed := range seeds {
|
for _, seed := range seeds {
|
||||||
soil := lookup_dest(seed, almanac.seed2soil)
|
soil := lookup_dest(seed, almanac.seed2soil)
|
||||||
fert := lookup_dest(soil, almanac.soil2fert)
|
fert := lookup_dest(soil, almanac.soil2fert)
|
||||||
@@ -57,38 +57,24 @@ func Part1(input string) int {
|
|||||||
|
|
||||||
func Part2(input string) int {
|
func Part2(input string) int {
|
||||||
seedsmap, almanac, _ := parseInput2(input)
|
seedsmap, almanac, _ := parseInput2(input)
|
||||||
fmt.Println(seedsmap)
|
|
||||||
fmt.Println(almanac)
|
|
||||||
minLocMap := Map{dest_range_start: utils.MaxInt}
|
minLoc := uint64(utils.MaxInt)
|
||||||
for _, x := range almanac.humid2loc {
|
for _, seedmap := range seedsmap {
|
||||||
if x.dest_range_start< minLocMap.dest_range_start {
|
for i := seedmap.seed;i<seedmap.seed+seedmap.range_len;i++ {
|
||||||
minLocMap = x
|
soil := lookup_dest(i, almanac.seed2soil)
|
||||||
|
fert := lookup_dest(soil, almanac.soil2fert)
|
||||||
|
water := lookup_dest(fert, almanac.fert2water)
|
||||||
|
light := lookup_dest(water, almanac.water2light)
|
||||||
|
temp := lookup_dest(light, almanac.light2temp)
|
||||||
|
humid := lookup_dest(temp, almanac.temp2humid)
|
||||||
|
loc := lookup_dest(humid, almanac.humid2loc)
|
||||||
|
if loc < minLoc {
|
||||||
|
minLoc = loc
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Println(minLoc)
|
||||||
}
|
}
|
||||||
fmt.Println(minLocMap.dest_range_start)
|
|
||||||
|
|
||||||
for i:=minLocMap.src_range_start;i<minLocMap.src_range_start+minLocMap.range_len;i++ {
|
|
||||||
temp := lookup_src(i, almanac.temp2humid)
|
|
||||||
fmt.Println(temp)
|
|
||||||
}
|
|
||||||
|
|
||||||
// minSeedMap := SeedMap{}
|
|
||||||
// minLoc := utils.MaxInt
|
|
||||||
// for _, seedmap := range seedsmap {
|
|
||||||
// for _, i := range []int{seedmap.seed, seedmap.seed+seedmap.range_len} {
|
|
||||||
// soil := lookup_dest(i, almanac.seed2soil)
|
|
||||||
// fert := lookup_dest(soil, almanac.soil2fert)
|
|
||||||
// water := lookup_dest(fert, almanac.fert2water)
|
|
||||||
// light := lookup_dest(water, almanac.water2light)
|
|
||||||
// temp := lookup_dest(light, almanac.light2temp)
|
|
||||||
// humid := lookup_dest(temp, almanac.temp2humid)
|
|
||||||
// loc := lookup_dest(humid, almanac.humid2loc)
|
|
||||||
// if loc < minLoc {
|
|
||||||
// minSeedMap = seedmap
|
|
||||||
// minLoc = loc
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
//
|
||||||
// fmt.Println(minSeedMap)
|
// fmt.Println(minSeedMap)
|
||||||
// minLoc = utils.MaxInt
|
// minLoc = utils.MaxInt
|
||||||
@@ -104,10 +90,10 @@ func Part2(input string) int {
|
|||||||
// minLoc = loc
|
// minLoc = loc
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
return -1
|
return int(minLoc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookup_dest(entity int, data []Map) int {
|
func lookup_dest(entity uint64, data []Map) uint64 {
|
||||||
for _, dataMap := range data {
|
for _, dataMap := range data {
|
||||||
if entity >= dataMap.src_range_start && entity <= dataMap.src_range_start + dataMap.range_len {
|
if entity >= dataMap.src_range_start && entity <= dataMap.src_range_start + dataMap.range_len {
|
||||||
return dataMap.dest_range_start + entity - dataMap.src_range_start
|
return dataMap.dest_range_start + entity - dataMap.src_range_start
|
||||||
@@ -116,7 +102,7 @@ func lookup_dest(entity int, data []Map) int {
|
|||||||
return entity
|
return entity
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookup_src(entity int, data []Map) int {
|
func lookup_src(entity uint64, data []Map) uint64 {
|
||||||
for _, dataMap := range data {
|
for _, dataMap := range data {
|
||||||
if entity >= dataMap.dest_range_start && entity <= dataMap.dest_range_start + dataMap.range_len {
|
if entity >= dataMap.dest_range_start && entity <= dataMap.dest_range_start + dataMap.range_len {
|
||||||
return dataMap.src_range_start + entity - dataMap.dest_range_start
|
return dataMap.src_range_start + entity - dataMap.dest_range_start
|
||||||
@@ -125,16 +111,16 @@ func lookup_src(entity int, data []Map) int {
|
|||||||
return entity
|
return entity
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseInput1(input string) ([]int, Almanac, error) {
|
func parseInput1(input string) ([]uint64, Almanac, error) {
|
||||||
|
|
||||||
var almanac Almanac
|
var almanac Almanac
|
||||||
var seeds []int
|
var seeds []uint64
|
||||||
|
|
||||||
lines := strings.Split(input, "\n")
|
lines := strings.Split(input, "\n")
|
||||||
seedLine := string(lines[0])
|
seedLine := string(lines[0])
|
||||||
tokens := strings.Fields(seedLine)
|
tokens := strings.Fields(seedLine)
|
||||||
for _, token := range tokens[1:] {
|
for _, token := range tokens[1:] {
|
||||||
seeds = append(seeds, utils.MustAtoi(token))
|
seeds = append(seeds, uint64(utils.MustAtoi(token)))
|
||||||
}
|
}
|
||||||
|
|
||||||
blocks := strings.Split(input, "\n\n")
|
blocks := strings.Split(input, "\n\n")
|
||||||
@@ -171,8 +157,8 @@ func parseInput2(input string) ([]SeedMap, Almanac, error) {
|
|||||||
seedLine := string(lines[0])
|
seedLine := string(lines[0])
|
||||||
tokens := strings.Fields(seedLine)
|
tokens := strings.Fields(seedLine)
|
||||||
for i:=1;i<len(tokens);i=i+2 {
|
for i:=1;i<len(tokens);i=i+2 {
|
||||||
seedStart := utils.MustAtoi(tokens[i])
|
seedStart := uint64(utils.MustAtoi(tokens[i]))
|
||||||
seedRange := utils.MustAtoi(tokens[i+1])
|
seedRange := uint64(utils.MustAtoi(tokens[i+1]))
|
||||||
seeds = append(seeds, SeedMap{seedStart, seedRange})
|
seeds = append(seeds, SeedMap{seedStart, seedRange})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,7 +191,7 @@ func getMap(input []string) []Map {
|
|||||||
var out []Map
|
var out []Map
|
||||||
for _, line := range input {
|
for _, line := range input {
|
||||||
tokens := strings.Fields(line)
|
tokens := strings.Fields(line)
|
||||||
out = append(out, Map{utils.MustAtoi(tokens[0]), utils.MustAtoi(tokens[1]),utils.MustAtoi(tokens[2])})
|
out = append(out, Map{uint64(utils.MustAtoi(tokens[0])), uint64(utils.MustAtoi(tokens[1])),uint64(utils.MustAtoi(tokens[2]))})
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
71
2023/go/day06/day06.go
Normal file
71
2023/go/day06/day06.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package day06
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Input struct {
|
||||||
|
Time uint64
|
||||||
|
Distance uint64
|
||||||
|
}
|
||||||
|
func Part1(input string) int {
|
||||||
|
answer := 1.0
|
||||||
|
ins := parseInput1(input)
|
||||||
|
for _, in := range ins {
|
||||||
|
t1, t2, _ := solveQuadratic(1, float64(in.Time) * -1, float64(in.Distance))
|
||||||
|
fmt.Printf("t1: %v t2: %v ans: %v\n", t1, t2, (t1 - t2 + 1))
|
||||||
|
answer *= (t1 - t2 + 1)
|
||||||
|
|
||||||
|
}
|
||||||
|
return int(answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input string) int {
|
||||||
|
answer := 1.0
|
||||||
|
ins := parseInput2(input)
|
||||||
|
for _, in := range ins {
|
||||||
|
t1, t2, _ := solveQuadratic(1, float64(in.Time) * -1, float64(in.Distance))
|
||||||
|
fmt.Printf("t1: %v t2: %v ans: %v\n", t1, t2, (t1 - t2 + 1))
|
||||||
|
answer *= (t1 - t2 + 1)
|
||||||
|
|
||||||
|
}
|
||||||
|
return int(answer)}
|
||||||
|
|
||||||
|
func parseInput1(input string) []Input {
|
||||||
|
var in []Input
|
||||||
|
in = append(in, Input{46, 214})
|
||||||
|
in = append(in, Input{80, 1177})
|
||||||
|
in = append(in, Input{78, 1402})
|
||||||
|
in = append(in, Input{66, 1024})
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseInput2(input string) []Input {
|
||||||
|
var in []Input
|
||||||
|
in = append(in, Input{46807866, 214117714021024})
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|
||||||
|
func solveQuadratic(a, b, c float64) (float64, float64, error) {
|
||||||
|
// Calculate the discriminant
|
||||||
|
discriminant := b*b - 4*a*c
|
||||||
|
|
||||||
|
// Check if the discriminant is non-negative
|
||||||
|
if discriminant < 0 {
|
||||||
|
return 0, 0, fmt.Errorf("no real roots, discriminant is negative")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the roots
|
||||||
|
root1 := (-b + math.Sqrt(discriminant)) / (2 * a)
|
||||||
|
root2 := (-b - math.Sqrt(discriminant)) / (2 * a)
|
||||||
|
|
||||||
|
if isWholeNumber(root1) { root1 = root1 - 1.0 }
|
||||||
|
if isWholeNumber(root2) { root2 = root2 + 1.0 }
|
||||||
|
return math.Floor(root1), math.Ceil(root2), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isWholeNumber(x float64) bool {
|
||||||
|
rounded := math.Round(x)
|
||||||
|
return x == rounded
|
||||||
|
}
|
||||||
17
2023/go/day06/day06_test.go
Normal file
17
2023/go/day06/day06_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package day06
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPart1(t *testing.T) {
|
||||||
|
r := Part1("")
|
||||||
|
require.Equal(t, 512295, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPart2(t *testing.T) {
|
||||||
|
r := Part2("")
|
||||||
|
require.Equal(t, 36530883, r)
|
||||||
|
}
|
||||||
2
2023/go/day06/input.txt
Normal file
2
2023/go/day06/input.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Time: 46 80 78 66
|
||||||
|
Distance: 214 1177 1402 1024
|
||||||
Reference in New Issue
Block a user