-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConway.lisp
187 lines (162 loc) · 9.34 KB
/
Conway.lisp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(in-package "ACL2")
(include-book "avl-rational-keys" :dir :teachpacks)
(include-book "io-utilities" :dir :teachpacks)
(include-book "io-utilities-ex" :dir :teachpacks)
(set-state-ok t)
(defun stdin->string (state)
(mv-let (chli error state)
(let ((channel *standard-ci*))
(if (null channel)
(mv nil
"Error while opening stdin for input"
state)
(mv-let (chlist chnl state)
(read-n-chars 4000000000 '() channel state)
(let ((state (close-input-channel chnl state)))
(mv chlist nil state)))))
(mv (reverse (chrs->str chli)) error state)))
(defun string-list->stdout (strli state)
(let ((channel *standard-co*))
(if (null channel)
(mv "Error while opening stdout"
state)
(mv-let (channel state)
(write-all-strings strli channel state)
(let ((state (close-output-channel channel state)))
(mv nil state))))))
(defun stdin->lines (state)
(mv-let (chnl state)
(mv *standard-ci* state)
(if (null chnl)
(mv "Error opening standard input" state)
(mv-let (c1 state) ; get one char ahead
(read-char$ chnl state)
(mv-let (rv-lines state) ; get line, record backwards
(rd-lines 4000000000 c1 nil chnl state)
(let* ((state (close-input-channel chnl state)))
(mv (reverse rv-lines) state))))))); rev, deliver
;added width input
(defun get-avl-key (x y width)
(+ (* width y) x))
;added x y width input
(defun input-lines->avl-tree (x y width input-lines last-gen)
(if (and (integerp x)
(integerp y)
(integerp width)
(tree? last-gen)
(consp input-lines))
(if (endp (car input-lines))
(input-lines->avl-tree 0 (1+ y) width (cdr input-lines) last-gen)
(input-lines->avl-tree (1+ x) y width (cons (cdar input-lines) (cdr input-lines))
(if (equal (car (car input-lines)) #\x)
(avl-insert last-gen (get-avl-key x y width) 0)
last-gen)))
last-gen))
(defun strings->char-lists (lst)
(if (endp lst)
nil
(cons (coerce (car lst) 'list) (strings->char-lists (cdr lst)))))
(defun stdin->avl-tree (state)
(mv-let (input-lines state)
(stdin->lines state)
(let* ((height (len input-lines))
(width (length (car input-lines))))
(mv (list (input-lines->avl-tree 0 0 width (strings->char-lists input-lines) (empty-tree)) width height) state))))
;(mv (input-lines->avl-tree 0 0 width (strings->char-lists input-lines) (empty-tree)) width height state))))
;added n counter
(defun num-live-neighbors (n width height x y last-gen)
(if (or (= n 0) (not (natp n))) ; this should be just (if (= n 0)), but this way makes ACL2 happy
0
(if (= n 5)
(+ 0 (num-live-neighbors (1- n) width height x y last-gen))
(let* ((i (mod (+ (1- x) (floor (1- n) 3)) width))
(j (mod (+ (1- y) (mod (1- n) 3)) height))
(key (get-avl-key i j width))
(live (if (null (avl-retrieve last-gen key)) 0 1)))
(+ live (num-live-neighbors (1- n) width height x y last-gen))))))
(defun build-next-generation-cell (width height x y last-gen next-gen)
(let* ((n (num-live-neighbors 9 width height x y last-gen))
(result (if (null (avl-retrieve last-gen (get-avl-key x y width)))
(if (= n 3) "x" nil)
(if (or (< n 2) (> n 3)) nil "x"))))
(if result
(avl-insert next-gen (get-avl-key x y width) result)
next-gen)))
; added curx counter
(defun build-next-generation-row (width height curx y last-gen next-gen)
(let* ((new-tree (build-next-generation-cell width height curx y last-gen next-gen))
(next-x (1- curx)))
(if (or (= curx 0) (not (natp curx))) ; should just be (if (= curx 0), but this way makes ACL2 admit the function
new-tree
(build-next-generation-row width height next-x y last-gen new-tree))))
(defun build-next-generation (width height cury last-gen next-gen)
(let* ((new-tree (build-next-generation-row width height (- width 1) cury last-gen next-gen))
(next-y (1- cury)))
(if (or (= cury 0) (not (natp cury)))
new-tree
(build-next-generation width height next-y last-gen new-tree))))
#| This function only exists because Proof Pad is buggy and completely breaks
if we use any string with a semicolon in it. So we have to hack around
the Proof Pad bug in ACL2 using this function. |#
(defun tablecell (live)
(let* ((semicolon (coerce (list (code-char 59)) 'string))
(cell-part2 (string-append (string-append " " semicolon) "</td>")))
(if live
(string-append "<td class='live'>" cell-part2)
(string-append "<td>" cell-part2))
))
(defun avl-tree->output-line (width curx y next-gen)
(if (and (> curx 0) (integerp curx))
;(string-append (if (avl-retrieve next-gen (get-avl-key (- width curx) y width)) "<td class='live'> </td>" "<td> </td>")
(string-append (tablecell (avl-retrieve next-gen (get-avl-key (- width curx) y width))) ;"<td class='live'> </td>" "<td> </td>")
(avl-tree->output-line width (1- curx) y next-gen))
nil))
(defun avl-tree->output-lines (width height cury next-gen)
(if (and (> cury 0) (integerp cury))
(cons (string-append "<tr>" (string-append (avl-tree->output-line width width (- height cury) next-gen) "</tr>"))
(avl-tree->output-lines width height (1- cury) next-gen))
nil))
;test input
#|(num-live-neighbors 0 4 4 1 1
(input-lines->avl-tree 0 0 4 (list (coerce "x xx" 'list)
(coerce "xxxx" 'list)
(coerce "x x" 'list)
(coerce " x" 'list))
(empty-tree)))
; test input 2
(build-next-generation-row 4 4 0 0 (input-lines->avl-tree 0 0 4 (list (coerce "x xx" 'list)
(coerce "xxxx" 'list)
(coerce "x x" 'list)
(coerce " x" 'list))
(empty-tree))
(empty-tree))
; test input 3
(avl-tree->output-line 4 0 0 (build-next-generation-row 4 4 0 0 (input-lines->avl-tree 0 0 4 (list (coerce "x xx" 'list)
(coerce "xxxx" 'list)
(coerce "x x" 'list)
(coerce " x" 'list))
(empty-tree))
(empty-tree)))
; test input 4
(avl-tree->output-lines 4 4 0 (build-next-generation 4 4 3 (input-lines->avl-tree 0 0 4 (list (coerce "x xx" 'list)
(coerce "xxxx" 'list)
(coerce "x x" 'list)
(coerce " x" 'list))
(empty-tree))
(empty-tree)))
; test input 5
(avl-tree->output-lines 4 4 0 (build-next-generation 4 4 0 (input-lines->avl-tree 0 0 4 (list (coerce " x " 'list)
(coerce "xx " 'list)
(coerce " " 'list)
(coerce " " 'list))
(empty-tree))
(empty-tree)))
|#
(defun main (state)
(mv-let (things state)
(stdin->avl-tree state)
(let ((avl-tree (car things))
(width (cadr things))
(height (caddr things)))
(string-list->stdout (avl-tree->output-lines width height height (build-next-generation width height (- height 1) avl-tree
(empty-tree))) state))))