Skip to content

Instantly share code, notes, and snippets.

@z3bi
Created December 22, 2022 21:38
Show Gist options
  • Save z3bi/61129422c9d2d768fc712f3b2c202417 to your computer and use it in GitHub Desktop.
Save z3bi/61129422c9d2d768fc712f3b2c202417 to your computer and use it in GitHub Desktop.
import UIKit
var input =
"""
1
2
-3
3
-2
0
4
"""
let lines = input.split(whereSeparator: \.isNewline)
struct Point: Equatable, Hashable {
let value: Int
var hasMoved = false
}
var points: [Point] = lines.map {
let val = Int(String($0))!
return Point(value: val, hasMoved: val == 0)
}
while points.contains(where: { $0.hasMoved == false }) {
let index = points.firstIndex(where: { $0.hasMoved == false })!
var point = points.remove(at: index)
point.hasMoved = true
var newIndex = index + point.value
newIndex = newIndex % points.count
if newIndex < 0 {
newIndex = points.count + newIndex
}
if newIndex == 0 {
newIndex = points.count
}
points.insert(point, at: newIndex)
}
let values = points.map { $0.value }
let zeroPos = values.firstIndex(of: 0)!
let output = values[(1000 + zeroPos) % values.count] + values[(2000 + zeroPos) % values.count] + values[(3000 + zeroPos) % values.count]
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment