Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Last active December 22, 2020 07:59
Show Gist options
  • Save felix-larsen/6274310ca441cf1eaba30e3685f058c5 to your computer and use it in GitHub Desktop.
Save felix-larsen/6274310ca441cf1eaba30e3685f058c5 to your computer and use it in GitHub Desktop.
22th December solution - Advent of Code 2020 - swift
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)
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)
}
import Foundation
let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december22.txt"
let contents = try! String(contentsOfFile: filename)
let lines = contents.components(separatedBy: CharacterSet.newlines).filter { !$0.isEmpty}
var player1Cards = [Int]()
var player2Cards = [Int]()
var player1 = true
for line in lines {
if line.contains("Player 1") {
player1 = true
} else if line.contains("Player 2") {
player1 = false
} else {
if player1 {
player1Cards.append(Int(line)!)
} else {
player2Cards.append(Int(line)!)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment