aoc2021/Sources/09/09.swift

141 lines
3.7 KiB
Swift
Raw Normal View History

2021-12-09 06:35:17 +00:00
//
// File.swift
//
//
// Created by Max Nuding on 05.12.21.
//
import Foundation
import Runner
import Collections
2021-12-09 18:51:45 +00:00
struct Coord: Hashable {
2021-12-09 06:35:17 +00:00
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) }
}
2021-12-09 06:48:45 +00:00
struct Field:Sequence, IteratorProtocol {
2021-12-09 18:51:45 +00:00
typealias Element = Coord
2021-12-09 06:35:17 +00:00
let numbers: [[Int]]
let numCols: Int
let numRows: Int
2021-12-09 19:01:26 +00:00
var neighbors = [Coord:[Coord]]()
2021-12-09 06:48:45 +00:00
var current: Coord? = Coord(row: 0, col: 0)
2021-12-09 06:35:17 +00:00
init(numbers: [[Int]]) {
self.numbers = numbers
2021-12-09 18:51:45 +00:00
numCols = numbers.first?.count ?? 0
2021-12-09 06:35:17 +00:00
numRows = numbers.count
}
2021-12-09 19:01:26 +00:00
mutating func getNeighborsCoordsFor(coord: Coord) -> [Coord] {
let n = neighbors[coord] ?? [coord.getAbove(), coord.getRight(), coord.getBelow(), coord.getLeft()]
2021-12-09 06:35:17 +00:00
.compactMap { $0 }
.filter { $0.row < numRows && $0.col < numCols }
2021-12-09 19:01:26 +00:00
neighbors[coord] = n
return n
2021-12-09 06:35:17 +00:00
}
2021-12-09 19:01:26 +00:00
mutating func isLowPointAt(coord: Coord) -> Bool {
2021-12-09 18:51:45 +00:00
let value = self[coord]
2021-12-09 06:35:17 +00:00
return getNeighborsCoordsFor(coord: coord)
.map { numbers[$0.row][$0.col] }
.allSatisfy { $0 > value }
}
2021-12-09 06:48:45 +00:00
private func getNextCoord() -> Coord? {
guard let current = current else {
return nil
}
if current.col == numCols - 1 && current.row == numRows - 1 {
return nil
}
if current.col == numCols - 1 {
return Coord(row: current.row + 1, col: 0)
}
return Coord(row: current.row, col: current.col + 1)
}
mutating func next() -> Coord? {
defer {
current = getNextCoord()
}
return current
}
2021-12-09 18:51:45 +00:00
subscript(index: Coord) -> Int {
get {
numbers[index.row][index.col]
}/*
set(newValue) {
// Perform a suitable setting action here.
}*/
}
2021-12-09 06:35:17 +00:00
}
2021-12-09 18:51:45 +00:00
class Day09: Runnable {
2021-12-09 06:35:17 +00:00
let inputPath: String
2021-12-09 18:51:45 +00:00
var lowestNeighbors = [Coord: Coord]()
var lowPoints = Set<Coord>()
var field = Field(numbers: [[Int]]())
required init(inputPath: String) {
self.inputPath = inputPath
}
2021-12-09 06:35:17 +00:00
2021-12-09 18:51:45 +00:00
public func run() {
2021-12-09 06:35:17 +00:00
let input = try! String(contentsOfFile: inputPath)
let entries = input
.trimmingCharacters(in: .newlines)
.components(separatedBy: .newlines)
.map { line in line.map { $0.wholeNumberValue! } }
2021-12-09 18:51:45 +00:00
field = Field(numbers: entries)
lowPoints = Set(field.filter { field.isLowPointAt(coord: $0) })
//Part 1
let sum = lowPoints.map { field[$0] }.reduce(0, +) + lowPoints.count
2021-12-09 06:35:17 +00:00
print(sum)
2021-12-09 18:51:45 +00:00
// Part 2:
var basinSize = [Coord: Int]()
for coord in field {
guard field[coord] != 9 else {
continue
}
let bl = basinLocation(for: coord)
basinSize[bl, default: 0] += 1
}
let result = basinSize.values.sorted(by: >)[0..<3].reduce(1, *)
print(result)
}
private func basinLocation(for coord: Coord) -> Coord {
guard !lowPoints.contains(coord) else {
return coord
}
let closestNeighbor = lowestNeighbors[coord] ?? field.getNeighborsCoordsFor(coord: coord).min(by: { field[$0] < field[$1] })!
lowestNeighbors[coord] = closestNeighbor
return basinLocation(for: closestNeighbor)
2021-12-09 06:35:17 +00:00
}
}