Trying to finish up 2019
This commit is contained in:
100
2019/day22/input
Normal file
100
2019/day22/input
Normal file
@@ -0,0 +1,100 @@
|
||||
deal into new stack
|
||||
deal with increment 32
|
||||
cut 5214
|
||||
deal with increment 50
|
||||
cut -7078
|
||||
deal with increment 3
|
||||
cut 5720
|
||||
deal with increment 18
|
||||
cut -6750
|
||||
deal with increment 74
|
||||
cut -6007
|
||||
deal with increment 16
|
||||
cut -3885
|
||||
deal with increment 40
|
||||
deal into new stack
|
||||
cut -2142
|
||||
deal with increment 25
|
||||
deal into new stack
|
||||
cut -1348
|
||||
deal with increment 40
|
||||
cut 3943
|
||||
deal with increment 14
|
||||
cut 7093
|
||||
deal with increment 67
|
||||
cut 1217
|
||||
deal with increment 75
|
||||
cut 597
|
||||
deal with increment 60
|
||||
cut -1078
|
||||
deal with increment 68
|
||||
cut -8345
|
||||
deal with increment 25
|
||||
cut 6856
|
||||
deal into new stack
|
||||
cut -4152
|
||||
deal with increment 59
|
||||
deal into new stack
|
||||
cut -80
|
||||
deal with increment 3
|
||||
deal into new stack
|
||||
deal with increment 44
|
||||
cut 1498
|
||||
deal with increment 18
|
||||
cut -7149
|
||||
deal with increment 58
|
||||
deal into new stack
|
||||
deal with increment 71
|
||||
cut -323
|
||||
deal into new stack
|
||||
deal with increment 58
|
||||
cut 1793
|
||||
deal with increment 45
|
||||
deal into new stack
|
||||
cut 7187
|
||||
deal with increment 48
|
||||
cut 2664
|
||||
deal into new stack
|
||||
cut 8943
|
||||
deal with increment 32
|
||||
deal into new stack
|
||||
deal with increment 62
|
||||
cut -9436
|
||||
deal with increment 67
|
||||
deal into new stack
|
||||
cut -1898
|
||||
deal with increment 61
|
||||
deal into new stack
|
||||
deal with increment 14
|
||||
cut 1287
|
||||
deal with increment 8
|
||||
cut 560
|
||||
deal with increment 6
|
||||
cut -2110
|
||||
deal with increment 8
|
||||
cut 9501
|
||||
deal with increment 25
|
||||
cut 4791
|
||||
deal with increment 70
|
||||
deal into new stack
|
||||
deal with increment 5
|
||||
cut 2320
|
||||
deal with increment 47
|
||||
cut -467
|
||||
deal into new stack
|
||||
deal with increment 19
|
||||
cut -1920
|
||||
deal with increment 16
|
||||
cut -8920
|
||||
deal with increment 65
|
||||
cut -3986
|
||||
deal with increment 3
|
||||
cut -2690
|
||||
deal with increment 35
|
||||
cut -757
|
||||
deal with increment 37
|
||||
cut -1280
|
||||
deal with increment 71
|
||||
cut 3765
|
||||
deal with increment 26
|
||||
deal into new stack
|
101
2019/day22/main.go
Normal file
101
2019/day22/main.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pt := helpers.GetArgNumber(1)
|
||||
inp := helpers.StdinToStringSlice()
|
||||
if pt == "1" {
|
||||
part1(inp)
|
||||
} else {
|
||||
part2(inp)
|
||||
}
|
||||
}
|
||||
|
||||
func NewDeck(n int) []int {
|
||||
s := make([]int, n)
|
||||
for i := 0; i < n; i++ {
|
||||
s[i] = i
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func deal(s []int) []int {
|
||||
s2 := make([]int, len(s))
|
||||
for i := range s {
|
||||
s2[len(s)-1-i] = s[i]
|
||||
}
|
||||
return s2
|
||||
}
|
||||
|
||||
func cut(s []int, n int) []int {
|
||||
if n >= 0 {
|
||||
return append(s[n:], s[:n]...)
|
||||
}
|
||||
n = -n
|
||||
return append(s[len(s)-n:], s[:len(s)-n]...)
|
||||
}
|
||||
|
||||
func dealn(s []int, n int) []int {
|
||||
s2 := make([]int, len(s))
|
||||
for i := range s {
|
||||
s2[(i*n)%len(s)] = s[i]
|
||||
}
|
||||
return s2
|
||||
}
|
||||
|
||||
func part1(inp []string) {
|
||||
s := NewDeck(10007)
|
||||
for _, line := range inp {
|
||||
if line == "deal into new stack" {
|
||||
s = deal(s)
|
||||
} else if line[:3] == "cut" {
|
||||
i, _ := strconv.Atoi(line[4:])
|
||||
s = cut(s, i)
|
||||
} else if line[:19] == "deal with increment" {
|
||||
i, _ := strconv.Atoi(line[20:])
|
||||
s = dealn(s, i)
|
||||
}
|
||||
}
|
||||
for i, v := range s {
|
||||
if v == 2019 {
|
||||
fmt.Println(i)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func part2(inp []string) {
|
||||
deckSize := big.NewInt(119315717514047)
|
||||
shuffles := big.NewInt(101741582076661)
|
||||
off, inc := big.NewInt(0), big.NewInt(1)
|
||||
for _, line := range inp {
|
||||
if line == "deal into new stack" {
|
||||
inc.Mul(inc, big.NewInt(-1))
|
||||
off.Add(off, inc)
|
||||
} else if line[:3] == "cut" {
|
||||
i, _ := strconv.Atoi(line[4:])
|
||||
off.Add(off, big.NewInt(0).Mul(big.NewInt(int64(i)), inc))
|
||||
} else if line[:19] == "deal with increment" {
|
||||
i, _ := strconv.Atoi(line[20:])
|
||||
inc.Mul(inc, big.NewInt(0).Exp(big.NewInt(int64(i)), big.NewInt(0).Sub(deckSize, big.NewInt(2)), deckSize))
|
||||
}
|
||||
}
|
||||
lastInc := big.NewInt(0).Exp(inc, shuffles, deckSize)
|
||||
lastOff := big.NewInt(0).Exp(inc, shuffles, deckSize)
|
||||
lastOff.Sub(big.NewInt(1), lastOff)
|
||||
mod := big.NewInt(0).Exp(big.NewInt(0).Sub(big.NewInt(1), inc), big.NewInt(0).Sub(deckSize, big.NewInt(2)), deckSize)
|
||||
lastOff.Mul(lastOff, mod)
|
||||
lastOff.Mul(lastOff, off)
|
||||
|
||||
res := big.NewInt(0).Mul(big.NewInt(2020), lastInc)
|
||||
res.Add(res, lastOff)
|
||||
res.Mod(res, deckSize)
|
||||
fmt.Println("Result:", res)
|
||||
}
|
Reference in New Issue
Block a user