-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-2.cfm
More file actions
72 lines (68 loc) · 1.37 KB
/
9-2.cfm
File metadata and controls
72 lines (68 loc) · 1.37 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
<cffunction name="solve" output="false">
<cfargument name="input" required="true" />
<cfset var score = 0 />
<cfset var level = 1 />
<cfset var garbageCount = 0 />
<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++ />
<cfelse>
<cfset garbageCount++ />
</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 garbageCount />
</cffunction>
<cfset testCases = [
{
input = '<>',
expectedOutput = 0
},
{
input = '<random characters>',
expectedOutput = 17
},
{
input = '<<<<>',
expectedOutput = 3
},
{
input = '<{!>}>',
expectedOutput = 2
},
{
input = '<!!>',
expectedOutput = 0
},
{
input = '<!!!>>',
expectedOutput = 0
},
{
input = '<{o"i!a,<{i<a>',
expectedOutput = 10
},
{
input = Trim(FileRead(ExpandPath('9.txt'))),
expectedOutput = 10045
}
] />
<cfinclude template="test_runner_include.cfm" />