Skip to content

Commit

Permalink
day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
gereons committed Dec 6, 2023
1 parent 85a75bb commit 264e491
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
37 changes: 33 additions & 4 deletions Sources/Day06/Day06.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,46 @@

import AoCTools

private struct Race {
let time: Int
let distance: Int

func waysToWin() -> Int {
var win = 0
for press in 1 ..< time {
let distance = press * (time - press)
if distance > self.distance {
win += 1
}
}
return win
}
}

final class Day06: AOCDay {

private let races: [Race]
private let part2Race: Race

init(input: String) {
// let input = input ?? Self.input
let lines = input.lines

let times = lines[0].allInts()
let distances = lines[1].allInts()

races = zip(times, distances).map { Race(time: $0, distance: $1) }

let totalTime = lines[0].replacingOccurrences(of: " ", with: "").allInts()[0]
let totalDistance = lines[1].replacingOccurrences(of: " ", with: "").allInts()[0]
part2Race = Race(time: totalTime, distance: totalDistance)
}

func part1() -> Int {
return 0
races
.map { $0.waysToWin() }
.reduce(1, *)
}

func part2() -> Int {
return 0
part2Race.waysToWin()
}
}
10 changes: 6 additions & 4 deletions Tests/Day06Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import XCTest

final class Day06Tests: XCTestCase {
let testInput = """
Time: 7 15 30
Distance: 9 40 200
"""

func testDay06_part1() throws {
let day = Day06(input: testInput)
XCTAssertEqual(day.part1(), 0)
XCTAssertEqual(day.part1(), 288)
}

func testDay06_part1_solution() throws {
let day = Day06(input: Day06.input)
XCTAssertEqual(day.part1(), 0)
XCTAssertEqual(day.part1(), 1710720)
}

func testDay06_part2() throws {
let day = Day06(input: testInput)
XCTAssertEqual(day.part2(), 0)
XCTAssertEqual(day.part2(), 71503)
}

func testDay06_part2_solution() throws {
let day = Day06(input: Day06.input)
XCTAssertEqual(day.part2(), 0)
XCTAssertEqual(day.part2(), 35349468)
}
}

0 comments on commit 264e491

Please sign in to comment.