Skip to content

Commit

Permalink
Factor out remove(atOffsets:)
Browse files Browse the repository at this point in the history
Strangely, this extension method seems to be declared in SwiftUI on
Apple platforms, rather than in the standard library where I would have
expected it...
  • Loading branch information
fwcd committed Jan 13, 2024
1 parent e6ca724 commit dda836a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions day19/src/day19.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ extension Range {
}
}

extension Array {
mutating func remove(atOffsets indexSet: IndexSet) {
self = enumerated().filter { !indexSet.contains($0.offset) }.map(\.element)
}
}

/// An n-dimensional generalization of a range/rectangle/cuboid.
struct Hyperrect {
/// The ranges along each axis.
Expand Down Expand Up @@ -307,10 +313,10 @@ extension System {
}

func acceptedCombinations(categories: [String] = ["x", "m", "a", "s"], total: Range<Int> = 1..<4001) throws -> Int {
let categoryIndices = Dictionary(uniqueKeysWithValues: categories.enumerated().map { ($0.element, $0.offset) })
let workflow = try mergedWorkflow()
var remaining: [Hyperrect] = [.init(ranges: Array(repeating: total, count: categories.count))]
var accepted: Int = 0
var categoryIndices = Dictionary(uniqueKeysWithValues: categories.enumerated().map { ($0.element, $0.offset) })

for rule in workflow.rules {
var toBeRemoved = IndexSet()
Expand Down Expand Up @@ -339,7 +345,7 @@ extension System {
}
}

remaining = remaining.enumerated().filter { !toBeRemoved.contains($0.offset) }.map(\.element)
remaining.remove(atOffsets: toBeRemoved)
remaining += toBeAdded
}

Expand Down

0 comments on commit dda836a

Please sign in to comment.