-
Notifications
You must be signed in to change notification settings - Fork 5
/
asm-blox-puzzles.el
1189 lines (1127 loc) · 49.9 KB
/
asm-blox-puzzles.el
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; asm-blox-puzzles.el --- Puzzle Definitions for asm-blox -*- lexical-binding: t -*-
;; Author: Zachary Romero
;; Maintainer: Zachary Romero
;; Version: 0.0.1
;; Homepage: https://github.com/zkry/asm-blox
;; Keywords: games
;; This file is not part of GNU Emacs
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains the definitions of the various game puzzles.
;;; Code:
(require 'cl-lib)
(defvar asm-blox-puzzles)
(declare-function asm-blox--problem-spec-create "asm-blox")
(declare-function asm-blox--cell-source-create "asm-blox")
(declare-function asm-blox--cell-sink-create "asm-blox")
(declare-function asm-blox--problem-spec-name "asm-blox")
(declare-function asm-blox--flatten-list "asm-blox")
(defun asm-blox-puzzles-list-of-lists-to-lisp (lists)
"Return a list of LISTS from 0-terminated list of number lists."
(reverse (car (seq-reduce (lambda (acc x)
(let ((lol (car acc))
(curr-list (cadr acc)))
(if (= x 0)
(list (cons (reverse curr-list) lol) '())
(list lol (cons x curr-list)))))
lists
(list '() '())))))
(defun asm-blox-puzzles-random-list-of-lists (&optional limit)
"Generate list of 0-terminated lists as helper.
If LIMIT is non-nil, the number generated will be less-than it."
(let* ((nums (seq-map (lambda (_) (if limit
(1+ (random limit))
(1+ (random 998))))
(make-list 40 nil)))
(breaks (seq-map (lambda (_)
(random 5))
(make-list 40 nil)))
(switched nil)
(i 0))
(seq-mapn (lambda (a b)
(setq i (1+ i))
(if (or (= i 40)
(and (not (= i 39))
(not (= i 1))
(and (not switched) (= b 0))))
(progn
(setq switched t)
0)
(setq switched nil)
a))
nums breaks)))
(defun asm-blox-puzzles--stack-machine-solver (args ops)
"Simulate the stack-machine with ARGS and OPS to generate solution."
(let ((args (seq-reverse args))
(result))
(dolist (op ops)
(pcase op
(0 (setq result (append result (list (car args))))
(setq args (cdr args)))
(1 (setq args (cons (+ (car args) (cadr args)) (cddr args))))
(2 (setq args (cons (- (car args)) (cdr args))))))
result))
(defun asm-blox-puzzles--stack-machine ()
"Generate a problem for simulating a simple stack machine."
(let* ((args (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(ops (append (seq-map (lambda (_) (random 3)) (make-list 10 nil)) (list 0)))
(result (asm-blox-puzzles--stack-machine-solver args ops)))
(asm-blox--problem-spec-create
:name "Stack Machine"
:difficulty 'hard
:sources (list (asm-blox--cell-source-create :row 0
:col -1
:data ops
:name "O")
(asm-blox--cell-source-create :row 0
:col 4
:data args
:name "A"))
:sinks
(list (asm-blox--cell-sink-create
:row 3
:col 1
:expected-data result
:name "T"))
:description "Read all 40 values from A, pushing them on a
stack so that the last number in A is at the top of the stack.
One by one, read an operation from O and do the following based on it's value:
0 -> send the top value of the stack to T.
1 -> pop the top two values of the stack, add them, and push onto the stack
2 -> pop the top value of the stack, multiply it by -1, and push onto the stack.")))
(defun asm-blox-puzzles--triangle-area ()
"Generate a problem of determining area of triangle."
(let* ((bases (seq-map (lambda (_) (* (1+ (random 10)) 2)) (make-list 40 nil)))
(heights (seq-map (lambda (_) (1+ (random 20))) (make-list 40 nil)))
(areas (seq-mapn (lambda (b h) (/ (* b h) 2)) bases heights)))
(asm-blox--problem-spec-create
:name "Triangle Area"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row 3
:col 2
:data bases
:name "B")
(asm-blox--cell-source-create :row 1
:col 4
:data heights
:name "H"))
:sinks
(list (asm-blox--cell-sink-create
:row -1
:col 1
:expected-data areas
:name "A"))
:description "Read base and height of a right-triangle
from B and H respectively. Send the area of right-triangle to A")))
(defun asm-blox-puzzles--delete-word ()
"Generate a problem of deleting a word."
(let* ((words '("chair" "pen" "book" "camera" "note" "printer" "cable"
"square" "thought" "mouse" "alarm" "case" "lamp" "bed"))
(word-ct (length words))
(l1 (string-join (list (nth (random word-ct) words)
(nth (random word-ct) words)
(nth (random word-ct) words)) " "))
(l2 (string-join (list (nth (random word-ct) words)
(nth (random word-ct) words)
(nth (random word-ct) words)) " "))
(l3 (string-join (list (nth (random word-ct) words)
(nth (random word-ct) words)
(nth (random word-ct) words)) " "))
(text (string-join (list l1 l2 l3) "\n"))
(del-idx (random 9))
(new-text (with-temp-buffer
(insert text)
(goto-char (point-min))
(dotimes (_ del-idx)
(forward-word 1)
(forward-char 1))
(kill-word 1)
(buffer-string))))
(asm-blox--problem-spec-create
:name "Delete Word"
:difficulty 'hard
:sources (list (asm-blox--cell-source-create :row -1
:col 3
:data (list del-idx)
:name "I"))
:sinks
(list (asm-blox--cell-sink-create
:row 1
:col 5
:expected-data nil
:name "O"
:default-editor-text text
:editor-point 0
:expected-text
new-text))
:description "<editor> Read 0-based index from I. Delete
that number word in the text.")))
;; TODO: give different inputs
(defun asm-blox-puzzles--indentation ()
"Generate a problem of indenting a code sequence properly."
(asm-blox--problem-spec-create
:name "Indentation I"
:difficulty 'medium
:sinks
(list (asm-blox--cell-sink-create
:row 1
:col 5
:expected-data nil
:name "O"
:default-editor-text
"func main () {\nfmt.Println(\"hello world\")\nreturn\n}"
:editor-point 1
:expected-text
"func main () {\n fmt.Println(\"hello world\")\n return\n}"))
:description "<editor> Edit text to match the target."))
(defun asm-blox-puzzles--number-sum ()
"Generate a problem of calculating y=x(x+1)/2."
(let* ((input (seq-map (lambda (_) (+ 1 (random 10))) (make-list 40 nil)))
(expected-output (seq-map (lambda (x) (/ (* x (+ 1 x)) 2))input)))
(asm-blox--problem-spec-create
:name "Number Sum"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 3
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 2
:col 4
:expected-data expected-output
:name "O"
:editor-text nil
:editor-point nil
:expected-text nil))
:description "Read a number from I, send to O the sum of numbers
from 0 to the read number. (ex. 3->6, 4->10, 5->15)")))
(defun asm-blox-puzzles--meeting-point ()
"Generate a problem of finding the point that minimizes movement."
(let* ((input (seq-map (lambda (_) (+ 1 (random 10))) (make-list 10 nil)))
(expected-output
(list (cl-loop for i from 1 to 1000
minimize (cl-loop for d in input
sum (abs (- i d)))))))
(asm-blox--problem-spec-create
:name "Meeting point"
:difficulty 'hard
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data input
:name "N"))
:sinks
(list (asm-blox--cell-sink-create :row 2
:col 4
:expected-data expected-output
:name "O"
:editor-text nil
:editor-point nil
:expected-text nil))
:description "Read the 10 numbers from N (n1, n2, ..., n10).
Send a number x which minimizes the equation
(cl-loop for n in N
sum (abs (- n x)))")))
(defun asm-blox-puzzles--simple-graph ()
"Generate a problem for the user to draw a simple graph."
(let* ((input (seq-map (lambda (_) (+ 1 (random 10))) (make-list 10 nil)))
(expected-text (string-join
(seq-map (lambda (x) (make-string x ?#)) input) "\n")))
(asm-blox--problem-spec-create
:name "Simple Graph"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data input
:name "A"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 5
:expected-data nil
:name "O"
:editor-text ""
:editor-point 1
:expected-text expected-text))
:description
"<editor> Read a number from A,
draw a line with that many '#' characters.")))
(defun asm-blox-puzzles--hello-world ()
"Generate a problem involving writing Hello World to the screen."
(asm-blox--problem-spec-create
:name "Editor Basics"
:difficulty 'easy
:sources (list )
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 5
:expected-data nil
:name "O"
:editor-text "01"
:editor-point 3
:expected-text "Hello World"))
:description "<editor> Write the string \"Hello World\" to the editor."))
(defun asm-blox-puzzles--upcase ()
"Generate a problem involving upcasing characters."
(let* ((input-1 (seq-map (lambda (_)
(+ (random 95) 32))
(make-list 40 nil)))
(expected (string-to-list (upcase (apply #'string input-1)))))
(asm-blox--problem-spec-create
:name "Upcase"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data input-1
:name "C"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 4
:expected-data expected
:name "O"))
:description "Read a character from C and send it to O,
upcasing it if it is a lowercase letter.")))
(defun asm-blox-puzzles--inc-ct ()
"Generate a simple addition problem."
(let* ((input-1 (append (seq-map (lambda (_)
(random 999))
(make-list 39 nil))
(list 0)))
(expected (list (seq-reduce #'+
(seq-mapn (lambda (a b)
(if (> b a) 1 0))
input-1
(cdr input-1))
0))))
(asm-blox--problem-spec-create
:name "Increment Cout"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data input-1
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 4
:expected-data expected
:name "O"))
:description
"Return the number of times subsequent values of I increase.
ex. 1 2 0 5 6 4
+ - + + - 3 increses")))
(defun asm-blox-puzzles--list-reverse ()
"Generate a simple list reverse problem."
(let* ((input-1 (asm-blox-puzzles-random-list-of-lists))
(lists (asm-blox-puzzles-list-of-lists-to-lisp input-1))
(expected (asm-blox--flatten-list (seq-map (lambda (l)
(append (reverse l) (list 0)))
lists))))
(asm-blox--problem-spec-create
:name "List Reverse"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row -1
:col 2
:data input-1
:name "L"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data expected
:name "R"))
:description
"Lists are 0 terminated.
Read a list from L, reverse it, and send it to R (terminating it with 0).")))
(defun asm-blox-puzzles--list-length ()
"Generate a simple addition problem."
(let* ((nums)
(lengths))
(while (< (length nums) 30)
(let ((len (1+ (random 10))))
(setq lengths (cons len lengths))
(setq nums (append nums
(seq-map (lambda (_)
(1+ (random 10)))
(make-list len nil))
(list 0)))))
(setq lengths (reverse lengths))
lengths
(asm-blox--problem-spec-create
:name "List Length"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data nums
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 4
:expected-data lengths
:name "O"))
:description "Lists are 0 terminated. Read a list from I,
calculate its length and send it to O.")))
(defun asm-blox-puzzles--turing ()
"Generate a simple Brain****-like puzzle."
(let* ((input-1 (list ?> ?> ?> ?+ ?+ ?. ?. ?< ?+ ?. ?> ?. ?+ ?. ?> ?> ?.))
(expected (list 2 2 1 2 3 0)))
(asm-blox--problem-spec-create
:name "Turing"
:difficulty 'hard
:sources (list (asm-blox--cell-source-create :row 0
:col -1
:data input-1
:name "X"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 4
:expected-data expected
:name "O"))
:description
"Read a number from X. Implement a machine that moves a head
on a tape with values of all zero.
- If X is the ASCII character '<' move the head one position to the left
- If X is the ASCII character '>' move the head one position to the right
- If X is the ASCII character '+' increment the value of the cell
- If X is the ASCII character '.' send the current value at the tape to O.
NOTE The head will go no more than +-10 spaces
from where the head starts off.")))
(defun asm-blox-puzzles--merge-step ()
"Generate a simple addition problem."
(let* ((input-1 (seq-sort #'< (seq-map (lambda (_) (random 100))
(make-list 20 nil))))
(input-2 (seq-sort #'< (seq-map (lambda (_) (random 100))
(make-list 20 nil))))
(expected (seq-sort #'< (append input-1 input-2))))
(asm-blox--problem-spec-create
:name "Merge Step"
:difficulty 'hard
:sources (list (asm-blox--cell-source-create :row 0
:col -1
:data input-1
:name "A")
(asm-blox--cell-source-create :row 2
:col -1
:data input-2
:name "B"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 4
:expected-data expected
:name "C"))
:description "Numbers in A and B are sorted. Read numbers from A and B,
combine them sorted and send it them to C.")))
(defun asm-blox-puzzles--filter ()
"Generate a simple addition problem."
(let* ((input-1 (seq-map (lambda (_) (random 100)) (make-list 40 nil)))
(expected (seq-map (lambda (x)
(if (= (mod x 2) 0)
0
x))
input-1)))
(asm-blox--problem-spec-create
:name "Number Filter"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data input-1
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 1
:col 4
:expected-data expected
:name "O"))
:description
"Read a value from I. If it is even send 0 to O, else send the value.")))
(defun asm-blox-puzzles--clock ()
"Generate a simple addition problem."
(let* ((input-1 (seq-map (lambda (_) (random 24)) (make-list 40 nil)))
(expected (cdr (seq-reverse
(seq-reduce (lambda (acc x)
(let ((top (car acc)))
(cons (mod (+ top x) 24)
acc)))
input-1
'(0))))))
(asm-blox--problem-spec-create
:name "Clock Hours"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row 1
:col -1
:data input-1
:name "H"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data expected
:name "T"))
:description "On a clock with hours 0 to 23, read a value from H and add
that value to the current time which starts at 0.
Write the current time to T for every time you move the current time.")))
(defun asm-blox-puzzles--add ()
"Generate a simple addition problem."
(let* ((input-1 (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(input-2 (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(input-3 (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(expected (seq-mapn #'+ input-1 input-2 input-3)))
(asm-blox--problem-spec-create
:name "Number Addition"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 0
:data input-1
:name "A")
(asm-blox--cell-source-create :row -1
:col 1
:data input-2
:name "B")
(asm-blox--cell-source-create :row -1
:col 2
:data input-3
:name "C"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data expected
:name "S"))
:description
"Take input from A, B, and C, add the three together, and send it to S.")))
(defun asm-blox-puzzles--number-sorter ()
"Generate problem of comparing two numbers, send them in different places."
(let* ((input-1 (seq-map (lambda (_) (random 10))
(make-list 40 nil)))
(input-2 (seq-map (lambda (_) (random 10))
(make-list 40 nil)))
(expected-1 (seq-mapn (lambda (a b) (if (> a b) a 0))
input-1 input-2))
(expected-2 (seq-mapn (lambda (a b) (if (> b a) b 0))
input-1 input-2)))
(asm-blox--problem-spec-create
:name "Number Chooser"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 0
:data input-1
:name "A")
(asm-blox--cell-source-create :row -1
:col 1
:data input-2
:name "B"))
:sinks
(list (asm-blox--cell-sink-create :row 0
:col 4
:expected-data expected-1
:name "L")
(asm-blox--cell-sink-create :row 2
:col 4
:expected-data expected-2
:name "R"))
:description "Take an input from A and B. If A>B then send A to L, 0 to R;
If B>A then send B to R, 0 to L. If A=B send 0 to L and R.")))
(defun asm-blox-puzzles--constant ()
"Generate a simple addition problem."
(let* ((expected (make-list 40 1)))
(asm-blox--problem-spec-create
:name "Constant Generator"
:difficulty 'tutorial
:sources (list )
:sinks
(list (asm-blox--cell-sink-create :row 0
:col 4
:expected-data expected
:name "N"))
:description "Repeatedly send the number 1 to N. There are no inputs.")))
(defun asm-blox-puzzles--identity ()
"Generate a simple addition problem."
(let* ((input-1 (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(expected input-1))
(asm-blox--problem-spec-create
:name "Identity"
:difficulty 'tutorial
:sources (list (asm-blox--cell-source-create :row -1
:col 0
:data input-1
:name "X"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 3
:expected-data expected
:name "X"))
:description
"Take an input from the input X and send it to the output X.")))
(defun asm-blox-puzzles--diagnostic-test ()
"Generate a problem."
(let* ((input-1 (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(input-2 (seq-map (lambda (_) (random 10)) (make-list 40 nil))))
(asm-blox--problem-spec-create
:name "Diagnostic Test"
:difficulty 'tutorial
:sources (list (asm-blox--cell-source-create :row 0
:col -1
:data input-1
:name "A")
(asm-blox--cell-source-create :row 2
:col -1
:data input-2
:name "B"))
:sinks
(list (asm-blox--cell-sink-create :row 0
:col 4
:expected-data input-1
:name "X")
(asm-blox--cell-sink-create :row 2
:col 4
:expected-data input-2
:name "Y"))
:description
"Send data from A to X. Send data from B to Y.")))
(defun asm-blox-puzzles--signal-amplifier ()
"Generate a problem."
(let* ((input (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(output (seq-map (lambda (x) (* 2 x)) input)))
(asm-blox--problem-spec-create
:name "Signal Amplifier"
:difficulty 'tutorial
:sources (list (asm-blox--cell-source-create :row -1
:col 2
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data output
:name "O"))
:description
"Read a value from I. Double it. Send that to O.")))
(defun asm-blox-puzzles--differential-converter ()
"Generate a problem."
(let* ((input-a (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(input-b (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(output-p (seq-mapn (lambda (a b) (- a b)) input-a input-b))
(output-n (seq-mapn (lambda (a b) (- b a)) input-a input-b)))
(asm-blox--problem-spec-create
:name "Differential Converter"
:difficulty 'tutorial
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input-a
:name "A")
(asm-blox--cell-source-create :row -1
:col 2
:data input-b
:name "B"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data output-p
:name "P")
(asm-blox--cell-sink-create :row 3
:col 2
:expected-data output-n
:name "N"))
:description
"Read a value from A and B. Send A - B to P. Send B - A to N.")))
(defun asm-blox-puzzles--signal-comparator ()
"Generate a problem."
(let* ((input (seq-map (lambda (_) (- (random 10) 5)) (make-list 40 nil)))
(output-g (seq-mapn (lambda (x) (if (> x 0) 1 0)) input))
(output-e (seq-mapn (lambda (x) (if (= x 0) 1 0)) input))
(output-l (seq-mapn (lambda (x) (if (< x 0) 1 0)) input)))
(asm-blox--problem-spec-create
:name "Signal Comparator"
:difficulty 'tutorial
:sources (list (asm-blox--cell-source-create :row -1
:col 0
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data output-g
:name "G")
(asm-blox--cell-sink-create :row 3
:col 2
:expected-data output-e
:name "E")
(asm-blox--cell-sink-create :row 3
:col 3
:expected-data output-l
:name "L"))
:description
"Read a value from I.
If I > 0 send 1 to G.
If I < 0 send 1 to L.
If I = 0 send 1 to E.
When sending 1 to output, send 0 to the other two output ports.")))
(defun asm-blox-puzzles--sequence-generator ()
"Generate a problem."
(let* ((input-1 (seq-map (lambda (_) (1+ (* (random 10) 2))) (make-list 8 nil)))
(input-2 (seq-map (lambda (_) (* (random 10) 2)) (make-list 8 nil)))
(output (asm-blox--flatten-list
(seq-mapn (lambda (a b)
(if (< a b)
(list a b 0)
(list b a 0)))
input-1 input-2))))
(asm-blox--problem-spec-create
:name "Sequence Generator"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input-1
:name "A")
(asm-blox--cell-source-create :row -1
:col 2
:data input-2
:name "B"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data output
:name "O"))
:description
"Read a value from A and B.
Send the lesser of the two to O.
Send the other value to O.
Send 0 to O.")))
(defun asm-blox-puzzles--sequence-counter ()
"Generate a problem."
(let* ((input (asm-blox-puzzles-random-list-of-lists 30))
(lists (asm-blox-puzzles-list-of-lists-to-lisp input))
(output-sum (seq-map
(lambda (list)
(apply #'+ list))
lists))
(output-length (seq-map
(lambda (list)
(length list))
lists)))
(asm-blox--problem-spec-create
:name "Sequence Counter"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data output-sum
:name "S")
(asm-blox--cell-sink-create :row 3
:col 3
:expected-data output-length
:name "L"))
:description
"Read a 0-terminated sequence from I.
Write the length of the sequence to L.
Write the sum of the sequence to S.")))
(defun asm-blox-puzzles--signal-edge-detector ()
"Generate a problem."
(let* ((input (asm-blox-puzzles-random-list-of-lists 100))
(output (seq-mapn (lambda (at prev)
(if (>= at (+ prev 10))
1
0))
input
(cons (car input) input))))
(asm-blox--problem-spec-create
:name "Signal Edge Detector"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data output
:name "O"))
:description
"Read a value from I, comparing it with the previous value.
If the value increased by 10 or more, write 1 to O.
Otherwise write 0 to O.
Always write 0 for the first input.")))
(defun asm-blox-puzzles--make-interrupt-handler-seq ()
"Generated an interrupt sequence."
(let ((state 'zero)
(res '()))
(while (< (length res) 40)
(let ((seq-length (1+ (random 9))))
(when (> (+ seq-length (length res)) 40)
(setq seq-length (- 40 (length res))))
(setq res (append res (make-list seq-length (if (eql state 'zero) 0 1))))
(setq state (if (eql state 'zero) 'one 'zero))))
res))
(defun asm-blox-puzzles--make-interrupt-handler-solution (a b c d)
"Return the solution to interrupt handler given sequences A, B, C and D."
(catch 'result
(cl-labels
((compare (x prev) (if (and (= prev 0) (= x 1)) 1 0)))
(seq-mapn
(lambda (a pa b pb c pc d pd)
(let ((ress (list (compare a pa)
(compare b pb)
(compare c pc)
(compare d pd))))
(when (> (apply #'+ ress) 1)
(throw 'result nil))
(if (= (apply #'+ ress) 0)
0
(seq-find #'identity
(seq-mapn (lambda (x idx)
(if (= 1 x)
idx
nil))
ress '(1 2 3 4))))))
(cdr a) a (cdr b) b (cdr c) c (cdr d) d))))
(defun asm-blox-puzzles--interrupt-handler ()
"Generate a problem."
(let* ((input-a)
(input-b)
(input-c)
(input-d)
(res))
;; kindof hacky....
(while (not res)
(setq input-a (asm-blox-puzzles--make-interrupt-handler-seq))
(setq input-b (asm-blox-puzzles--make-interrupt-handler-seq))
(setq input-c (asm-blox-puzzles--make-interrupt-handler-seq))
(setq input-d (asm-blox-puzzles--make-interrupt-handler-seq))
(setq res (asm-blox-puzzles--make-interrupt-handler-solution
input-a input-b input-c input-d)))
(asm-blox--problem-spec-create
:name "Interrupt Handler"
:difficulty 'easy
:sources (list (asm-blox--cell-source-create :row -1
:col 0
:data input-a
:name "1")
(asm-blox--cell-source-create :row -1
:col 1
:data input-b
:name "2")
(asm-blox--cell-source-create :row -1
:col 2
:data input-c
:name "3")
(asm-blox--cell-source-create :row -1
:col 3
:data input-d
:name "4"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data res
:name "O"))
:description
"Read inputs 1, 2, 3, and 4.
Whenever an input sequence changes from 0 to 1
write the input port number to O.
Example:
/---- Input ports
1 2 3 4 | O <- Output
--------+--
0 0 0 0 | 0
0 1 0 0 | 2
0 1 0 0 | 0
0 1 0 1 | 4
1 1 0 0 | 1
1 1 0 0 | 0")))
(defun asm-blox-puzzles--signal-pattern-detector ()
"Generate a problem."
(let* ((input (seq-map (lambda (_)
(let ((res (random 5)))
(if (>= res 3)
0
res)))
(make-list 40 nil)))
(output (seq-mapn
(lambda (at p1 p2)
(if (= at p1 p2 0)
1
0))
input
(cons -1 input)
(cons -1 (cons -1 input)))))
(if (< (apply #'+ output) 5)
;; hacky...
(asm-blox-puzzles--signal-pattern-detector)
(asm-blox--problem-spec-create
:name "Signal Pattern Detector"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data output
:name "O"))
:description
"Read a value from I.
Find the pattern 0, 0, 0:
- If the current value, and the two previous values are all 0,
write 1.
- Otherwise write 0."))))
(defun asm-blox-puzzles--sequence-peak-detector ()
"Generate a problem."
(let* ((input (asm-blox-puzzles-random-list-of-lists 100))
(input-list (asm-blox-puzzles-list-of-lists-to-lisp input))
(output-n (seq-map (lambda (l)
(apply #'min l))
input-list))
(output-x (seq-map (lambda (l)
(apply #'max l))
input-list)))
(asm-blox--problem-spec-create
:name "Sequence Peak Detector"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 1
:expected-data output-n
:name "N")
(asm-blox--cell-sink-create :row 3
:col 2
:expected-data output-x
:name "X"))
:description
"Read a 0-terminated sequence from I.
Write the minimum value of the sequence to N.
Write the maximum value of the sequence to X.")))
(defun asm-blox-puzzles--sequence-reverser ()
"Generate a problem."
(let* ((input (asm-blox-puzzles-random-list-of-lists 100))
(input-list (asm-blox-puzzles-list-of-lists-to-lisp input))
(reversed (asm-blox--flatten-list
(seq-map (lambda (l)
(append (reverse l) '(0)))
input-list))))
(asm-blox--problem-spec-create
:name "Sequence Reverser"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input
:name "I"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data reversed
:name "R"))
:description
"Read a sequence from I. Reverse the sequence and write it to R (0-terminated).")))
(defun asm-blox-puzzles--signal-multiplier ()
"Generate a problem."
(let* ((input-a (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(input-b (seq-map (lambda (_) (random 10)) (make-list 40 nil)))
(output (seq-mapn (lambda (a b) (* a b)) input-a input-b)))
(asm-blox--problem-spec-create
:name "Signal Multiplier"
:difficulty 'medium
:sources (list (asm-blox--cell-source-create :row -1
:col 1
:data input-a
:name "A")
(asm-blox--cell-source-create :row -1
:col 2
:data input-b
:name "B"))
:sinks
(list (asm-blox--cell-sink-create :row 3
:col 2
:expected-data output
:name "M"))
:description
"Read a value from A and B. Multiply the numbers. Send result to M."
:banned-commands '(MUL))))