aoc2021/Sources/05/05.swift

108 lines
2.9 KiB
Swift
Raw Normal View History

2021-12-05 08:14:34 +00:00
//
// File.swift
2021-12-05 10:43:02 +00:00
//
2021-12-05 08:14:34 +00:00
//
// Created by Max Nuding on 05.12.21.
//
import Foundation
2021-12-05 10:43:02 +00:00
import Runner
2021-12-05 08:14:34 +00:00
struct Line: CustomStringConvertible {
let from: Point
let to: Point
var description: String { "\(from.x),\(from.y) -> \(to.x),\(to.y)" }
var isStraight: Bool { from.x == to.x || from.y == to.y }
init(inputLine: String) {
let points = inputLine.components(separatedBy: " -> ")
let res = points.map { point in point.split(separator: ",").map { Int($0)! } }
from = Point(x: res[0][0], y: res[0][1])
to = Point(x: res[1][0], y: res[1][1])
}
func getCoveredPoints() -> [Point] {
var points = [Point]()
let startX = min(from.x, to.x)
let endX = max(from.x, to.x)
let startY = min(from.y, to.y)
let endY = max(from.y, to.y)
2021-12-05 09:03:08 +00:00
if isStraight {
for x in startX...endX {
for y in startY...endY {
points.append(Point(x: x, y: y))
}
}
} else {
var x = from.x
var y = from.y
let dirX = from.x > to.x ? -1 : 1
let dirY = from.y > to.y ? -1 : 1
while x != to.x {
2021-12-05 08:14:34 +00:00
points.append(Point(x: x, y: y))
2021-12-05 09:03:08 +00:00
x += dirX
y += dirY
2021-12-05 08:14:34 +00:00
}
2021-12-05 09:03:08 +00:00
points.append(to)
2021-12-05 08:14:34 +00:00
}
return points
}
}
struct Point: Hashable {
let x: Int
let y: Int
}
struct Field: CustomStringConvertible {
var coveredPoints = [Point: Int]()
var description: String {
2021-12-05 09:03:08 +00:00
let minX = 0
let minY = 0
2021-12-05 08:14:34 +00:00
let maxX = coveredPoints.max(by: {$0.key.x < $1.key.x})?.key.x ?? 0
let maxY = coveredPoints.max(by: {$0.key.y < $1.key.y})?.key.y ?? 0
var desc = "Field from \(minX), \(minY) to \(maxX), \(maxY))\n"
for y in minY...maxY {
for x in minX...maxX {
let val = coveredPoints[Point(x: x, y: y)] ?? 0
desc += val == 0 ? "." : "\(val)"
}
desc += "\n"
}
return desc
}
mutating func cover(point: Point) {
coveredPoints[point] = (coveredPoints[point] ?? 0) + 1
}
}
2021-12-05 10:43:02 +00:00
struct Day05: Runnable {
2021-12-05 08:14:34 +00:00
let inputPath: String
func run() {
let input = try! String(contentsOfFile: inputPath)
let lines = input
.components(separatedBy: .newlines)
.filter { !$0.isEmpty }
.compactMap { Line(inputLine: $0) }
run(lines: lines.filter { $0.isStraight })
2021-12-05 09:03:08 +00:00
run(lines: lines)
2021-12-05 08:14:34 +00:00
}
2021-12-05 09:03:08 +00:00
2021-12-05 08:14:34 +00:00
func run(lines: [Line]) {
var field = Field()
//let straightLines = lines.filter { $0.isStraight }
for line in lines {
for point in line.getCoveredPoints() {
field.cover(point: point)
}
}
2021-12-05 09:03:08 +00:00
//print(field)
print(field.coveredPoints.filter { $0.value > 1 }.count)
2021-12-05 08:14:34 +00:00
}
}