Skip to content

Commit 247bf7a

Browse files
committed
Add 2024, day 5, part 1
1 parent ac567b1 commit 247bf7a

File tree

3 files changed

+1452
-0
lines changed

3 files changed

+1452
-0
lines changed

Year2024/Sources/Day05.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
import Advent
3+
4+
public enum Day05: Day {
5+
6+
public static let title = "Print Queue"
7+
8+
public static func part1(_ input: Input) throws -> Int {
9+
let sections = input.lines
10+
.split(separator: "")
11+
12+
let rules = try sections
13+
.first.unwrapped
14+
.map { try $0.split(separator: "|").map(Int.init) }
15+
.map { try ($0.first.unwrapped, $0.last.unwrapped) }
16+
.group(by: \.0)
17+
.mapValues { Set($0.map(\.1)) }
18+
19+
return try sections
20+
.last.unwrapped
21+
.map { try $0.split(separator: ",").map(Int.init) }
22+
.filter {
23+
var seen: Set<Int> = []
24+
return $0.allSatisfy { value in
25+
defer { seen.insert(value) }
26+
let disallowed = rules[value] ?? []
27+
return seen.intersection(disallowed).count == 0
28+
}
29+
}
30+
.sum(of: \.middle)
31+
}
32+
33+
public static func part2(_ input: Input) throws -> Int {
34+
0
35+
}
36+
}

Year2024/Tests/Day05Tests.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import Advent
3+
import Year2024
4+
import XCTest
5+
6+
final class Day05Tests: XCTestCase {
7+
8+
func testPart1Examples() throws {
9+
XCTAssertEqual(try Day05.part1([
10+
"47|53",
11+
"97|13",
12+
"97|61",
13+
"97|47",
14+
"75|29",
15+
"61|13",
16+
"75|53",
17+
"29|13",
18+
"97|29",
19+
"53|29",
20+
"61|53",
21+
"97|53",
22+
"61|29",
23+
"47|13",
24+
"75|47",
25+
"97|75",
26+
"47|61",
27+
"75|61",
28+
"47|29",
29+
"75|13",
30+
"53|13",
31+
"",
32+
"75,47,61,53,29",
33+
"97,61,53,29,13",
34+
"75,29,13",
35+
"75,97,47,61,53",
36+
"61,13,29",
37+
"97,13,75,29,47",
38+
]), 143)
39+
}
40+
41+
func testPart1Puzzle() throws {
42+
let input = try Bundle.module.input(named: "Day05")
43+
XCTAssertEqual(try Day05.part1(input), 5091)
44+
}
45+
46+
func testPart2Examples() throws {
47+
XCTAssertEqual(try Day05.part2([]), 0)
48+
}
49+
50+
func testPart2Puzzle() throws {
51+
let input = try Bundle.module.input(named: "Day05")
52+
XCTAssertEqual(try Day05.part2(input), 0)
53+
}
54+
}

0 commit comments

Comments
 (0)