Browse Source

Just use a sequence not an array

It's about 30% slower, but a lot nicer.
master
Chris Smith 5 years ago
parent
commit
c1820b60c5
1 changed files with 7 additions and 13 deletions
  1. 7
    13
      day14.nim

+ 7
- 13
day14.nim View File

@@ -7,8 +7,7 @@ let
7 7
     inputLen = input.len
8 8
 
9 9
 var
10
-    scores: array[50_000_000, int8]  # Arbitrary large preallocation
11
-    size = 2
10
+    scores = @[3'i8, 7'i8]
12 11
     elf1 = 0
13 12
     elf2 = 1
14 13
     part2 = -1
@@ -18,30 +17,25 @@ proc check(inputSeq: seq[int8], val: int, part2: var int) {.inline.} =
18 17
     if val == inputSeq[found]:
19 18
         found.inc
20 19
         if found == inputLen:
21
-            part2 = size - inputLen
20
+            part2 = scores.len - inputLen
22 21
             found = 0
23 22
     else:
24 23
         found = 0
25 24
 
26
-scores[0] = 3
27
-scores[1] = 7
28
-
29
-while size < inputInt + 10 or part2 == -1:
25
+while scores.len < inputInt + 10 or part2 == -1:
30 26
     var score = scores[elf1] + scores[elf2]
31 27
     
32 28
     if score >= 10:
33
-        scores[size] = 1
34
-        size.inc
29
+        scores.add(1)
35 30
         inputSeq.check(1, part2)
36 31
 
37 32
     let newScore = cast[int8](score mod 10)
38
-    scores[size] = newScore
39
-    size.inc
33
+    scores.add(newScore)
40 34
 
41 35
     inputSeq.check(newScore, part2)
42 36
 
43
-    elf1 = (elf1 + scores[elf1] + 1) mod size
44
-    elf2 = (elf2 + scores[elf2] + 1) mod size
37
+    elf1 = (elf1 + scores[elf1] + 1) mod scores.len
38
+    elf2 = (elf2 + scores[elf2] + 1) mod scores.len
45 39
 
46 40
 echo scores[inputInt..inputInt+9].join
47 41
 echo part2