-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-1.cfm
More file actions
73 lines (69 loc) · 1.4 KB
/
9-1.cfm
File metadata and controls
73 lines (69 loc) · 1.4 KB
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
<cffunction name="solve" output="false">
<cfargument name="input" required="true" />
<cfset var score = 0 />
<cfset var level = 1 />
<cfset var inGarbage = false />
<cfset var charIndex = 1 />
<cfset var char = '' />
<cfloop condition="charIndex lte Len(arguments.input)">
<cfset char = Mid(arguments.input, charIndex, 1) />
<cfif inGarbage>
<cfif char eq '>'>
<cfset inGarbage = false />
<cfelseif char eq '!'>
<cfset charIndex++ />
</cfif>
<cfelseif char eq '<'>
<cfset inGarbage = true />
<cfelseif char eq '{'>
<cfset score += level />
<cfset level++ />
<cfelseif char eq '}'>
<cfset level-- />
<cfelseif char eq ','>
<cfelse>
<cfthrow message="Unexpected char: #char#" />
</cfif>
<cfset charIndex++ />
</cfloop>
<cfreturn score />
</cffunction>
<cfset testCases = [
{
input = '{}',
expectedOutput = 1
},
{
input = '{{{}}}',
expectedOutput = 6
},
{
input = '{{},{}}',
expectedOutput = 5
},
{
input = '{{{},{},{{}}}}',
expectedOutput = 16
},
{
input = '{<a>,<a>,<a>,<a>}',
expectedOutput = 1
},
{
input = '{{<ab>},{<ab>},{<ab>},{<ab>}}',
expectedOutput = 9
},
{
input = '{{<!!>},{<!!>},{<!!>},{<!!>}}',
expectedOutput = 9
},
{
input = '{{<a!>},{<a!>},{<a!>},{<ab>}}',
expectedOutput = 3
},
{
input = Trim(FileRead(ExpandPath('9.txt'))),
expectedOutput = 23588
}
] />
<cfinclude template="test_runner_include.cfm" />