This commit is contained in:
Max Nuding 2021-12-04 11:09:56 +01:00
parent d341f9b45c
commit 42e442da09
Signed by: phlaym
GPG Key ID: A06651BAB6777237

View File

@ -58,13 +58,16 @@ struct Day04 {
.map { Int($0)! }
_ = lines.removeFirst() //empty seperator line
let boards = lines
let boardsInitial = lines
.split(separator: "")
.map { boardLines in
Board(numbers: boardLines.map { $0.split(separator: " ").map { num in Int(num)! } })
}
var boardsA = boards
runA(boards: &boardsA, inputNumbers: inputNumbers)
var boards = boardsInitial
runA(boards: &boards, inputNumbers: inputNumbers)
boards = boardsInitial
runB(boards: &boards, inputNumbers: inputNumbers)
}
func runA(boards: inout [Board], inputNumbers: [Int]) {
@ -81,7 +84,17 @@ struct Day04 {
}
}
func runB(_ commands: [String]) {
func runB(boards: inout [Board], inputNumbers: [Int]) {
for inputNumber in inputNumbers {
//print("\nInput: \(inputNumber)")
for (idx, var board) in boards.enumerated() {
//print("Checking board: \(idx)")
if !board.hasWon && board.crossOf(num: inputNumber) && board.checkHasWon() {
print(board.calculateScore() * inputNumber)
return
}
boards[idx] = board
}
}
}
}