-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.nim
106 lines (81 loc) · 2.3 KB
/
day23.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# NOTE: if an exception is thrown during execution, stack size has
# to be increased with command 'ulimit -s unlimited'
import sequtils
import strutils
# Types
type
Cup = int
# Input data parser
proc parseInput(inputData: string): (array[9,Cup], int) =
# Parse input
let cups = (@inputData).mapIt(ord(it) - ord('0') - 1)
# Initialize next cup
var nextCup: array[9, Cup]
for i in 0..8:
nextCup[cups[i]] = cups[(i+1) mod 9]
(nextCup, cups[0])
# Move
proc move(cups: var openArray[Cup], current: Cup): Cup =
let l = cups.len
# Pick three clockwise cups
let pickedCups = [
cups[current], cups[cups[current]], cups[cups[cups[current]]]]
cups[current] = cups[cups[cups[cups[current]]]]
# Select destination cup
var destination: Cup
for i in 1..l-1:
destination = (l-1) - (((l-1) - current + i) mod l)
if destination notin pickedCups:
break
# Place clockwise
cups[pickedCups[^1]] = cups[destination]
cups[destination] = pickedCups[0]
# Pick new current
cups[current]
# Print cup ordering
proc order(cups: openArray[Cup]): seq[Cup] =
let l = cups.len
var ordering = newSeq[Cup](l)
ordering[0] = 0
var index = 0
for i in 1..l-1:
index = cups[index]
ordering[i] = index
ordering
# Input data
let inputData = "716892543"
# Parse input data
var (cups, current) = parseInput(inputData)
# Perform 100 moves
for _ in 1..100:
current = cups.move(current)
# Get final cup ordering
var ordering = cups.order()
ordering.delete(0, 0)
# Print results
echo "--- Part 1 Report ---"
echo "Cup ordering = " & ordering.mapIt(intToStr(it + 1)).join()
## Part 2
const Ncups: int = 1000000
# Input data parser
proc parseInputExtended(inputData: string): (array[Ncups,Cup], int) =
# Parse input
let cups = (@inputData).mapIt(ord(it) - ord('0') - 1)
# Initialize next cup
var nextCup: array[Ncups, Cup]
for i in 0..7:
nextCup[cups[i]] = cups[(i+1)]
nextCup[cups[^1]] = 9
for i in 9..Ncups-2:
nextCup[i] = (i+1) mod Ncups
nextCup[^1] = cups[0]
(nextCup, cups[0])
# Parse input data
var cups2: array[Ncups,Cup]
(cups2, current) = parseInputExtended(inputData)
# Perform 10000000 moves
for i in 1..10000000:
current = cups2.move(current)
# Print results
echo "--- Part 2 Report ---"
echo "Clockwise cups product = " & $((cups2[0]+1) * (cups2[cups2[0]]+1))