Created
December 9, 2020 05:56
-
-
Save felix-larsen/7a67749983ef0b5c26a5d5f8ade7a919 to your computer and use it in GitHub Desktop.
9th December solution - Advent of Code - swift
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
let preamble = 25 | |
var solution1 = -1 | |
for i in 0..<numbers.count { | |
if !checkThatAnyNumbers(in: Array(numbers[i..<preamble + i]), sumTo: numbers[preamble + i]) { | |
solution1 = numbers[preamble + i] | |
break | |
} | |
} | |
print(solution1) | |
func checkThatAnyNumbers(in numbers: [Int], sumTo sum: Int) -> Bool{ | |
for indexNum1 in 0..<numbers.count { | |
for indexNum2 in (indexNum1 + 1)..<numbers.count { | |
if numbers[indexNum1] + numbers[indexNum2] == sum { | |
return true | |
} | |
} | |
} | |
return false | |
} |
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
let newSum = solution1 | |
var solution2 = [Int]() | |
outerLoop: for i in 0..<numbers.count { | |
innerLoop: for i2 in (i + 1)..<numbers.count { | |
let sum = numbers[i...i2].reduce(0, +) | |
if sum > newSum { | |
break innerLoop | |
} else if sum == newSum { | |
solution2 = Array(numbers[i...i2]) | |
break outerLoop | |
} | |
} | |
} | |
print(solution2.min()! + solution2.max()!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment