chore: add day 14
This commit is contained in:
parent
77a54c5563
commit
a0d852416f
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
aoc "go.sour.is/advent-of-code-2023"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
// var log = aoc.Log
|
||||
|
@ -21,75 +22,57 @@ type result struct {
|
|||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
var maps []Map
|
||||
var maplist []Map
|
||||
var m Map
|
||||
|
||||
for scan.Scan() {
|
||||
text := scan.Text()
|
||||
if len(text) == 0 {
|
||||
maps = append(maps, m)
|
||||
maplist = append(maplist, m)
|
||||
m = Map{}
|
||||
}
|
||||
|
||||
m = append(m, []rune(text))
|
||||
}
|
||||
maps = append(maps, m)
|
||||
maplist = append(maplist, m)
|
||||
|
||||
score1 := 0
|
||||
for _, m := range maps {
|
||||
for _, m := range maplist {
|
||||
m = aoc.Transpose(reverse(m))
|
||||
m.Sort()
|
||||
score1 += m.Score()
|
||||
}
|
||||
|
||||
score2 := 0
|
||||
// type record [5]int
|
||||
// var last, current record
|
||||
// memo := make(map[record]int)
|
||||
type record [5]int
|
||||
var current record
|
||||
memo := make(map[record]int)
|
||||
|
||||
// for _, m := range maps {
|
||||
// fmt.Println(m)
|
||||
for _, m := range maplist {
|
||||
// fmt.Println(m)
|
||||
|
||||
// m = aoc.Transpose(reverse(m))
|
||||
m = aoc.Transpose(reverse(m))
|
||||
|
||||
// for i := 0; i < 1_000_000_000; i++ {
|
||||
// m.Sort()
|
||||
// current[0] = m.Score()
|
||||
for i := 0; i < 1_000_000_000; i++ {
|
||||
m, current = cycle(m)
|
||||
|
||||
// m = aoc.Transpose(reverse(m))
|
||||
// m.Sort()
|
||||
// current[1] = m.Score()
|
||||
v, ok := memo[current]
|
||||
if ok && v > 1 {
|
||||
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))
|
||||
// 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()
|
||||
|
||||
// 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()
|
||||
// }
|
||||
// fmt.Println(i, counts)
|
||||
i = 1_000_000_000 - (1_000_000_000-counts[0]-counts[1])%counts[2]
|
||||
clear(memo)
|
||||
}
|
||||
memo[current] += 1
|
||||
// fmt.Println(i, current, v)
|
||||
}
|
||||
score2 += m.Score()
|
||||
}
|
||||
|
||||
return &result{valuePT1: score1, valuePT2: score2}, nil
|
||||
}
|
||||
|
@ -149,3 +132,27 @@ func reverse(m Map) Map {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -63,20 +63,13 @@ type lens struct {
|
|||
label string
|
||||
value int
|
||||
}
|
||||
type box []lens
|
||||
type boxes [256]box
|
||||
|
||||
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 (l lens) String() string {
|
||||
return fmt.Sprintf("[%s %d]", l.label, l.value)
|
||||
}
|
||||
|
||||
type box []lens
|
||||
|
||||
func (lis box) String() string {
|
||||
var buf strings.Builder
|
||||
if len(lis) > 0 {
|
||||
|
@ -88,21 +81,34 @@ func (lis box) String() string {
|
|||
return buf.String()
|
||||
}
|
||||
|
||||
func (l lens) String() string {
|
||||
return fmt.Sprintf("[%s %d]", l.label, l.value)
|
||||
type boxes [256]box
|
||||
|
||||
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 {
|
||||
if a, _, ok := strings.Cut(op, "-"); ok {
|
||||
i := hash(a)
|
||||
|
||||
pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a })
|
||||
|
||||
if pos >= 0 {
|
||||
lis[i] = append(lis[i][:pos], lis[i][pos+1:]...)
|
||||
}
|
||||
} else if a, b, ok := strings.Cut(op, "="); ok {
|
||||
i := hash(a)
|
||||
pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a })
|
||||
v := aoc.Atoi(b)
|
||||
|
||||
pos := slices.IndexFunc(lis[i], func(l lens) bool { return l.label == a })
|
||||
|
||||
if pos == -1 {
|
||||
lis[i] = append(lis[i], lens{a, v})
|
||||
} else {
|
||||
|
@ -113,11 +119,18 @@ func (lis boxes) Op(op string) boxes {
|
|||
return lis
|
||||
}
|
||||
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 s := range lis[b] {
|
||||
sum += (b+1) * (s+1) * lis[b][s].value
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
}
|
||||
|
|
5
go.mod
5
go.mod
|
@ -4,4 +4,7 @@ go 1.21.4
|
|||
|
||||
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
2
go.sum
|
@ -1,2 +1,4 @@
|
|||
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
|
||||
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=
|
||||
|
|
Loading…
Reference in New Issue
Block a user