05b
This commit is contained in:
parent
4d88fb7905
commit
951cf5f12c
@ -22,20 +22,29 @@ struct Line: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getCoveredPoints() -> [Point] {
|
func getCoveredPoints() -> [Point] {
|
||||||
guard isStraight else {
|
|
||||||
return [Point]()
|
|
||||||
}
|
|
||||||
|
|
||||||
var points = [Point]()
|
var points = [Point]()
|
||||||
let startX = min(from.x, to.x)
|
let startX = min(from.x, to.x)
|
||||||
let endX = max(from.x, to.x)
|
let endX = max(from.x, to.x)
|
||||||
let startY = min(from.y, to.y)
|
let startY = min(from.y, to.y)
|
||||||
let endY = max(from.y, to.y)
|
let endY = max(from.y, to.y)
|
||||||
|
|
||||||
for x in startX...endX {
|
if isStraight {
|
||||||
for y in startY...endY {
|
for x in startX...endX {
|
||||||
points.append(Point(x: x, y: y))
|
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 {
|
||||||
|
points.append(Point(x: x, y: y))
|
||||||
|
x += dirX
|
||||||
|
y += dirY
|
||||||
|
}
|
||||||
|
points.append(to)
|
||||||
}
|
}
|
||||||
return points
|
return points
|
||||||
}
|
}
|
||||||
@ -50,8 +59,8 @@ struct Field: CustomStringConvertible {
|
|||||||
var coveredPoints = [Point: Int]()
|
var coveredPoints = [Point: Int]()
|
||||||
|
|
||||||
var description: String {
|
var description: String {
|
||||||
let minX = coveredPoints.min(by: {$0.key.x < $1.key.x})?.key.x ?? 0
|
let minX = 0
|
||||||
let minY = coveredPoints.min(by: {$0.key.y < $1.key.y})?.key.y ?? 0
|
let minY = 0
|
||||||
let maxX = coveredPoints.max(by: {$0.key.x < $1.key.x})?.key.x ?? 0
|
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
|
let maxY = coveredPoints.max(by: {$0.key.y < $1.key.y})?.key.y ?? 0
|
||||||
var desc = "Field from \(minX), \(minY) to \(maxX), \(maxY))\n"
|
var desc = "Field from \(minX), \(minY) to \(maxX), \(maxY))\n"
|
||||||
@ -80,7 +89,9 @@ struct Day05 {
|
|||||||
.filter { !$0.isEmpty }
|
.filter { !$0.isEmpty }
|
||||||
.compactMap { Line(inputLine: $0) }
|
.compactMap { Line(inputLine: $0) }
|
||||||
run(lines: lines.filter { $0.isStraight })
|
run(lines: lines.filter { $0.isStraight })
|
||||||
|
run(lines: lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(lines: [Line]) {
|
func run(lines: [Line]) {
|
||||||
var field = Field()
|
var field = Field()
|
||||||
//let straightLines = lines.filter { $0.isStraight }
|
//let straightLines = lines.filter { $0.isStraight }
|
||||||
@ -89,7 +100,7 @@ struct Day05 {
|
|||||||
field.cover(point: point)
|
field.cover(point: point)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print(field)
|
//print(field)
|
||||||
print(field.coveredPoints.filter { $0.value > 1}.count)
|
print(field.coveredPoints.filter { $0.value > 1 }.count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ print("Finished in \(execTimeMs)ms")
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
print("Starting day 05")
|
print("Starting day 05")
|
||||||
let input03 = Bundle.module.path(forResource: "05_test", ofType: ".txt")!
|
let input03 = Bundle.module.path(forResource: "05", ofType: ".txt")!
|
||||||
let start = CFAbsoluteTimeGetCurrent()
|
let start = CFAbsoluteTimeGetCurrent()
|
||||||
Day05(inputPath: input03).run()
|
Day05(inputPath: input03).run()
|
||||||
let end = CFAbsoluteTimeGetCurrent()
|
let end = CFAbsoluteTimeGetCurrent()
|
||||||
|
Loading…
Reference in New Issue
Block a user