Skip to content

Commit d18b456

Browse files
committed
day2
1 parent 4a8dfd7 commit d18b456

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Below is my current advancement in Advent of code:
99
| Day | Part 1 | Part 2 |
1010
|:---|:---:|:---:|
1111
| Day 1 | :star: | :star: |
12-
| Day 2 | | |
12+
| Day 2 | :star: | :star: |
1313
| Day 3 | | |
1414
| Day 4 | | |
1515
| Day 5 | | |

Sources/Day02.swift

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Algorithms
22

33
struct Report {
44
var value: [Int]
5-
65
}
76
extension Report {
87
init(_ data: Substring) {
@@ -14,6 +13,16 @@ func spreadsWell(_ value: Int) -> Bool {
1413
abs(value) <= 3 && abs(value) >= 1
1514
}
1615

16+
func getPermutationsExcludingOneElement(_ arr: [Int]) -> [[Int]] {
17+
var perms = [[Int]]()
18+
for idx in 0..<arr.count {
19+
var copiedArr = arr
20+
let _ = copiedArr.remove(at: idx)
21+
perms.append(copiedArr)
22+
}
23+
return perms
24+
}
25+
1726
struct ReportAnalyzer {
1827

1928
func isSafe(report: Report, damped: Bool = false) -> Bool {
@@ -33,25 +42,14 @@ struct ReportAnalyzer {
3342
let spread = diffs.filter { spreadsWell($0) }
3443

3544
let isSafe = order.count == 1 && spread.count == diffs.count
36-
if damped {
37-
38-
let orderIsOffByOne =
39-
order.count == 2
40-
&& (order["desc", default: []].count == 1 || order["asc", default: []].count == 1)
41-
// let spreadIsOffByOne = spread.count == diffs.count - 1
42-
let spreadDefect = diffs.firstIndex { spreadsWell($0) == false }
43-
if let defect = spreadDefect {
44-
print(defect, report.value)
45-
return ReportAnalyzer().isSafe(
46-
report: Report(
47-
value: report.value.filter {
48-
$0 == report.value[defect + 1]
49-
}))
50-
}
51-
// print("\(report): safe (\(isSafe)) order (\(orderIsOffByOne)) spread (\(spreadIsOffByOne))")
52-
return isSafe || orderIsOffByOne
45+
if damped && !isSafe {
46+
let reportPermutations = getPermutationsExcludingOneElement(report.value)
47+
let isAnySafe =
48+
reportPermutations.map {
49+
return ReportAnalyzer().isSafe(report: Report(value: Array($0)))
50+
}.filter { $0 }.count >= 1
51+
return isAnySafe
5352
}
54-
5553
return isSafe
5654
}
5755
}
@@ -70,6 +68,5 @@ struct Day02: AdventDay {
7068

7169
func part2() -> Int {
7270
reports.map { ReportAnalyzer().isSafe(report: $0, damped: true) }.filter { $0 }.count
73-
7471
}
7572
}

Tests/Day02.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@ struct Day02Tests {
3030
let challenge = Day02(data: testData)
3131
#expect(challenge.part2() == 4)
3232
}
33+
34+
@Test("perms")
35+
func testPerms() async throws {
36+
#expect(getPermutationsExcludingOneElement([1, 2, 3]) == [[2, 3], [1, 3], [1, 2]])
37+
}
38+
39+
@Test("more cases")
40+
func testMoreCases() async throws {
41+
#expect(
42+
Day02(
43+
data: """
44+
1 2 3 4 10 11
45+
10 9 8 7 1
46+
"""
47+
).part2() == 1)
48+
}
3349
}

0 commit comments

Comments
 (0)