Browse Source

Unsatisfying day 12.

master
Chris Smith 5 years ago
parent
commit
50a54c8a56
3 changed files with 103 additions and 0 deletions
  1. 2
    0
      answers/12.txt
  2. 34
    0
      data/12.txt
  3. 67
    0
      day12.nim

+ 2
- 0
answers/12.txt View File

@@ -0,0 +1,2 @@
1
+2823
2
+2900000001856

+ 34
- 0
data/12.txt View File

@@ -0,0 +1,34 @@
1
+initial state: .##..##..####..#.#.#.###....#...#..#.#.#..#...#....##.#.#.#.#.#..######.##....##.###....##..#.####.#
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
+..##. => .

+ 67
- 0
day12.nim View File

@@ -0,0 +1,67 @@
1
+import math, sequtils, strscans, strutils, tables
2
+
3
+type
4
+    State = tuple[start: int, plants: string]
5
+
6
+const
7
+    iterations = 50_000_000_000
8
+
9
+func score(state: State): int =
10
+    for i, c in state.plants:
11
+        if c == '#':
12
+            result += i + state.start
13
+
14
+
15
+func iterate(state: State, rules: ref Table[string, char]): State =
16
+    let
17
+        input = "...." & state.plants & "...."
18
+        start = state.start - 2
19
+
20
+    var
21
+        output = ""
22
+        left = -1
23
+        
24
+    for i in 0..input.len - 5:
25
+        let val = rules[input.substr(i, i + 4)]
26
+        output &= val
27
+        if left == -1 and val == '#':
28
+            left = i - 1
29
+
30
+    (start + left, output.substr(left).strip(leading=false, chars={'.'}))
31
+
32
+
33
+proc solve(initialState: State, rules: ref Table[string, char]) =
34
+    var state = initialState
35
+
36
+    for i in 1..iterations:
37
+        var nextState = iterate(state, rules)
38
+
39
+        if i == 20:
40
+            echo nextState.score
41
+
42
+        if state.plants == nextState.plants:
43
+            # Same pattern of plants, they're just wandering off somewhere.
44
+            let
45
+                difference = nextState.score - state.score
46
+                remaining = iterations - i
47
+                score = nextState.score + difference * remaining
48
+            echo score
49
+            return
50
+
51
+        state = nextState
52
+
53
+    # If we reach here then the pattern didn't repeat, and it's
54
+    # probably taken an *awfully* long time.
55
+    echo state.score
56
+
57
+
58
+var
59
+    rules = newTable[string, char]()
60
+    lines = readFile("data/12.txt").strip.splitlines
61
+    initialState: State = (0, lines[0].strip.substr(15))
62
+
63
+for line in lines[2..lines.len-1]:
64
+    if line.strip.len > 0:
65
+        rules.add(line.substr(0,4), line.substr(9)[0])
66
+
67
+solve(initialState, rules)