From c6e3288cb8b3ebff28a76ec5e4019d98d1b2ad43 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Thu, 9 Dec 2021 07:35:17 +0100 Subject: [PATCH] 09a --- Package.swift | 7 +- Sources/09/09.swift | 82 +++++++++++++++++ Sources/09/main.swift | 12 +++ Sources/Runner/Resources/input/09.txt | 100 +++++++++++++++++++++ Sources/Runner/Resources/input/09_test.txt | 5 ++ 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 Sources/09/09.swift create mode 100644 Sources/09/main.swift create mode 100644 Sources/Runner/Resources/input/09.txt create mode 100644 Sources/Runner/Resources/input/09_test.txt diff --git a/Package.swift b/Package.swift index d95acbc..9dabc11 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,8 @@ let package = Package( .executable(name: "05", targets: ["05"]), .executable(name: "06", targets: ["06"]), .executable(name: "07", targets: ["07"]), - .executable(name: "08", targets: ["08"]) + .executable(name: "08", targets: ["08"]), + .executable(name: "09", targets: ["09"]) ], dependencies: [ // Dependencies declare other packages that this package depends on. @@ -75,6 +76,10 @@ let package = Package( .product(name: "Collections", package: "swift-collections"), .targetItem(name: "Runner", condition: nil) ] + ), + .executableTarget( + name: "09", + dependencies: [.targetItem(name: "Runner", condition: nil)] ) ] ) diff --git a/Sources/09/09.swift b/Sources/09/09.swift new file mode 100644 index 0000000..fb01a62 --- /dev/null +++ b/Sources/09/09.swift @@ -0,0 +1,82 @@ +// +// File.swift +// +// +// Created by Max Nuding on 05.12.21. +// + +import Foundation +import Runner +import Collections + +struct Coord { + let row: Int + let col: Int + + func getLeft() -> Coord? { + guard col > 0 else { + return nil + } + return Coord(row: row, col: col - 1) + } + + func getRight() -> Coord { Coord(row: row, col: col + 1) } + + func getAbove() -> Coord? { + guard row > 0 else { + return nil + } + return Coord(row: row - 1, col: col) + } + + func getBelow() -> Coord { Coord(row: row + 1, col: col) } +} + +struct Field { + let numbers: [[Int]] + let numCols: Int + let numRows: Int + + init(numbers: [[Int]]) { + self.numbers = numbers + numCols = numbers.first!.count + numRows = numbers.count + } + + func getNeighborsCoordsFor(coord: Coord) -> [Coord] { + return [coord.getAbove(), coord.getRight(), coord.getBelow(), coord.getLeft()] + .compactMap { $0 } + .filter { $0.row < numRows && $0.col < numCols } + } + + func lowPointValueAt(coord: Coord) -> Int? { + let value = numbers[coord.row][coord.col] + return getNeighborsCoordsFor(coord: coord) + .map { numbers[$0.row][$0.col] } + .allSatisfy { $0 > value } + ? value + : nil + } +} + +struct Day09: Runnable { + let inputPath: String + + func run() { + let input = try! String(contentsOfFile: inputPath) + let entries = input + .trimmingCharacters(in: .newlines) + .components(separatedBy: .newlines) + .map { line in line.map { $0.wholeNumberValue! } } + let field = Field(numbers: entries) + var sum = 0 + for row in 0..