04a
This commit is contained in:
parent
b0609897df
commit
d341f9b45c
@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import AppKit
|
|
||||||
|
|
||||||
struct Day03 {
|
struct Day03 {
|
||||||
let inputPath: String
|
let inputPath: String
|
||||||
|
87
Sources/aoc2021/04.swift
Normal file
87
Sources/aoc2021/04.swift
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
//
|
||||||
|
// File.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Max Nuding on 04.12.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct Board {
|
||||||
|
var numbers: [[Int?]]
|
||||||
|
var hasWon = false
|
||||||
|
|
||||||
|
mutating func crossOf(num: Int) -> Bool {
|
||||||
|
for (rowNumber, row) in numbers.enumerated() {
|
||||||
|
if let colNumber = row.firstIndex(of: num) {
|
||||||
|
numbers[rowNumber][colNumber] = nil
|
||||||
|
//print("Found at: \(rowNumber),\(colNumber)")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func checkHasWon() -> Bool {
|
||||||
|
//print("HasWon?: \(numbers)")
|
||||||
|
let foundInRows = numbers.contains { row in
|
||||||
|
row.allSatisfy { $0 == nil }
|
||||||
|
}
|
||||||
|
if foundInRows {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
let foundInColumns = numbers.indices.contains { columnIndex in
|
||||||
|
return numbers.allSatisfy { $0[columnIndex] == nil }
|
||||||
|
}
|
||||||
|
hasWon = foundInColumns
|
||||||
|
return hasWon
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateScore() -> Int {
|
||||||
|
numbers
|
||||||
|
.reduce([], +)
|
||||||
|
.compactMap { $0 }
|
||||||
|
.reduce(0, +)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Day04 {
|
||||||
|
let inputPath: String
|
||||||
|
|
||||||
|
func run() {
|
||||||
|
let input = try! String(contentsOfFile: inputPath)
|
||||||
|
var lines = input
|
||||||
|
.components(separatedBy: .newlines)
|
||||||
|
let inputNumbers = lines
|
||||||
|
.removeFirst()
|
||||||
|
.split(separator: ",")
|
||||||
|
.map { Int($0)! }
|
||||||
|
_ = lines.removeFirst() //empty seperator line
|
||||||
|
|
||||||
|
let boards = 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runA(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.crossOf(num: inputNumber) && board.checkHasWon() {
|
||||||
|
print(board.calculateScore() * inputNumber)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
boards[idx] = board
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runB(_ commands: [String]) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
19
Sources/aoc2021/Resources/input/04.txt
Normal file
19
Sources/aoc2021/Resources/input/04.txt
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
||||||
|
|
||||||
|
22 13 17 11 0
|
||||||
|
8 2 23 4 24
|
||||||
|
21 9 14 16 7
|
||||||
|
6 10 3 18 5
|
||||||
|
1 12 20 15 19
|
||||||
|
|
||||||
|
3 15 0 2 22
|
||||||
|
9 18 13 17 5
|
||||||
|
19 8 7 25 23
|
||||||
|
20 11 10 24 4
|
||||||
|
14 21 16 12 6
|
||||||
|
|
||||||
|
14 21 17 24 4
|
||||||
|
10 16 15 9 19
|
||||||
|
18 8 23 26 20
|
||||||
|
22 11 13 6 5
|
||||||
|
2 0 12 3 7
|
@ -20,6 +20,7 @@ let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
|||||||
print("Finished in \(execTimeMs)ms")
|
print("Finished in \(execTimeMs)ms")
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
print("Starting day 03")
|
print("Starting day 03")
|
||||||
let input03 = Bundle.module.path(forResource: "03", ofType: ".txt")!
|
let input03 = Bundle.module.path(forResource: "03", ofType: ".txt")!
|
||||||
let start = CFAbsoluteTimeGetCurrent()
|
let start = CFAbsoluteTimeGetCurrent()
|
||||||
@ -27,3 +28,12 @@ Day03(inputPath: input03).run()
|
|||||||
let end = CFAbsoluteTimeGetCurrent()
|
let end = CFAbsoluteTimeGetCurrent()
|
||||||
let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
||||||
print("Finished in \(execTimeMs)ms")
|
print("Finished in \(execTimeMs)ms")
|
||||||
|
*/
|
||||||
|
|
||||||
|
print("Starting day 04")
|
||||||
|
let input03 = Bundle.module.path(forResource: "04", ofType: ".txt")!
|
||||||
|
let start = CFAbsoluteTimeGetCurrent()
|
||||||
|
Day04(inputPath: input03).run()
|
||||||
|
let end = CFAbsoluteTimeGetCurrent()
|
||||||
|
let execTimeMs = round((end - start) * 1000.0 * 100.0) / 100.0
|
||||||
|
print("Finished in \(execTimeMs)ms")
|
||||||
|
Loading…
Reference in New Issue
Block a user