-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-1.cfm
More file actions
42 lines (38 loc) · 1007 Bytes
/
13-1.cfm
File metadata and controls
42 lines (38 loc) · 1007 Bytes
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
<cffunction name="solve" output="false">
<cfargument name="input" required="true" />
<cfset var firewall = {} />
<cfset var maxDepth = 0 />
<cfset var line = '' />
<cfset var words = '' />
<cfset var depth = '' />
<cfloop list="#arguments.input#" item="line" delimiters="#Chr(10)#">
<cfset words = ListToArray(Trim(line), Chr(32)) />
<cfset depth = Replace(words[1], ':', '') />
<cfset firewall[depth] = words[2] />
<cfset maxDepth = Max(maxDepth, depth) />
</cfloop>
<cfset var score = 0 />
<cfset var myPos = '' />
<cfloop from="0" to="#maxDepth#" index="myPos">
<cfif StructKeyExists(firewall, myPos)>
<cfif myPos % (firewall[myPos] * 2 - 2) eq 0>
<cfset score += myPos * firewall[myPos] />
</cfif>
</cfif>
</cfloop>
<cfreturn score />
</cffunction>
<cfset testCases = [
{
input = '0: 3
1: 2
4: 4
6: 4',
expectedOutput = 24
},
{
input = Trim(FileRead(ExpandPath('13.txt'))),
expectedOutput = 2508
}
] />
<cfinclude template="test_runner_include.cfm" />