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