Skip to content

Commit

Permalink
2023 day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
davedelong committed Dec 9, 2023
1 parent a29ec60 commit 6665b46
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"location" : "[email protected]:davedelong/ExtendedSwift.git",
"state" : {
"branch" : "main",
"revision" : "069c78bc9e6f8d54afcb88cc577908c8902912dd"
"revision" : "ee59b1b4aaaf8ddfc4d3fd03a28f80b7e7c67e0f"
}
},
{
Expand Down
45 changes: 33 additions & 12 deletions Sources/AOC2023/Day 9/2023-Day9.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,44 @@
//

struct Day9: Day {
typealias Part1 = String
typealias Part2 = String
typealias Part1 = Int
typealias Part2 = Int

static var rawInput: String? { nil }

func run() async throws -> (Part1, Part2) {
let p1 = try await part1()
let p2 = try await part2()

let sequences = input().lines.map(\.integers)

var p1 = 0
var p2 = 0

for sequence in sequences {
var derivitives = [sequence]
var current = sequence
while derivitives.last!.contains(where: { $0 != 0 }) {
let diff = current.adjacentPairs().map { $1 - $0 }
derivitives.append(diff)
current = diff
}

// we've derived the zero sequence
var p1Extrapolation = 0
var p2Extrapolation = 0

for index in derivitives.indices.reversed() {
let nextP1 = derivitives[index].last! + p1Extrapolation
let nextP2 = derivitives[index].first! - p2Extrapolation

p1Extrapolation = nextP1
p2Extrapolation = nextP2
}

p1 += p1Extrapolation
p2 += p2Extrapolation
}

return (p1, p2)
}

func part1() async throws -> Part1 {
return #function
}

func part2() async throws -> Part2 {
return #function
}

}
Loading

0 comments on commit 6665b46

Please sign in to comment.