day 23, part 2

This commit is contained in:
Sven Weidauer 2020-12-24 10:33:29 +01:00
parent 35353b8641
commit 241f983584

View file

@ -1,4 +1,6 @@
class Node { var nodesByValue: [Node?] = Array(repeating: nil, count: 1000001)
final class Node {
var next: Node! var next: Node!
var value: Int var value: Int
@ -11,11 +13,20 @@ class Node {
precondition(!values.isEmpty) precondition(!values.isEmpty)
let first = Node(value: values[0]) let first = Node(value: values[0])
nodesByValue[values[0]] = first
first.next = first first.next = first
var current = first var current = first
for value in values.dropFirst() { for value in values.dropFirst() {
let new = Node(value: value, next: first) let new = Node(value: value, next: first)
nodesByValue[value] = new
current.next = new
current = new
}
for value in 10...1000000 {
let new = Node(value: value, next: first)
nodesByValue[value] = new
current.next = new current.next = new
current = new current = new
} }
@ -24,20 +35,13 @@ class Node {
} }
func find(value: Int) -> Node? { func find(value: Int) -> Node? {
var current = self return nodesByValue[value]
repeat {
if current.value == value {
return current
}
current = current.next
} while current !== self
return nil
} }
} }
let input = [9, 5, 2, 3, 1, 6, 4, 8, 7] let input = [9, 5, 2, 3, 1, 6, 4, 8, 7]
let max = input.max()! let max = 1000000
//let max = input.max()!
let node = Node.make(input) let node = Node.make(input)
var selected = node var selected = node
@ -94,12 +98,15 @@ func showSolution() {
} }
for i in 0..<100 { for i in 0..<10_000_000 {
print("\(i + 1)) ", terminator: "") // print("\(i + 1)) ", terminator: "")
printStack() // printStack()
round() round()
} }
print("Final: ", terminator: "") //print("Final: ", terminator: "")
printStack() //printStack()
showSolution() //showSolution()
let one = node.find(value: 1)!
print(one.next.value * one.next.next.value)