Compare commits
No commits in common. "b1e4c4d634caef02fd84efdbfcf55d8433a138de" and "0d652660f1ba2c6d3fd593774e8a9f0c19a4d16c" have entirely different histories.
b1e4c4d634
...
0d652660f1
|
@ -4,7 +4,6 @@ import (
|
|||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
|
@ -63,7 +62,7 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||
func(i int, v int, counts [3]int) [3]int {
|
||||
counts[v]++
|
||||
return counts
|
||||
}, [3]int{}, slices.Values(maps.Values(memo)))
|
||||
}, [3]int{}, maps.Values(memo)...)
|
||||
|
||||
// fmt.Println(i, counts)
|
||||
i = 1_000_000_000 - (1_000_000_000-counts[0]-counts[1])%counts[2]
|
||||
|
|
|
@ -32,14 +32,14 @@ func run(scan *bufio.Scanner) (*result, error) {
|
|||
r.valuePT1 = aoc.Reduce(func(i int, t string, sum int) int {
|
||||
sum += hash(t)
|
||||
return sum
|
||||
}, 0, slices.Values(ops))
|
||||
}, 0, ops...)
|
||||
}
|
||||
|
||||
var boxen boxes
|
||||
|
||||
boxen = aoc.Reduce(func(i int, op string, b boxes) boxes {
|
||||
return b.Op(op)
|
||||
}, boxen, slices.Values(ops))
|
||||
}, boxen, ops...)
|
||||
|
||||
r.valuePT2 = boxen.Sum()
|
||||
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
File diff suppressed because it is too large
Load Diff
|
@ -4,9 +4,6 @@ import (
|
|||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"iter"
|
||||
"slices"
|
||||
"sort"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
)
|
||||
|
@ -23,64 +20,11 @@ type result struct {
|
|||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
var (
|
||||
left []int
|
||||
right []int
|
||||
)
|
||||
|
||||
for scan.Scan() {
|
||||
txt := scan.Text()
|
||||
_ = scan.Text()
|
||||
|
||||
var l, r int
|
||||
_, err := fmt.Sscanf(txt, "%d %d", &l, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
left = append(left, l)
|
||||
right = append(right, r)
|
||||
return &result{}, nil
|
||||
}
|
||||
|
||||
sort.Ints(left)
|
||||
sort.Ints(right)
|
||||
|
||||
result := &result{}
|
||||
|
||||
result.valuePT1 = aoc.Reduce(func(i int, z pair[int, int], sum int) int {
|
||||
return sum + aoc.ABS(z.L-z.R)
|
||||
}, 0, zip(slices.Values(left), slices.Values(right)))
|
||||
|
||||
rmap := aoc.Reduce(func(i int, z int, m map[int]int) map[int]int {
|
||||
m[z]++
|
||||
return m
|
||||
}, make(map[int]int), slices.Values(right))
|
||||
|
||||
for _, v := range left {
|
||||
if r, ok := rmap[v]; ok {
|
||||
result.valuePT2 += v*r
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type pair[L, R any] struct {
|
||||
L L
|
||||
R R
|
||||
}
|
||||
|
||||
func zip[L, R any](l iter.Seq[L], r iter.Seq[R]) iter.Seq[pair[L,R]] {
|
||||
return func(yield func(pair[L, R]) bool) {
|
||||
pullR, stop := iter.Pull(r)
|
||||
defer stop()
|
||||
|
||||
for l := range l {
|
||||
r, _ := pullR()
|
||||
|
||||
if !yield(pair[L, R]{L: l, R: r}) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ func TestExample(t *testing.T) {
|
|||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 11)
|
||||
is.Equal(result.valuePT2, 31)
|
||||
is.Equal(result.valuePT1, 0)
|
||||
is.Equal(result.valuePT2, 0)
|
||||
}
|
||||
|
||||
func TestSolution(t *testing.T) {
|
||||
|
@ -36,6 +36,6 @@ func TestSolution(t *testing.T) {
|
|||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 2756096)
|
||||
is.Equal(result.valuePT2, 23117829)
|
||||
is.Equal(result.valuePT1, 0)
|
||||
is.Equal(result.valuePT2, 0)
|
||||
}
|
||||
|
|
16
itertools.go
16
itertools.go
|
@ -1,7 +1,6 @@
|
|||
package aoc
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
@ -18,8 +17,8 @@ func Repeat[T any](s T, i int) []T {
|
|||
return lis
|
||||
}
|
||||
|
||||
func Reduce[T, U any](fn func(int, T, U) U, u U, list iter.Seq[T]) U {
|
||||
for i, t := range Enumerate(list) {
|
||||
func Reduce[T, U any](fn func(int, T, U) U, u U, list ...T) U {
|
||||
for i, t := range list {
|
||||
u = fn(i, t, u)
|
||||
}
|
||||
return u
|
||||
|
@ -55,14 +54,3 @@ func Pairwise[T any](arr []T) [][2]T {
|
|||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
func Enumerate[T any](arr iter.Seq[T]) iter.Seq2[int, T] {
|
||||
return func(yield func(int, T) bool) {
|
||||
i := 0
|
||||
for v := range arr {
|
||||
if !yield(i, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user