Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Last active December 23, 2020 11:51
Show Gist options
  • Save felix-larsen/231ea733254117fd135c1c9ef5cd8610 to your computer and use it in GitHub Desktop.
Save felix-larsen/231ea733254117fd135c1c9ef5cd8610 to your computer and use it in GitHub Desktop.
23rd December solution - Advent of code - swift
var currentCup = cups.first!
for _ in 1...100 {
(currentCup,cups) = runMove(currentCup, cups)
}
let parts = cups.split(separator: 1)
var result = [Int]()
if parts.count > 1 {
result = Array(parts[1] + parts[0])
} else {
result = Array(parts[0])
}
print(result.map {String($0)}.joined())
func runMove(_ currentCup: Int, _ pCups: [Int]) -> (Int,[Int]) {
var cups = pCups
let index = cups.firstIndex(of: currentCup)!
var pickUp = [Int]()
var cupIndex = cups.startIndex
if index < cups.endIndex - 1 {
cupIndex = cups.index(index, offsetBy: 1)
}
for _ in 1...3 {
let cup = cups.remove(at: cupIndex)
pickUp.append(cup)
if cupIndex >= cups.count {
cupIndex = 0
}
}
let nextCup = cups[cupIndex]
var destination = currentCup - 1
var destinationIndex = cups.firstIndex(of: destination)
while destinationIndex == nil {
destination -= 1
if destination < cups.min()! {
destination = cups.max()!
}
destinationIndex = cups.firstIndex(of: destination)
}
var insertIndex = cups.startIndex
if destinationIndex! < cups.endIndex - 1 {
insertIndex = cups.index(destinationIndex!, offsetBy: 1)
}
cups.insert(contentsOf: pickUp, at: insertIndex)
return (nextCup, cups)
}
class Node {
let value: Int
var next: Node?
init(value: Int, next: Node? = nil) {
self.value = value
self.next = next
}
}
var cupsLinkedList = [Int: Node]()
let maxValue = 1_000_000
cups.append(contentsOf: Array(10...maxValue) as [Int])
cups.forEach { cup in
cupsLinkedList[cup] = Node(value: cup)
}
cups.enumerated().forEach { (i,cup) in
if i == cups.count - 1 {
cupsLinkedList[cup]?.next = cupsLinkedList[cups[0]]
} else {
cupsLinkedList[cup]?.next = cupsLinkedList[cups[i+1]]
}
}
var currentNode = cupsLinkedList[cups[0]]!
var node = cupsLinkedList[cups[0]]!
var number: String = ""
for _ in 0..<cupsLinkedList.count {
number += "\(node.value)"
node = node.next!
}
print(number)
for i in 1...10_000_000 {
(currentNode) = runMove(currentNode)
}
let node1 = cupsLinkedList[1]!.next!
let node2 = node1.next!
print(node1.value * node2.value)
func runMove(_ currentCup: Node) -> Node {
var iterator: Node = currentCup.next!
var pickUp = [Node]()
for _ in 1...3 {
pickUp.append(iterator)
iterator = iterator.next!
}
let nextCup = iterator
var destination = currentCup.value - 1
var destinationNode: Node? = nil
while destinationNode == nil {
if pickUp.contains(where: { $0.value == destination }) {
if destination == 1 {
destination = maxValue
} else {
destination -= 1
}
continue
} else if destination == 0 {
destination = maxValue
}
destinationNode = cupsLinkedList[destination]
}
let afterNode = destinationNode!.next
destinationNode!.next = pickUp.first!
pickUp.last!.next = afterNode
currentCup.next = nextCup
return nextCup
}
let labeling = "327465189"
var cups = labeling.map { Int(String($0))! }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment