d14
This commit is contained in:
parent
b79f1a5d06
commit
6a046fdb1c
6 changed files with 136 additions and 2 deletions
53
d14/d14_test.go
Normal file
53
d14/d14_test.go
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package d14
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestP1(t *testing.T) {
|
||||||
|
result := P1(`NNCB
|
||||||
|
|
||||||
|
CH -> B
|
||||||
|
HH -> N
|
||||||
|
CB -> H
|
||||||
|
NH -> C
|
||||||
|
HB -> C
|
||||||
|
HC -> B
|
||||||
|
HN -> C
|
||||||
|
NN -> C
|
||||||
|
BH -> H
|
||||||
|
NC -> B
|
||||||
|
NB -> B
|
||||||
|
BN -> B
|
||||||
|
BB -> N
|
||||||
|
BC -> B
|
||||||
|
CC -> N
|
||||||
|
CN -> C`)
|
||||||
|
|
||||||
|
assert.EqualValues(t, 1588, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestP2(t *testing.T) {
|
||||||
|
result := P2(`NNCB
|
||||||
|
|
||||||
|
CH -> B
|
||||||
|
HH -> N
|
||||||
|
CB -> H
|
||||||
|
NH -> C
|
||||||
|
HB -> C
|
||||||
|
HC -> B
|
||||||
|
HN -> C
|
||||||
|
NN -> C
|
||||||
|
BH -> H
|
||||||
|
NC -> B
|
||||||
|
NB -> B
|
||||||
|
BN -> B
|
||||||
|
BB -> N
|
||||||
|
BC -> B
|
||||||
|
CC -> N
|
||||||
|
CN -> C`)
|
||||||
|
|
||||||
|
assert.EqualValues(t, 2188189693529, result)
|
||||||
|
}
|
68
d14/p1.go
Normal file
68
d14/p1.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
package d14
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func reduce[S any, A any](slice []S, f func(elm S, acc A) A, acc A) A {
|
||||||
|
for _, elm := range slice {
|
||||||
|
acc = f(elm, acc)
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}
|
||||||
|
|
||||||
|
func makePairs(template string) map[string]int {
|
||||||
|
pairs := make(map[string]int)
|
||||||
|
|
||||||
|
for i := 0; i < len(template)-1; i++ {
|
||||||
|
pairs[string(template[i])+string(template[i+1])] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return pairs
|
||||||
|
}
|
||||||
|
|
||||||
|
func templateAndInstructions(in string) (string, map[string]string) {
|
||||||
|
parts := strings.Split(strings.TrimSpace(in), "\n\n")
|
||||||
|
instructions := make(map[string]string)
|
||||||
|
for _, pair := range strings.Split(parts[1], "\n") {
|
||||||
|
mapping := strings.Split(pair, " -> ")
|
||||||
|
instructions[mapping[0]] = mapping[1]
|
||||||
|
}
|
||||||
|
return parts[0], instructions
|
||||||
|
}
|
||||||
|
|
||||||
|
func count(template string, instructions map[string]string, runs int) map[string]int {
|
||||||
|
init := func(char string, acc map[string]int) map[string]int { acc[char]++; return acc }
|
||||||
|
counts := reduce(strings.Split(template, ""), init, make(map[string]int))
|
||||||
|
pairs := makePairs(template)
|
||||||
|
for i := 0; i < runs; i++ {
|
||||||
|
for pair, count := range maps.Clone(pairs) {
|
||||||
|
left := pair[:1]
|
||||||
|
right := pair[1:]
|
||||||
|
middle := instructions[pair]
|
||||||
|
pairs[pair] -= count
|
||||||
|
pairs[left+middle] += count
|
||||||
|
pairs[middle+right] += count
|
||||||
|
counts[middle] += count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return counts
|
||||||
|
}
|
||||||
|
|
||||||
|
func countPolymer(in string, runs int) int {
|
||||||
|
template, instructions := templateAndInstructions(in)
|
||||||
|
counts := maps.Values(count(template, instructions, runs))
|
||||||
|
slices.Sort(counts)
|
||||||
|
max := counts[len(counts)-1]
|
||||||
|
min := counts[0]
|
||||||
|
|
||||||
|
return max - min
|
||||||
|
}
|
||||||
|
|
||||||
|
func P1(in string) int {
|
||||||
|
return countPolymer(in, 10)
|
||||||
|
}
|
5
d14/p2.go
Normal file
5
d14/p2.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package d14
|
||||||
|
|
||||||
|
func P2(in string) int {
|
||||||
|
return countPolymer(in, 40)
|
||||||
|
}
|
6
go.mod
6
go.mod
|
@ -1,10 +1,12 @@
|
||||||
module github.com/fotonmoton/aoc2021
|
module github.com/fotonmoton/aoc2021
|
||||||
|
|
||||||
go 1.17
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/stretchr/testify v1.7.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/stretchr/testify v1.7.0 // indirect
|
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||||
)
|
)
|
||||||
|
|
3
go.sum
3
go.sum
|
@ -5,6 +5,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4=
|
||||||
|
golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
3
main.go
3
main.go
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/fotonmoton/aoc2021/d11"
|
"github.com/fotonmoton/aoc2021/d11"
|
||||||
"github.com/fotonmoton/aoc2021/d12"
|
"github.com/fotonmoton/aoc2021/d12"
|
||||||
"github.com/fotonmoton/aoc2021/d13"
|
"github.com/fotonmoton/aoc2021/d13"
|
||||||
|
"github.com/fotonmoton/aoc2021/d14"
|
||||||
"github.com/fotonmoton/aoc2021/d2"
|
"github.com/fotonmoton/aoc2021/d2"
|
||||||
"github.com/fotonmoton/aoc2021/d3"
|
"github.com/fotonmoton/aoc2021/d3"
|
||||||
"github.com/fotonmoton/aoc2021/d4"
|
"github.com/fotonmoton/aoc2021/d4"
|
||||||
|
@ -58,4 +59,6 @@ func main() {
|
||||||
fmt.Printf("day 12 part 2: %v\n", d12.P2(client.Day(12)))
|
fmt.Printf("day 12 part 2: %v\n", d12.P2(client.Day(12)))
|
||||||
fmt.Printf("day 13: %v\n", d13.P1(client.Day(13)))
|
fmt.Printf("day 13: %v\n", d13.P1(client.Day(13)))
|
||||||
fmt.Printf("day 13 part 2: %v\n", d13.P2(client.Day(13)))
|
fmt.Printf("day 13 part 2: %v\n", d13.P2(client.Day(13)))
|
||||||
|
fmt.Printf("day 14: %v\n", d14.P1(client.Day(14)))
|
||||||
|
fmt.Printf("day 14 part 2: %v\n", d14.P2(client.Day(14)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue