-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.coffee
67 lines (61 loc) · 1.43 KB
/
9.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
{_log,test,expect,testAndRun} = require './util'
clear_garbage = ( input, cb )->
inside = no
escape = no
cleared = 0
for c, i in input
if escape
escape = no
continue
if inside
switch c
when '!'
escape = yes
when '>'
inside = no
else
++cleared
else
switch c
when '<'
inside = yes
else
cb c
cleared
get_score = ( input )->
res = ''
level = 0
score = 0
cleared = clear_garbage input, (c)->
res+=c
switch c
when '{'
++level
when '}'
score += level--
return
[score,cleared]
test.score = ->
expect.nth(0) 1, get_score '{}'
expect.nth(0) 6, get_score '{{{}}}'
expect.nth(0) 5, get_score '{{},{}}'
expect.nth(0) 16, get_score '{{{},{},{{}}}}'
expect.nth(0) 1, get_score '{<a>,<a>,<a>,<a>}'
expect.nth(0) 9, get_score '{{<ab>},{<ab>},{<ab>},{<ab>}}'
expect.nth(0) 9, get_score '{{<!!>},{<!!>},{<!!>},{<!!>}}'
expect.nth(0) 3, get_score '{{<a!>},{<a!>},{<a!>},{<ab>}}'
return
test.cleared = ->
expect.nth(1) 0, get_score '<>'
expect.nth(1) 17, get_score '<random characters>'
expect.nth(1) 3, get_score '<<<<>'
expect.nth(1) 2, get_score '<{!>}>'
expect.nth(1) 0, get_score '<!!>'
expect.nth(1) 0, get_score '<!!!>>'
expect.nth(1) 10, get_score '<{o"i!a,<{i<a>'
return
testAndRun ->
[score,cleared] = get_score require './input/9.txt'
_log.yellow '1:', score
_log.yellow '2:', cleared
return