Created
December 10, 2020 14:15
-
-
Save karlis/4d155899354173a1e86a226f4bd37d0f to your computer and use it in GitHub Desktop.
Advent2020Day10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Day10 { | |
var adapterTypes: [Bool] | |
public init(input: String) { | |
var parsed = Self.parse(input) | |
parsed.append(0) | |
adapterTypes = parsed | |
.sorted() | |
.slidingWindows(ofCount: 2) // import swift-algorithms | |
.map { ($0.first! + 1 == $0.last!) } | |
} | |
static func parse(_ input: String) -> [Int] { | |
input | |
.split(whereSeparator: \.isNewline) | |
.map { Int(String($0))! } | |
} | |
// MARK: - Results | |
public var solve1: Int { | |
let ones = adapterTypes.filter { $0 }.count | |
return (adapterTypes.count - ones) * ones | |
} | |
public var solve2: Int { | |
adapterTypes | |
.split(separator: false) | |
.map { $0.count } | |
.map { | |
if $0 == 2 { return 2 } | |
if $0 == 3 { return 4 } | |
if $0 == 4 { return 7 } | |
return 1 | |
} | |
.reduce(1, *) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment