-
-
Save jamierocks/503475bcd4dfe82616662a418b9b189b to your computer and use it in GitHub Desktop.
Advent Of Code 2020 Solution for Day 22 in 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
import Foundation | |
class Game { | |
var playerOne: Player | |
var playerTwo: Player | |
init(_ playerOne: Player, _ playerTwo: Player) { | |
self.playerOne = playerOne | |
self.playerTwo = playerTwo | |
} | |
func play(_ recursiveCombat: Bool = false) -> Player { | |
var players: (Player, Player) | |
var player1History: [[Int]] = [] | |
var player2History: [[Int]] = [] | |
repeat { | |
if (recursiveCombat) { | |
if player1History.contains(playerOne.deck) || player2History.contains(playerTwo.deck) { | |
return playerOne | |
} | |
player1History.append(playerOne.deck) | |
player2History.append(playerTwo.deck) | |
} | |
players = playRound(recursiveCombat) | |
} | |
while (players.1.deck.count != 0) | |
return players.0 | |
} | |
func playRound(_ recursiveCombat: Bool) -> (Player, Player) { | |
let cardOne = playerOne.deck.removeFirst() | |
let cardTwo = playerTwo.deck.removeFirst() | |
var winner: Player | |
if recursiveCombat && | |
playerOne.deck.count >= cardOne && | |
playerTwo.deck.count >= cardTwo { | |
let game = Game( | |
Player(playerOne.name, Array<Int>(playerOne.deck[0...(cardOne - 1)])), | |
Player(playerTwo.name, Array<Int>(playerTwo.deck[0...(cardTwo - 1)])) | |
) | |
winner = game.play(true) | |
if winner.name == playerOne.name { | |
winner = playerOne | |
} | |
else { | |
winner = playerTwo | |
} | |
} | |
else if cardOne > cardTwo { | |
winner = playerOne | |
} | |
else { | |
winner = playerTwo | |
} | |
var loser: Player | |
var cards: [Int] | |
if winner.name == playerOne.name { | |
loser = playerTwo | |
cards = [cardOne, cardTwo] | |
} | |
else { | |
loser = playerOne | |
cards = [cardTwo, cardOne] | |
} | |
winner.deck.append(contentsOf: cards) | |
return (winner, loser) | |
} | |
} |
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
import Foundation | |
func run(_ name: String, _ game: () -> Game) { | |
print("=== \(name) ===") | |
print(" Part One") | |
let partOne = game().play() | |
print(" Winner = \(partOne.name), Cards = \(partOne.deck.count)") | |
print(" Score = \(partOne.calculateScore())") | |
print(" Part Two") | |
let partTwo = game().play(true) | |
print(" Winner = \(partTwo.name), Cards = \(partTwo.deck.count)") | |
print(" Score = \(partTwo.calculateScore())") | |
print("=== End ===") | |
} | |
run("Test Input", { | |
return Game( | |
Player("Player 1", [ | |
9, 2, 6, 3, 1 | |
]), | |
Player("Player 2", [ | |
5, 8, 4, 7, 10 | |
]) | |
) | |
}) | |
run("Real Input", { | |
return Game( | |
Player("Player 1", [ | |
// fill in your input | |
]), | |
Player("Player 2", [ | |
// fill in your input | |
]) | |
) | |
}) |
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
import Foundation | |
class Player { | |
var name: String | |
var deck: [Int] | |
init(_ name: String, _ deck: [Int]) { | |
self.name = name | |
self.deck = deck | |
} | |
func calculateScore() -> Int { | |
var result = 0 | |
for (i, card) in deck.enumerated() { | |
result += (deck.count - i) * card | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment