Skip to content

Instantly share code, notes, and snippets.

@proxpero
Last active December 2, 2018 06:59
Show Gist options
  • Save proxpero/aa1b2d1000755fd3308ffa4444ec8626 to your computer and use it in GitHub Desktop.
Save proxpero/aa1b2d1000755fd3308ffa4444ec8626 to your computer and use it in GitHub Desktop.
Advent of Code 2018, day 1
// prepare the puzzle input
let input = loadFile(named: "day1") // convenience function in resources
.split(separator: "\n")
.compactMap(Int.init) // extension on Int to init? with String.Subsequence
// part 1
let part1 = input.reduce(0, +)
// part 2
// Make an infinite sequence from the input
let frequencies = sequence(state: input.startIndex) { (index) -> Int? in
defer {
index = index.advanced(by: 1)
}
if index == input.endIndex {
index = input.startIndex
}
return input[index]
}
var history = Set<Int>([0])
var current = 0
for frequency in frequencies {
current += frequency
if !history.contains(current) {
history.insert(current)
continue
}
break
}
let part2 = current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment