Last active
December 22, 2020 07:59
-
-
Save felix-larsen/6274310ca441cf1eaba30e3685f058c5 to your computer and use it in GitHub Desktop.
22th December solution - Advent of Code 2020 - swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while player1Cards.count != 0 && player2Cards.count != 0 { | |
let card1 = player1Cards.removeFirst() | |
let card2 = player2Cards.removeFirst() | |
if card1 > card2 { | |
player1Cards.append(contentsOf: [card1, card2]) | |
} else { | |
player2Cards.append(contentsOf: [card2, card1]) | |
} | |
} | |
var winningCards = [Int]() | |
if player1Cards.count != 0 { | |
winningCards = player1Cards | |
} else { | |
winningCards = player2Cards | |
} | |
let score = (winningCards.reversed() as [Int]).enumerated().reduce(0) { (score, element) -> Int in | |
score + (element.offset + 1) * element.element | |
} | |
print(score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var (playerlayer1Winner, winningCards) = playGameWith(player1Cards: player1Cards, player2Cards: player2Cards) | |
let score = (winningCards.reversed() as [Int]).enumerated().reduce(0) { (score, element) -> Int in | |
score + (element.offset + 1) * element.element | |
} | |
print(winningCards) | |
print(score) | |
func playRoundWith( player1Cards: [Int], player2Cards: [Int], previousRounds: [([Int], [Int])]) -> (Bool, Bool) { | |
if previousRounds.contains(where: { (pair) -> Bool in | |
pair.0 == player1Cards && pair.1 == player2Cards | |
}) { | |
return (true, true) | |
} | |
let card1 = player1Cards.first! | |
let card2 = player2Cards.first! | |
let deckCount1 = player1Cards.count - 1 | |
let deckCount2 = player2Cards.count - 1 | |
if deckCount1 >= card1 && deckCount2 >= card2 { | |
let (isPlayer1Winner, _) = playGameWith(player1Cards: player1Cards.dropFirst().dropLast(deckCount1 - card1), player2Cards: player2Cards.dropFirst().dropLast(deckCount2 - card2)) | |
return (isPlayer1Winner, false) | |
} else if card1 > card2 { | |
return (true, false) | |
} else { | |
return (false, false) | |
} | |
} | |
func playGameWith( player1Cards: [Int], player2Cards: [Int]) -> (Bool, [Int]) { | |
var perviousRounds = [([Int], [Int])]() | |
var newPlayer1Cards = player1Cards | |
var newPlayer2Cards = player2Cards | |
while newPlayer1Cards.count != 0 && newPlayer2Cards.count != 0 { | |
let (player1WonRound, player1WonGame) = playRoundWith(player1Cards: newPlayer1Cards, player2Cards: newPlayer2Cards, previousRounds: perviousRounds) | |
if player1WonGame { | |
return (true, newPlayer1Cards) | |
} | |
perviousRounds.append((newPlayer1Cards, newPlayer2Cards)) | |
let card1 = newPlayer1Cards.removeFirst() | |
let card2 = newPlayer2Cards.removeFirst() | |
if player1WonRound { | |
newPlayer1Cards.append(contentsOf: [card1, card2]) | |
} else { | |
newPlayer2Cards.append(contentsOf: [card2, card1]) | |
} | |
} | |
var winningCards = [Int]() | |
if newPlayer1Cards.count != 0 { | |
winningCards = newPlayer1Cards | |
} else { | |
winningCards = newPlayer2Cards | |
} | |
return (newPlayer1Cards.count != 0, winningCards) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment