Skip to content

Commit

Permalink
Add 2024, day 1, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed Dec 1, 2024
1 parent 1b85ef3 commit 662e816
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Year2024/Sources/Day01.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ public enum Day01: Day {
}

public static func part2(_ input: Input) throws -> Int {
0
let rows = try input.lines
.map {
let values = $0.split(whereSeparator:(\.isWhitespace))
return try (Int(values.first.unwrapped), Int(values.last.unwrapped))
}
let columns = rotate(rows)
let lhs = columns.0
let rhs = columns.1
return lhs.map { $0 * rhs.count(of: $0) }.sum
}
}

Expand Down
11 changes: 9 additions & 2 deletions Year2024/Tests/Day01Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ final class Day01Tests: XCTestCase {
}

func testPart2Examples() throws {
XCTAssertEqual(try Day01.part2([]), 0)
XCTAssertEqual(try Day01.part2([
"3 4",
"4 3",
"2 5",
"1 3",
"3 9",
"3 3",
]), 31)
}

func testPart2Puzzle() throws {
let input = try Bundle.module.input(named: "Day01")
XCTAssertEqual(try Day01.part2(input), 0)
XCTAssertEqual(try Day01.part2(input), 23741109)
}
}

0 comments on commit 662e816

Please sign in to comment.