-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22-1.cfm
More file actions
55 lines (51 loc) · 1.61 KB
/
22-1.cfm
File metadata and controls
55 lines (51 loc) · 1.61 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
<cffunction name="solve" output="false">
<cfargument name="input" required="true" />
<cfset var grid = CreateObject('component', 'Grid').init() />
<cfset var line = '' />
<cfset var lineIndex = '' />
<cfset var char = '' />
<cfset var charIndex = '' />
<cfset var position = '' />
<cfloop list="#arguments.input#" item="line" index="lineIndex" delimiters="#Chr(10)#">
<cfloop array="#ListToArray(Trim(line), '')#" item="char" index="charIndex">
<cfset position = { x = charIndex, y = lineIndex } />
<cfset grid.setValue(position, char) />
</cfloop>
</cfloop>
<cfset var infections = 0 />
<cfset var center = (Len(line) - 1) / 2 + 1 />
<cfset var currentPosition = { x = center, y = center } />
<cfset var currentDirection = 'UP' />
<cfset var burst = '' />
<cfloop from="1" to="10000" index="burst">
<cfswitch expression="#grid.getValue(currentPosition, '.')#">
<cfcase value="##">
<cfset currentDirection = grid.turnRight(currentDirection) />
<cfset grid.setValue(currentPosition, '.') />
</cfcase>
<cfcase value=".">
<cfset currentDirection = grid.turnLeft(currentDirection) />
<cfset grid.setValue(currentPosition, '##') />
<cfset infections++ />
</cfcase>
<cfdefaultcase>
<cfthrow message="Unexpected" />
</cfdefaultcase>
</cfswitch>
<cfset currentPosition = grid.move(currentPosition, currentDirection) />
</cfloop>
<cfreturn infections />
</cffunction>
<cfset testCases = [
{
input = '..##
##..
...',
expectedOutput = 5587
},
{
input = Trim(FileRead(ExpandPath('22.txt'))),
expectedOutput = 5246
}
] />
<cfinclude template="test_runner_include.cfm" />