Created
December 10, 2020 07:13
-
-
Save felix-larsen/75db1e3f87dddb9039705df4811252a4 to your computer and use it in GitHub Desktop.
10th December solution - Advent of Code - swift
This file contains hidden or 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
let countDiff = listOfDifferences.reduce(into: [:]) { counts, joltDiff in counts[joltDiff, default: 0] += 1 } | |
print(countDiff) | |
print((countDiff[1] ?? 0 ) * (countDiff[3] ?? 0 )) |
This file contains hidden or 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment