Browse Source

Slooowwww day 14

master
Chris Smith 5 years ago
parent
commit
b103c400c6
3 changed files with 44 additions and 0 deletions
  1. 2
    0
      answers/14.txt
  2. 1
    0
      data/14.txt
  3. 41
    0
      day14.nim

+ 2
- 0
answers/14.txt View File

@@ -0,0 +1,2 @@
1
+2776141917
2
+20331097

+ 1
- 0
data/14.txt View File

@@ -0,0 +1 @@
1
+598701

+ 41
- 0
day14.nim View File

@@ -0,0 +1,41 @@
1
+import sequtils, strutils
2
+
3
+let
4
+    input = readFile("data/14.txt").strip
5
+    inputSeq = input.map do (c: char) -> int: cast[int](c) - 48
6
+    lastDigit = inputSeq[inputSeq.len - 1]
7
+    inputInt = input.parseInt
8
+
9
+var
10
+    scores = newSeq[int](100_000_000)  # Arbitrary large preallocation
11
+    size = 2
12
+    elf1 = 0
13
+    elf2 = 1
14
+    part2 = -1
15
+
16
+scores[0] = 3
17
+scores[1] = 7
18
+
19
+while size < inputInt + 10 or part2 == -1:
20
+    var score = scores[elf1] + scores[elf2]
21
+    
22
+    if score >= 10:
23
+        let newScore = score div 10
24
+        scores[size] = newScore
25
+        size.inc
26
+
27
+        if newScore == lastDigit and size > input.len and scores[size-input.len..size-1] == inputSeq:
28
+            part2 = size - input.len
29
+
30
+    let newScore = score mod 10
31
+    scores[size] = newScore
32
+    size.inc
33
+
34
+    if newScore == lastDigit and size > input.len and scores[size-input.len..size-1] == inputSeq:
35
+        part2 = size - input.len
36
+
37
+    elf1 = (elf1 + scores[elf1] + 1) mod size
38
+    elf2 = (elf2 + scores[elf2] + 1) mod size
39
+
40
+echo scores[inputInt..inputInt+9].join
41
+echo part2