diff --git a/Year2024/Sources/Day02.swift b/Year2024/Sources/Day02.swift index 28e3051..60ec671 100644 --- a/Year2024/Sources/Day02.swift +++ b/Year2024/Sources/Day02.swift @@ -11,15 +11,32 @@ public enum Day02: Day { .split(whereSeparator:(\.isWhitespace)) .map(Int.init) - let differences = zip(values, values.dropFirst()) - .map(-) - - return differences.map(abs).allSatisfy { $0 < 4 } - && differences.map(\.signum).allSame + return check(differences(values)) } } public static func part2(_ input: Input) throws -> Int { - 0 + try input.lines.count { + let values = try $0 + .split(whereSeparator:(\.isWhitespace)) + .map(Int.init) + + if check(differences(values)) { return true } + + return (0.. [Int] { + zip(values.dropFirst(), values).map(-) +} + +fileprivate func check(_ differences: [Int]) -> Bool { + differences.map(abs).allSatisfy { 0 < $0 && $0 < 4 } + && differences.map(\.signum).allSame +} diff --git a/Year2024/Tests/Day02Tests.swift b/Year2024/Tests/Day02Tests.swift index 902d913..0073de9 100644 --- a/Year2024/Tests/Day02Tests.swift +++ b/Year2024/Tests/Day02Tests.swift @@ -22,11 +22,19 @@ final class Day02Tests: XCTestCase { } func testPart2Examples() throws { - XCTAssertEqual(try Day02.part2([]), 0) + XCTAssertEqual(try Day02.part2([ + "7 6 4 2 1", + "1 2 7 8 9", + "9 7 6 2 1", + "1 3 2 4 5", + "8 6 4 4 1", + "1 3 6 7 9", + ]), 4) + } func testPart2Puzzle() throws { let input = try Bundle.module.input(named: "Day02") - XCTAssertEqual(try Day02.part2(input), 0) + XCTAssertEqual(try Day02.part2(input), 354) } }