Last active
February 24, 2019 01:05
-
-
Save cassdeckard/16c21d251c7da47a3c2958174b86c90d to your computer and use it in GitHub Desktop.
AOC 2018 solutions
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
| import Foundation | |
| let data = """ | |
| -1 | |
| 1 | |
| """ | |
| var visitedFrequencies = Set<Int>() | |
| let parsedData = data.components(separatedBy: "\n").map{ Int($0)! } | |
| var sum = 0 | |
| var done = false | |
| repeat { | |
| sum = parsedData.reduce(sum) { (last, next) in | |
| let result = last + next | |
| if visitedFrequencies.contains(result) { | |
| print(result) | |
| done = true | |
| } | |
| visitedFrequencies.insert(result) | |
| return result | |
| } | |
| } while !done | |
| print("-====-") | |
| print(sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment