-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57b4e10
commit bc40056
Showing
9 changed files
with
295 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ public struct AdventOfCode2023: Year { | |
Day8(), | ||
Day9(), | ||
Day10(), | ||
Day11(), | ||
// {DAYS} | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Algorithms | ||
import Foundation | ||
import Utilities | ||
|
||
public struct Day11: Day { | ||
public func part1Solution(for input: Input) -> Int { | ||
solution(for: input, expansion: 2) | ||
} | ||
|
||
public func part2Solution(for input: Input) -> Int { | ||
solution(for: input, expansion: 1000000) | ||
} | ||
|
||
func solution(for input: Input, expansion: Int) -> Int { | ||
let grid = Grid(input.lines.characters) { _, input in | ||
input == "." ? nil : () | ||
} | ||
|
||
let range = grid.keys.pointRange() | ||
|
||
var xSpace = IndexRangeSet(ranges: [range.x]) | ||
var ySpace = IndexRangeSet(ranges: [range.y]) | ||
|
||
for point in grid.keys { | ||
xSpace.remove(point.x) | ||
ySpace.remove(point.y) | ||
} | ||
|
||
return grid.keys | ||
.sorted() | ||
.combinations(ofCount: 2) | ||
.map { combination in | ||
let range = combination.pointRange() | ||
|
||
return combination[0].manhattanDistance(to: combination[1]) | ||
+ xSpace.map { $0.overlaps(range.x) ? $0.count : 0 }.sum() * (expansion - 1) | ||
+ ySpace.map { $0.overlaps(range.y) ? $0.count : 0 }.sum() * (expansion - 1) | ||
} | ||
.sum() | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
public extension ClosedRange where Bound == Int { | ||
func offset(_ amount: Bound) -> Self { | ||
(lowerBound + amount)...(upperBound + amount) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Foundation | ||
|
||
public extension ClosedRange where Bound == Int { | ||
/// Splits a range based on the overlap with another range | ||
/// - Parameter other: The other range to use for splitting. | ||
/// - Returns: `nil` if the ranges don't overlap. Otherwise it returns a tuple containing the | ||
/// range of the overlap and optionally the lower and/or upper remainders that are | ||
/// outside the overlapping range. | ||
func split(by other: Self) -> (overlap: Self, lowerRemainder: Self?, upperRemainder: Self?)? { | ||
guard overlaps(other) else { return nil } | ||
|
||
let lowerRemainder = lowerBound < other.lowerBound | ||
? lowerBound...other.lowerBound - 1 | ||
: nil | ||
|
||
let upperRemainder = upperBound > other.upperBound | ||
? other.upperBound + 1...upperBound | ||
: nil | ||
|
||
return ( | ||
clamped(to: other), | ||
lowerRemainder, | ||
upperRemainder | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Utilities | ||
import XCTest | ||
@testable import AdventOfCode2023 | ||
|
||
final class Day11Tests: XCTestCase { | ||
let day = Day11() | ||
|
||
let input: Input = """ | ||
...#...... | ||
.......#.. | ||
#......... | ||
.......... | ||
......#... | ||
.#........ | ||
.........# | ||
.......... | ||
.......#.. | ||
#...#..... | ||
""" | ||
|
||
func testPart1() { | ||
XCTAssertEqual(day.part1Solution(for: input), 374) | ||
XCTAssertEqual(day.part1Solution(), 9639160) | ||
} | ||
|
||
func testPart2() { | ||
XCTAssertEqual(day.solution(for: input, expansion: 10), 1030) | ||
XCTAssertEqual(day.solution(for: input, expansion: 100), 8410) | ||
XCTAssertEqual(day.part2Solution(), 752936133304) | ||
} | ||
} |