chore: add day 14
All checks were successful
Go Bump / bump (push) Successful in 12s
Go Test / build (push) Successful in 36s

This commit is contained in:
xuu 2023-12-15 15:09:59 -07:00
parent 77a54c5563
commit a0d852416f
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
4 changed files with 88 additions and 63 deletions

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
aoc "go.sour.is/advent-of-code-2023" aoc "go.sour.is/advent-of-code-2023"
"golang.org/x/exp/maps"
) )
// var log = aoc.Log // var log = aoc.Log
@ -21,75 +22,57 @@ type result struct {
func (r result) String() string { return fmt.Sprintf("%#v", r) } func (r result) String() string { return fmt.Sprintf("%#v", r) }
func run(scan *bufio.Scanner) (*result, error) { func run(scan *bufio.Scanner) (*result, error) {
var maps []Map var maplist []Map
var m Map var m Map
for scan.Scan() { for scan.Scan() {
text := scan.Text() text := scan.Text()
if len(text) == 0 { if len(text) == 0 {
maps = append(maps, m) maplist = append(maplist, m)
m = Map{} m = Map{}
} }
m = append(m, []rune(text)) m = append(m, []rune(text))
} }
maps = append(maps, m) maplist = append(maplist, m)
score1 := 0 score1 := 0
for _, m := range maps { for _, m := range maplist {
m = aoc.Transpose(reverse(m)) m = aoc.Transpose(reverse(m))
m.Sort() m.Sort()
score1 += m.Score() score1 += m.Score()
} }
score2 := 0 score2 := 0
// type record [5]int type record [5]int
// var last, current record var current record
// memo := make(map[record]int) memo := make(map[record]int)
// for _, m := range maps { for _, m := range maplist {
// fmt.Println(m) // fmt.Println(m)
// m = aoc.Transpose(reverse(m)) m = aoc.Transpose(reverse(m))
// for i := 0; i < 1_000_000_000; i++ { for i := 0; i < 1_000_000_000; i++ {
// m.Sort() m, current = cycle(m)
// current[0] = m.Score()
// m = aoc.Transpose(reverse(m)) v, ok := memo[current]
// m.Sort() if ok && v > 1 {
// current[1] = m.Score() counts := aoc.Reduce(
func(i int, v int, counts [3]int) [3]int {
counts[v]++
return counts
}, [3]int{}, maps.Values(memo)...)
// m = aoc.Transpose(reverse(m)) // fmt.Println(i, counts)
// m.Sort() i = 1_000_000_000 - (1_000_000_000-counts[0]-counts[1])%counts[2]
// current[2] = m.Score() clear(memo)
}
// m = aoc.Transpose(reverse(m)) memo[current] += 1
// m.Sort() // fmt.Println(i, current, v)
// current[3] = m.Score() }
score2 += m.Score()
// m = aoc.Transpose(reverse(m)) }
// current[4] = m.Score()
// if slices.Equal(last[:], current[:]) {
// break
// }
// v, ok := memo[current]
// if (i - 104) % 13 == 12 {
// fmt.Println(i, current, ok, v)
// }
// // if ok && v > 2 {
// // break
// // }
// memo[current] += 1
// last = current
// }
// score2 += m.Score()
// }
return &result{valuePT1: score1, valuePT2: score2}, nil return &result{valuePT1: score1, valuePT2: score2}, nil
} }
@ -149,3 +132,27 @@ func reverse(m Map) Map {
} }
return m return m
} }
func cycle(m Map) (Map, [5]int) {
var current [5]int
m.Sort()
current[0] = m.Score()
m = aoc.Transpose(reverse(m))
m.Sort()
current[1] = m.Score()
m = aoc.Transpose(reverse(m))
m.Sort()
current[2] = m.Score()
m = aoc.Transpose(reverse(m))
m.Sort()
current[3] = m.Score()
m = aoc.Transpose(reverse(m))
current[4] = m.Score()
return m, current
}

View File

@ -63,20 +63,13 @@ type lens struct {
label string label string
value int value int
} }
type box []lens
type boxes [256]box
func (lis boxes) String() string { func (l lens) String() string {
var buf strings.Builder return fmt.Sprintf("[%s %d]", l.label, l.value)
buf.WriteString("Boxes:\n")
for i, b := range lis {
if len(b) > 0 {
fmt.Fprintf(&buf, "Box %d: %v\n",i, b)
}
}
return buf.String()
} }
type box []lens
func (lis box) String() string { func (lis box) String() string {
var buf strings.Builder var buf strings.Builder
if len(lis) > 0 { if len(lis) > 0 {
@ -88,21 +81,34 @@ func (lis box) String() string {
return buf.String() return buf.String()
} }
func (l lens) String() string { type boxes [256]box
return fmt.Sprintf("[%s %d]", l.label, l.value)
func (lis boxes) String() string {
var buf strings.Builder
buf.WriteString("Boxes:\n")
for i, b := range lis {
if len(b) > 0 {
fmt.Fprintf(&buf, "Box %d: %v\n", i, b)
}
}
return buf.String()
} }
func (lis boxes) Op(op string) boxes { func (lis boxes) Op(op string) boxes {
if a, _, ok := strings.Cut(op, "-"); ok { if a, _, ok := strings.Cut(op, "-"); ok {
i := hash(a) i := hash(a)
pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a }) pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a })
if pos >= 0 { if pos >= 0 {
lis[i] = append(lis[i][:pos], lis[i][pos+1:]...) lis[i] = append(lis[i][:pos], lis[i][pos+1:]...)
} }
} else if a, b, ok := strings.Cut(op, "="); ok { } else if a, b, ok := strings.Cut(op, "="); ok {
i := hash(a) i := hash(a)
pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a })
v := aoc.Atoi(b) v := aoc.Atoi(b)
pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a })
if pos == -1 { if pos == -1 {
lis[i] = append(lis[i], lens{a, v}) lis[i] = append(lis[i], lens{a, v})
} else { } else {
@ -113,11 +119,18 @@ func (lis boxes) Op(op string) boxes {
return lis return lis
} }
func (lis boxes) Sum() int { func (lis boxes) Sum() int {
sum := 0 // return aoc.Reduce(func(b int, box box, sum int) int {
// return aoc.Reduce(
// func(s int, lens lens, sum int) int {
// return sum + (b+1)*(s+1)*lens.value
// }, sum, box...)
// }, 0, lis[:]...)
var sum int
for b := range lis { for b := range lis {
for s := range lis[b] { for s := range lis[b] {
sum += (b+1) * (s+1) * lis[b][s].value sum += (b+1) * (s+1) * lis[b][s].value
} }
} }
return sum return sum
} }

5
go.mod
View File

@ -4,4 +4,7 @@ go 1.21.4
toolchain go1.21.5 toolchain go1.21.5
require github.com/matryer/is v1.4.1 require (
github.com/matryer/is v1.4.1
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611
)

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ= github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4=
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=