Skip to content

Commit 6acaffc

Browse files
committed
2017.25: turing machine. map based.
1 parent 0d5cd87 commit 6acaffc

File tree

6 files changed

+271
-1
lines changed

6 files changed

+271
-1
lines changed

25_turing_machine.go

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"metalim/advent/2017/lib/source"
6+
"strconv"
7+
8+
. "github.com/logrusorgru/aurora"
9+
)
10+
11+
var test1 = `Begin in state A.
12+
Perform a diagnostic checksum after 6 steps.
13+
14+
In state A:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the right.
18+
- Continue with state B.
19+
If the current value is 1:
20+
- Write the value 0.
21+
- Move one slot to the left.
22+
- Continue with state B.
23+
24+
In state B:
25+
If the current value is 0:
26+
- Write the value 1.
27+
- Move one slot to the left.
28+
- Continue with state A.
29+
If the current value is 1:
30+
- Write the value 1.
31+
- Move one slot to the right.
32+
- Continue with state A.`
33+
34+
func main() {
35+
var ins source.Inputs
36+
37+
ins = ins.Test(1, test1, `3`)
38+
39+
for par := range ins.Advent(2017, 25) {
40+
fmt.Println(Brown("\n" + par.Name))
41+
ssw := par.Lines().WordsTrim(":.")
42+
fmt.Println(len(ssw), Black(ssw[0]).Bold())
43+
44+
state := ssw[0][len(ssw[0])-1]
45+
n, _ := strconv.Atoi(ssw[1][len(ssw[1])-2])
46+
ssw = ssw[2:]
47+
48+
rules := map[string][2]cmd{}
49+
for len(ssw) > 0 {
50+
ins := ssw[1][len(ssw[1])-1]
51+
52+
v1, _ := strconv.Atoi(ssw[3][len(ssw[3])-1])
53+
l1 := ssw[4][len(ssw[4])-1] == "left"
54+
st1 := ssw[5][len(ssw[5])-1]
55+
56+
v2, _ := strconv.Atoi(ssw[7][len(ssw[7])-1])
57+
l2 := ssw[8][len(ssw[8])-1] == "left"
58+
st2 := ssw[9][len(ssw[9])-1]
59+
60+
rules[ins] = [2]cmd{{v1, l1, st1}, {v2, l2, st2}}
61+
ssw = ssw[10:]
62+
}
63+
fmt.Println(string(state), n)
64+
65+
tape := map[int]int{}
66+
var p int
67+
68+
for ; n > 0; n-- {
69+
c := rules[state][tape[p]]
70+
tape[p] = c.v
71+
if c.l {
72+
p--
73+
} else {
74+
p++
75+
}
76+
state = c.st
77+
}
78+
var sum int
79+
for _, v := range tape {
80+
if v == 1 {
81+
sum++
82+
}
83+
}
84+
par.SubmitInt(1, sum)
85+
86+
if par.Part(2) {
87+
par.SubmitInt(2, 0)
88+
}
89+
}
90+
}
91+
92+
////////////////////////////////////////////////////////////////////////
93+
94+
type cmd struct {
95+
v int
96+
l bool
97+
st string
98+
}
99+
100+
////////////////////////////////////////////////////////////////////////
101+
102+
type tape struct {
103+
s [2][]bool
104+
p int
105+
}
106+
107+
func (t *tape) get() bool {
108+
i := 0
109+
p := t.p
110+
if p < 0 {
111+
p = -p - 1
112+
i = 1
113+
}
114+
if p < len(t.s[i]) {
115+
return t.s[i][p]
116+
}
117+
return false
118+
}
119+
120+
func (t *tape) set(v bool) {
121+
i := 0
122+
p := t.p
123+
if p < 0 {
124+
p = -p - 1
125+
i = 1
126+
}
127+
if p < len(t.s[i]) {
128+
t.s[i] = append(t.s[i], make([]bool, p)...) // extra space
129+
}
130+
t.s[i][p] = v
131+
}
132+
133+
func (t *tape) left() {
134+
t.p--
135+
}
136+
137+
func (t *tape) right() {
138+
t.p++
139+
}

inputs/2017_25_github.txt

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 12208951 steps.
3+
4+
In state A:
5+
If the current value is 0:
6+
- Write the value 1.
7+
- Move one slot to the right.
8+
- Continue with state B.
9+
If the current value is 1:
10+
- Write the value 0.
11+
- Move one slot to the left.
12+
- Continue with state E.
13+
14+
In state B:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the left.
18+
- Continue with state C.
19+
If the current value is 1:
20+
- Write the value 0.
21+
- Move one slot to the right.
22+
- Continue with state A.
23+
24+
In state C:
25+
If the current value is 0:
26+
- Write the value 1.
27+
- Move one slot to the left.
28+
- Continue with state D.
29+
If the current value is 1:
30+
- Write the value 0.
31+
- Move one slot to the right.
32+
- Continue with state C.
33+
34+
In state D:
35+
If the current value is 0:
36+
- Write the value 1.
37+
- Move one slot to the left.
38+
- Continue with state E.
39+
If the current value is 1:
40+
- Write the value 0.
41+
- Move one slot to the left.
42+
- Continue with state F.
43+
44+
In state E:
45+
If the current value is 0:
46+
- Write the value 1.
47+
- Move one slot to the left.
48+
- Continue with state A.
49+
If the current value is 1:
50+
- Write the value 1.
51+
- Move one slot to the left.
52+
- Continue with state C.
53+
54+
In state F:
55+
If the current value is 0:
56+
- Write the value 1.
57+
- Move one slot to the left.
58+
- Continue with state E.
59+
If the current value is 1:
60+
- Write the value 1.
61+
- Move one slot to the right.
62+
- Continue with state A.

inputs/2017_25_google.txt

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 12481997 steps.
3+
4+
In state A:
5+
If the current value is 0:
6+
- Write the value 1.
7+
- Move one slot to the right.
8+
- Continue with state B.
9+
If the current value is 1:
10+
- Write the value 0.
11+
- Move one slot to the left.
12+
- Continue with state C.
13+
14+
In state B:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the left.
18+
- Continue with state A.
19+
If the current value is 1:
20+
- Write the value 1.
21+
- Move one slot to the right.
22+
- Continue with state D.
23+
24+
In state C:
25+
If the current value is 0:
26+
- Write the value 0.
27+
- Move one slot to the left.
28+
- Continue with state B.
29+
If the current value is 1:
30+
- Write the value 0.
31+
- Move one slot to the left.
32+
- Continue with state E.
33+
34+
In state D:
35+
If the current value is 0:
36+
- Write the value 1.
37+
- Move one slot to the right.
38+
- Continue with state A.
39+
If the current value is 1:
40+
- Write the value 0.
41+
- Move one slot to the right.
42+
- Continue with state B.
43+
44+
In state E:
45+
If the current value is 0:
46+
- Write the value 1.
47+
- Move one slot to the left.
48+
- Continue with state F.
49+
If the current value is 1:
50+
- Write the value 1.
51+
- Move one slot to the left.
52+
- Continue with state C.
53+
54+
In state F:
55+
If the current value is 0:
56+
- Write the value 1.
57+
- Move one slot to the right.
58+
- Continue with state D.
59+
If the current value is 1:
60+
- Write the value 1.
61+
- Move one slot to the right.
62+
- Continue with state A.

lib/source/advent.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func trySubmit(name string, year, day, part int, v string) {
245245
buf, err := ioutil.ReadAll(resp.Body)
246246
check(err)
247247
html := string(buf)
248-
reg := regexp.MustCompile("(?s)<main>\\s*<article><p>(.*)</p></article>\\s*</main>")
248+
reg := regexp.MustCompile("(?s)<main>\\s*<article>\\s*<p>(.*)</p>\\s*</article>\\s*</main>")
249249
m := reg.FindStringSubmatch(html)
250250
main := html
251251
if len(m) > 1 {
@@ -285,6 +285,11 @@ func trySubmit(name string, year, day, part int, v string) {
285285
return
286286
}
287287

288+
if strings.Contains(main, "Congratulations! You've finished every puzzle") {
289+
fmt.Println(Green("Congratulations! You've finished every puzzle in this year."))
290+
ioutil.WriteFile(outkey, []byte(v), 600)
291+
return
292+
}
288293
// some unknown response.
289294
ioutil.WriteFile(outkey+".err.txt", []byte(main), 600)
290295
fmt.Println("main:", main)

results/2017_25_1_github.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4387

results/2017_25_2_github.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

0 commit comments

Comments
 (0)