Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created December 10, 2020 07:13
Show Gist options
  • Save felix-larsen/75db1e3f87dddb9039705df4811252a4 to your computer and use it in GitHub Desktop.
Save felix-larsen/75db1e3f87dddb9039705df4811252a4 to your computer and use it in GitHub Desktop.
10th December solution - Advent of Code - swift
let countDiff = listOfDifferences.reduce(into: [:]) { counts, joltDiff in counts[joltDiff, default: 0] += 1 }
print(countDiff)
print((countDiff[1] ?? 0 ) * (countDiff[3] ?? 0 ))
let arrangementCountForConsecutiveOnes =
[2 : 2, // 2 consecutive ones have 2 possible arrangements
3 : 4, // 3 consecutive ones have 4 possible arrangements
4 : 7] // 4 consecutive ones have 7 possible arrangements
var consectuiveOnesCount = [Int:Int]()
var consecutiveOnes = 0
listOfDifferences.forEach { (diff) in
if diff == 1{
consecutiveOnes += 1
} else {
if consecutiveOnes > 1 {
consectuiveOnesCount[consecutiveOnes, default: 0] += 1
}
consecutiveOnes = 0
}
}
let arragmentsCount = pow(Decimal(arrangementCountForConsecutiveOnes[2] ?? 0),consectuiveOnesCount[2] ?? 0)
* pow(Decimal(arrangementCountForConsecutiveOnes[3] ?? 0),consectuiveOnesCount[3] ?? 0)
* pow(Decimal(arrangementCountForConsecutiveOnes[4] ?? 0),consectuiveOnesCount[4] ?? 0)
print(arragmentsCount)
let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december10.txt"
let contents = try! String(contentsOfFile: filename)
let lines = contents.components(separatedBy: CharacterSet.newlines)
let numbers = lines.compactMap { Int($0) }
let targetJolts = numbers.max()! + 3
var sortedNumbers = numbers.sorted()
sortedNumbers.append(targetJolts)
var listOfDifferences = [Int]()
sortedNumbers.reduce(0) { (lastJolt, jolt) -> Int in
listOfDifferences.append(jolt - lastJolt)
return jolt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment