-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent2017_25.py
48 lines (37 loc) · 1.15 KB
/
advent2017_25.py
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# http://adventofcode.com/2017
# solution copied from https://github.com/norvig/pytudes/blob/master/ipynb/Advent%202017.ipynb
import math
import itertools
from functools import lru_cache
from collections import Counter, defaultdict
from pprint import pprint
import csv
import logging as log
import os
import pytest
import queue
LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper()
log.basicConfig(level=LOGLEVEL)
def machine():
"machine()[state][value] == (new_value, move, new_state)}"
L, R = -1, +1
A, B, C, D, E, F = "ABCDEF"
return {
A: [(1, R, B), (0, R, F)],
B: [(0, L, B), (1, L, C)],
C: [(1, L, D), (0, R, C)],
D: [(1, L, E), (1, R, A)],
E: [(1, L, F), (0, L, D)],
F: [(1, R, A), (0, L, E)],
}
def turing(machine, state, steps):
"Run the Turing machine for given number of steps, then return tape."
tape = defaultdict(int)
cursor = 0
for step in range(steps):
tape[cursor], move, state = machine[state][tape[cursor]]
cursor += move
return tape
if __name__ == "__main__":
print(sum(turing(machine(), "A", 12425180).values()))