04b - cleanup
This commit is contained in:
parent
c09561597e
commit
83abcbc6e3
@ -61,35 +61,22 @@ struct Day04 {
|
||||
.map { Int($0)! }
|
||||
_ = lines.removeFirst() //empty seperator line
|
||||
|
||||
let boardsInitial = lines
|
||||
var boards = lines
|
||||
.split(separator: "")
|
||||
.map { boardLines in
|
||||
Board(numbers: boardLines.map { $0.split(separator: " ").map { num in Int(num)! } })
|
||||
}
|
||||
var boards = boardsInitial
|
||||
runA(boards: &boards, inputNumbers: inputNumbers)
|
||||
|
||||
boards = boardsInitial
|
||||
runB(boards: &boards, inputNumbers: inputNumbers)
|
||||
run(boards: &boards, inputNumbers: inputNumbers)
|
||||
}
|
||||
|
||||
func runA(boards: inout [Board], inputNumbers: [Int]) {
|
||||
for inputNumber in inputNumbers {
|
||||
for (idx, var board) in boards.enumerated() {
|
||||
if board.crossOf(num: inputNumber) && board.checkHasWon() {
|
||||
print(board.calculateScore() * inputNumber)
|
||||
return
|
||||
}
|
||||
boards[idx] = board
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runB(boards: inout [Board], inputNumbers: [Int]) {
|
||||
func run(boards: inout [Board], inputNumbers: [Int]) {
|
||||
var countWon = 0
|
||||
for inputNumber in inputNumbers {
|
||||
for (idx, var board) in boards.enumerated() {
|
||||
if board.crossOf(num: inputNumber) && !board.hasWon && board.checkHasWon() {
|
||||
if countWon == 0 {
|
||||
print(board.calculateScore() * inputNumber)
|
||||
}
|
||||
countWon += 1
|
||||
}
|
||||
if countWon == boards.count {
|
||||
|
Loading…
Reference in New Issue
Block a user