Skip to content

Commit a1e8d32

Browse files
committed
day5 done
1 parent 0353971 commit a1e8d32

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Below is my current advancement in Advent of code:
1212
| Day 2 | :star: | :star: |
1313
| Day 3 | :star: | :star: |
1414
| Day 4 | :star: | :star: |
15-
| Day 5 | :star: (22min) | |
15+
| Day 5 | :star: (22min) | :star: (21min) |
1616
| Day 6 | | |
1717
| Day 7 | | |
1818
| Day 8 | | |

Sources/Day05.swift

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import Algorithms
22

3-
struct Rule {
4-
var before: Int
5-
var after: Int
6-
7-
}
8-
extension Rule {
9-
init(data: Substring) {
10-
let parts = data.split(separator: "|")
11-
before = Int(parts[0]) ?? 0
12-
after = Int(parts[1]) ?? 0
13-
}
14-
}
15-
163
struct Day05: AdventDay {
174
var rules: [Int: [Int]]
185
var updates: [[Int]]
196

7+
func middlePage(_ update: [Int]) -> Int {
8+
update[update.count / 2]
9+
}
10+
2011
init(data: String) {
2112
let parts = data.split(separator: "\n\n")
2213
rules = [Int: [Int]]()
@@ -29,22 +20,44 @@ struct Day05: AdventDay {
2920
updates = parts[1].lines().map { $0.integers(separator: ",") }
3021
}
3122

23+
func isCorrect(_ update: [Int]) -> Bool {
24+
for (idx, page) in update.enumerated() {
25+
let rulesForPage = rules[page, default: [Int]()]
26+
let brokenRules = rulesForPage.map { update[idx + 1..<update.count].contains($0) }.count {
27+
$0 == true
28+
}
29+
if brokenRules > 0 {
30+
return false
31+
}
32+
}
33+
return true
34+
}
35+
3236
func part1() -> Int {
33-
updates.filter { update in
34-
for (idx, page) in update.enumerated() {
35-
let rulesForPage = rules[page, default: [Int]()]
36-
let brokenRules = rulesForPage.map { update[idx + 1..<update.count].contains($0) }.count {
37-
$0 == true
38-
}
39-
if brokenRules > 0 {
40-
return false
37+
updates.filter { isCorrect($0) }.map { middlePage($0) }.sum
38+
}
39+
40+
func fixOrder(_ update: [Int]) -> [Int] {
41+
var fixedUpdate = update
42+
for (idx, page) in update.enumerated() {
43+
let rulesForPage = rules[page, default: [Int]()]
44+
let brokenRules = rulesForPage.map { update[idx + 1..<update.count].contains($0) }
45+
for (isBroken, infringingPage) in zip(brokenRules, rulesForPage) {
46+
if isBroken {
47+
if let infringingIndex = fixedUpdate.firstIndex(of: infringingPage) {
48+
let value = fixedUpdate.remove(at: infringingIndex)
49+
fixedUpdate.insert(value, at: idx)
50+
}
4151
}
4252
}
43-
return true
44-
}.map { update in update[update.count / 2] }.sum
53+
}
54+
if !isCorrect(fixedUpdate) {
55+
fixedUpdate = fixOrder(fixedUpdate)
56+
}
57+
return fixedUpdate
4558
}
4659

4760
func part2() -> Int {
48-
0
61+
updates.filter { !isCorrect($0) }.map { middlePage(fixOrder($0)) }.sum
4962
}
5063
}

Tests/Day04.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct Day04Tests {
2727
XMAS
2828
""")
2929
#expect(
30-
searchInNeighbors(pos: Coord(x: 0, y: 0), grid) == [Coord(x: 1, y: 0), Coord(x: 1, y: 1)])
30+
searchInNeighbors(pos: Coord(x: 0, y: 0), grid) == [Coord(x: 1, y: 1), Coord(x: 1, y: 0)])
3131
}
3232

3333
@Test("part1 simple")
@@ -39,7 +39,7 @@ struct Day04Tests {
3939
XMAS
4040
"""
4141
let challenge = Day04(data: grid)
42-
#expect(challenge.part1() == 2)
42+
#expect(challenge.part1() == 6)
4343
}
4444

4545
@Test("part1")

Tests/Day05.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct Day05Tests {
5050
@Test("part2")
5151
func testPart2() async throws {
5252
let challenge = Day05(data: testData)
53-
#expect(challenge.part2() == 0)
53+
#expect(challenge.fixOrder([75, 97, 47, 61, 53]) == [97, 75, 47, 61, 53])
54+
#expect(challenge.fixOrder([61, 13, 29]) == [61, 29, 13])
55+
#expect(challenge.fixOrder([97, 13, 75, 29, 47]) == [97, 75, 47, 29, 13])
56+
#expect(challenge.part2() == 123)
5457
}
5558
}

0 commit comments

Comments
 (0)