Browse Source

Minor optimisations

master
Chris Smith 5 years ago
parent
commit
f1e2824734
1 changed files with 7 additions and 7 deletions
  1. 7
    7
      day14.nim

+ 7
- 7
day14.nim View File

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