Browse Source

Day 7 performance improvements

- Skip over times when nothing is going to happen
- Instead of storing time remaining and decrementing it each cycle,
  store the end time instead
master
Chris Smith 5 years ago
parent
commit
d08c31c992
1 changed files with 16 additions and 14 deletions
  1. 16
    14
      day07.nim

+ 16
- 14
day07.nim View File

@@ -30,31 +30,33 @@ func part2(dependencies: array[26, array[27, bool]]): int =
30 30
         task_times: array[26, int]
31 31
         completed = ""
32 32
         time = 0
33
+        step = 0
33 34
 
34 35
     status.deepCopy(dependencies)
35 36
 
36 37
     while len(completed) < 26:
37
-        time += 1
38
+        time += step
39
+        step = int.high
40
+
38 41
         for i in 0..25:
39
-            if task_times[i] == 1:
40
-                task_times[i] = 0
41
-                completed &= chr(i + 65)
42
-                status.addr.execute(i)
43
-            elif task_times[i] > 1:
44
-                task_times[i] -= 1
42
+            if task_times[i] > 0: 
43
+                if task_times[i] <= time:
44
+                    task_times[i] = 0
45
+                    completed &= chr(i + 65)
46
+                    status.addr.execute(i)
47
+                else:
48
+                    step = min(step, task_times[i] - time)
45 49
 
46 50
         for i in 0..4:
47
-            if worker_times[i] > 0:
48
-                worker_times[i] -= 1
49
-            
50
-            if worker_times[i] == 0:
51
+            if worker_times[i] <= time:
51 52
                 var task = status.next_task
52 53
                 if task > -1:
53
-                    worker_times[i] = 61 + task
54
-                    task_times[task] = 61 + task
54
+                    worker_times[i] = time + 61 + task
55
+                    task_times[task] = time + 61 + task
55 56
                     status[task][0] = true
57
+                    step = min(step, 61 + task)
56 58
 
57
-    time - 1
59
+    time
58 60
 
59 61
 echo dependencies.part1
60 62
 echo dependencies.part2