mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2020-day22: 537/997 recursive "war" card game. hard instructions to get right on the first go
This commit is contained in:
+84
-16
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/mathutil"
|
||||
"github.com/alexchao26/advent-of-code-go/cast"
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
@@ -15,32 +15,100 @@ func main() {
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
var ans int
|
||||
if part == 1 {
|
||||
ans := part1(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
fmt.Println("Output:", ans)
|
||||
ans = part1(util.ReadFile("./input.txt"))
|
||||
} else {
|
||||
ans := part2(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
fmt.Println("Output:", ans)
|
||||
ans = part2(util.ReadFile("./input.txt"))
|
||||
}
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
|
||||
// leaderboard: 537
|
||||
func part1(input string) int {
|
||||
parsed := parseInput(input)
|
||||
_ = parsed
|
||||
deck1, deck2 := parseInput(input)
|
||||
|
||||
return 0
|
||||
for !(len(deck1) == 0 || len(deck2) == 0) {
|
||||
top1, top2 := deck1[0], deck2[0]
|
||||
if top1 > top2 {
|
||||
deck1 = append(deck1, top1, top2)
|
||||
} else {
|
||||
deck2 = append(deck2, top2, top1)
|
||||
}
|
||||
deck1 = deck1[1:]
|
||||
deck2 = deck2[1:]
|
||||
}
|
||||
|
||||
winningDeck := append(deck1, deck2...)
|
||||
var sumOfProducts int
|
||||
multiplier := 1
|
||||
for i := len(winningDeck) - 1; i >= 0; i-- {
|
||||
sumOfProducts += multiplier * winningDeck[i]
|
||||
multiplier++
|
||||
}
|
||||
|
||||
return sumOfProducts
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
return 0
|
||||
deck1, deck2 := parseInput(input)
|
||||
winningScore, _ := recursiveGame(deck1, deck2)
|
||||
return winningScore
|
||||
}
|
||||
|
||||
func parseInput(input string) (ans []int) {
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, l := range lines {
|
||||
ans = append(ans, mathutil.StrToInt(l))
|
||||
// leaderboard: 997
|
||||
func recursiveGame(deck1, deck2 []int) (finalScore int, player1Wins bool) {
|
||||
previousHands1 := map[string]bool{}
|
||||
previousHands2 := map[string]bool{}
|
||||
|
||||
for !(len(deck1) == 0 || len(deck2) == 0) {
|
||||
top1, top2 := deck1[0], deck2[0]
|
||||
|
||||
if previousHands1[fmt.Sprintf("%v", deck1)] || previousHands2[fmt.Sprintf("%v", deck2)] {
|
||||
player1Wins = true
|
||||
} else {
|
||||
previousHands1[fmt.Sprintf("%v", deck1)] = true
|
||||
previousHands2[fmt.Sprintf("%v", deck2)] = true
|
||||
|
||||
// if not enough cards in either deck, just compare cards
|
||||
if top1 > len(deck1)-1 || top2 > len(deck2)-1 {
|
||||
player1Wins = top1 > top2
|
||||
} else {
|
||||
// otherwise recurse
|
||||
_, player1Wins = recursiveGame(append([]int{}, deck1[1:top1+1]...), append([]int{}, deck2[1:top2+1]...))
|
||||
}
|
||||
}
|
||||
|
||||
if player1Wins {
|
||||
deck1 = append(deck1, top1, top2)
|
||||
} else {
|
||||
deck2 = append(deck2, top2, top1)
|
||||
}
|
||||
|
||||
deck1 = deck1[1:]
|
||||
deck2 = deck2[1:]
|
||||
}
|
||||
return ans
|
||||
|
||||
winningDeck := append(deck1, deck2...)
|
||||
var sumOfProducts int
|
||||
multiplier := 1
|
||||
for i := len(winningDeck) - 1; i >= 0; i-- {
|
||||
sumOfProducts += multiplier * winningDeck[i]
|
||||
multiplier++
|
||||
}
|
||||
|
||||
// player1Wins boolean is equivalent to if their deck does not have zero cards
|
||||
return sumOfProducts, len(deck1) != 0 // 997
|
||||
}
|
||||
|
||||
func parseInput(input string) ([]int, []int) {
|
||||
players := strings.Split(input, "\n\n")
|
||||
var deck1, deck2 []int
|
||||
for _, l := range strings.Split(players[0], "\n")[1:] {
|
||||
deck1 = append(deck1, cast.ToInt(l))
|
||||
}
|
||||
for _, l := range strings.Split(players[1], "\n")[1:] {
|
||||
deck2 = append(deck2, cast.ToInt(l))
|
||||
}
|
||||
return deck1, deck2
|
||||
}
|
||||
|
||||
+20
-2
@@ -2,15 +2,32 @@ package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
var example = `Player 1:
|
||||
9
|
||||
2
|
||||
6
|
||||
3
|
||||
1
|
||||
|
||||
Player 2:
|
||||
5
|
||||
8
|
||||
4
|
||||
7
|
||||
10`
|
||||
|
||||
func Test_part1(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
|
||||
{"example", example, 306},
|
||||
{"actual", util.ReadFile("input.txt"), 33403},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -27,7 +44,8 @@ func Test_part2(t *testing.T) {
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
|
||||
{"example", example, 291},
|
||||
{"actual", util.ReadFile("input.txt"), 29177},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user