From 6427bb0aac9a9c0dee481778187d97d54dc3eaea Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 25 Dec 2021 17:47:53 +0100 Subject: [PATCH] Day 22 part 2 --- day22.swift | 98 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/day22.swift b/day22.swift index 9a67df3..a27b374 100644 --- a/day22.swift +++ b/day22.swift @@ -3,44 +3,57 @@ import Foundation @main struct Day22: Puzzle { func run() { - var cube: [Point: Bool] = [:] - let activeRange = -50...50 - let scanner = Scanner(string: input) + var cubes: [Cube] = [] + while !scanner.isAtEnd { - let (command, xrange, yrange, zrange) = scanner.line() - if let xrange = xrange.intersection(with: activeRange) { - for x in xrange.clamped(to: activeRange) { - if let yrange = yrange.intersection(with: activeRange) { - for y in yrange.clamped(to: activeRange) { - if let zrange = zrange.intersection(with: activeRange) { - for z in zrange.clamped(to: activeRange) { - cube[Point(x: x, y: y, z: z)] = command - } - } - } + cubes.append(scanner.cube()) + } + + var xStepsSet: Set = [] + var yStepsSet: Set = [] + var zStepsSet: Set = [] + + for cube in cubes { + xStepsSet.insert(cube.start.x) + xStepsSet.insert(cube.end.x + 1) + yStepsSet.insert(cube.start.y) + yStepsSet.insert(cube.end.y + 1) + zStepsSet.insert(cube.start.z) + zStepsSet.insert(cube.end.z + 1) + } + + var xSteps = xStepsSet.sorted() + var ySteps = yStepsSet.sorted() + var zSteps = zStepsSet.sorted() + + let firstX = xSteps.removeFirst() + let firstY = ySteps.removeFirst() + let firstZ = zSteps.removeFirst() + + var part2 = 0 + + var x0 = firstX + for x in xSteps { + let possibleX = cubes.filter { $0.xRange.overlaps(x0.. Self? { - guard upperBound >= other.lowerBound && lowerBound <= other.upperBound else { return nil } - return clamped(to: other) + print("Part 2:", part2) } } @@ -50,6 +63,20 @@ struct Point: Hashable { var z: Int } +struct Cube { + var start: Point + var end: Point + var state: Bool + + var xRange: ClosedRange { start.x...end.x } + var yRange: ClosedRange { start.y...end.y } + var zRange: ClosedRange { start.z...end.z } + + func contains(_ point: Point) -> Bool { + xRange.contains(point.x) && yRange.contains(point.y) && zRange.contains(point.z) + } +} + extension Scanner { func onOff() -> Bool { if scanString("on") != nil { @@ -84,6 +111,15 @@ extension Scanner { let zRange = range() return (command, xRange, yRange, zRange) } + + func cube() -> Cube { + let (state, xrange, yrange, zrange) = line() + return Cube( + start: Point(x: xrange.lowerBound, y: yrange.lowerBound, z: zrange.lowerBound), + end: Point(x: xrange.upperBound, y: yrange.upperBound, z: zrange.upperBound), + state: state + ) + } } let input = """