-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.coffee
92 lines (82 loc) · 869 Bytes
/
23.coffee
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
_log = require 'ololog'
input = '''
jio a, +18
inc a
tpl a
inc a
tpl a
tpl a
tpl a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
tpl a
tpl a
inc a
jmp +22
tpl a
inc a
tpl a
inc a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
jio a, +8
inc b
jie a, +4
tpl a
inc a
jmp +2
hlf a
jmp -7
'''.split '\n'
exec = ( ops, a )->
i = 0
r =
a:a ? 0
b:0
while 0<=i<ops.length
l = ops[i]
#_log i, r.a, r.b, l
switch l[..2]
when 'hlf'
r[l[4]] //= 2
when 'tpl'
r[l[4]] *= 3
when 'inc'
++r[l[4]]
when 'jmp'
i += +l[4..] - 1
when 'jie'
if r[l[4]]%2 is 0
i += +l[6..] - 1
when 'jio'
if r[l[4]] is 1
i += +l[6..] - 1
else
throw new Error "unknown instruction @#{i}: #{l}"
++i
r
try
_log.yellow exec input
_log.yellow exec input, 1
catch e
_log.red e