Skip to content

Commit

Permalink
Implementation for 22a
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 30, 2019
1 parent 91bede8 commit 2c7c9a0
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 0 deletions.
98 changes: 98 additions & 0 deletions challenge/day22/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package day22

import (
"fmt"
"strconv"
"strings"

"github.com/nlowe/aoc2019/challenge"
"github.com/spf13/cobra"
)

var A = &cobra.Command{
Use: "22a",
Short: "Day 22, Problem A",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Answer: %d\n", a(challenge.FromFile()))
},
}

const (
instrDealNewStack = "deal into new stack"
instrCut = "cut "
instrDealWithIncrement = "deal with increment "
)

var deckSize = 10007

func a(challenge *challenge.Input) int {
deck := fancyShuffle(challenge)

for i, v := range deck {
if v == 2019 {
return i
}
}

panic("no solution")
}

func fancyShuffle(challenge *challenge.Input) []int {
deck := make([]int, deckSize)
for i := range deck {
deck[i] = i
}

for instruction := range challenge.Lines() {
if instruction == instrDealNewStack {
deck = dealIntoNewStack(deck)
} else if strings.HasPrefix(instruction, instrCut) {
n, err := strconv.Atoi(strings.TrimPrefix(instruction, instrCut))
if err != nil {
panic(err)
}

deck = cut(deck, n)
} else if strings.HasPrefix(instruction, instrDealWithIncrement) {
increment, err := strconv.Atoi(strings.TrimPrefix(instruction, instrDealWithIncrement))
if err != nil {
panic(err)
}

deck = dealWithIncrement(deck, increment)
}
}

return deck
}

func dealIntoNewStack(deck []int) []int {
newStack := make([]int, len(deck))
for i, v := range deck {
newStack[len(newStack)-i-1] = v
}

return newStack
}

func cut(deck []int, n int) []int {
newStack := make([]int, len(deck))
for i := range deck {
if n > 0 {
newStack[i] = deck[(n+i)%len(deck)]
} else {
newStack[i] = deck[(len(deck)+i+n)%len(deck)]
}
}

return newStack
}

func dealWithIncrement(deck []int, increment int) []int {
newStack := make([]int, len(deck))
for i := range deck {
newStack[(i*increment)%len(deck)] = deck[i]
}

return newStack
}
44 changes: 44 additions & 0 deletions challenge/day22/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package day22

import (
"testing"

"github.com/nlowe/aoc2019/challenge"
"github.com/stretchr/testify/require"
)

func TestFancyShuffle(t *testing.T) {
deckSize = 10
for _, tt := range []struct {
instructions string
expected []int
}{
{instructions: "deal into new stack", expected: []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
{instructions: "cut 3", expected: []int{3, 4, 5, 6, 7, 8, 9, 0, 1, 2}},
{instructions: "cut -4", expected: []int{6, 7, 8, 9, 0, 1, 2, 3, 4, 5}},
{instructions: "deal with increment 3", expected: []int{0, 7, 4, 1, 8, 5, 2, 9, 6, 3}},
{instructions: `deal with increment 7
deal into new stack
deal into new stack`, expected: []int{0, 3, 6, 9, 2, 5, 8, 1, 4, 7}},
{instructions: `cut 6
deal with increment 7
deal into new stack`, expected: []int{3, 0, 7, 4, 1, 8, 5, 2, 9, 6}},
{instructions: `deal with increment 7
deal with increment 9
cut -2`, expected: []int{6, 3, 0, 7, 4, 1, 8, 5, 2, 9}},
{instructions: `deal into new stack
cut -2
deal with increment 7
cut 8
cut -4
deal with increment 7
cut 3
deal with increment 9
deal with increment 3
cut -1`, expected: []int{9, 2, 5, 8, 1, 4, 7, 0, 3, 6}},
} {
t.Run(tt.instructions, func(t *testing.T) {
require.Equal(t, tt.expected, fancyShuffle(challenge.FromLiteral(tt.instructions)))
})
}
}
100 changes: 100 additions & 0 deletions challenge/day22/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
deal with increment 16
cut -7810
deal with increment 70
cut 8978
deal into new stack
deal with increment 14
cut 9822
deal with increment 31
cut -3630
deal with increment 37
cut -929
deal with increment 74
cut -9268
deal with increment 47
cut -7540
deal with increment 13
cut -5066
deal with increment 73
cut 1605
deal into new stack
cut 1615
deal with increment 72
cut 1025
deal with increment 28
cut 6427
deal with increment 10
deal into new stack
cut -8336
deal with increment 33
cut -9834
deal with increment 64
deal into new stack
deal with increment 42
cut 7013
deal into new stack
deal with increment 55
deal into new stack
cut 8349
deal into new stack
deal with increment 41
cut -9073
deal with increment 11
deal into new stack
deal with increment 46
cut 613
deal with increment 66
cut 4794
deal with increment 3
cut -6200
deal with increment 52
deal into new stack
cut 1328
deal with increment 29
cut -1670
deal into new stack
cut -706
deal with increment 66
cut -2827
deal with increment 6
cut 8493
deal with increment 10
deal into new stack
deal with increment 75
cut 3163
deal with increment 14
cut 4848
deal with increment 66
deal into new stack
deal with increment 52
deal into new stack
deal with increment 71
deal into new stack
deal with increment 50
cut -9466
deal with increment 46
cut -9621
deal into new stack
deal with increment 14
deal into new stack
cut 7236
deal with increment 71
cut -9836
deal with increment 16
deal into new stack
cut -1519
deal with increment 53
cut -5190
deal with increment 68
cut 4313
deal into new stack
deal with increment 39
deal into new stack
deal with increment 5
cut 3476
deal with increment 61
cut -2533
deal with increment 10
cut 4921
deal with increment 67
cut 3563
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/nlowe/aoc2019/challenge/day2"
"github.com/nlowe/aoc2019/challenge/day20"
"github.com/nlowe/aoc2019/challenge/day21"
"github.com/nlowe/aoc2019/challenge/day22"
"github.com/nlowe/aoc2019/challenge/day3"
"github.com/nlowe/aoc2019/challenge/day4"
"github.com/nlowe/aoc2019/challenge/day5"
Expand Down Expand Up @@ -83,6 +84,7 @@ func init() {
day19.A, day19.B,
day20.A, day20.B,
day21.A, day21.B,
day22.A,
)

flags := rootCmd.PersistentFlags()
Expand Down

0 comments on commit 2c7c9a0

Please sign in to comment.