-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14-2.cfm
More file actions
68 lines (59 loc) · 2.24 KB
/
Copy path14-2.cfm
File metadata and controls
68 lines (59 loc) · 2.24 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
<cffunction name="findAndMarkNeighbours" output="false">
<cfargument name="grid" required="true" />
<cfargument name="rowIndex" required="true" />
<cfargument name="colIndex" required="true" />
<cfif arguments.rowIndex lt 0 || arguments.rowIndex gt 127 || arguments.colIndex lt 0 || arguments.colIndex gt 127 || arguments.grid[arguments.rowIndex][arguments.colIndex] eq '0'>
<cfreturn />
</cfif>
<cfset arguments.grid[arguments.rowIndex][arguments.colIndex] = '0' />
<cfset findAndMarkNeighbours(arguments.grid, arguments.rowIndex - 1, arguments.colIndex) />
<cfset findAndMarkNeighbours(arguments.grid, arguments.rowIndex + 1, arguments.colIndex) />
<cfset findAndMarkNeighbours(arguments.grid, arguments.rowIndex, arguments.colIndex - 1) />
<cfset findAndMarkNeighbours(arguments.grid, arguments.rowIndex, arguments.colIndex + 1) />
</cffunction>
<cffunction name="solve" output="false">
<cfargument name="input" required="true" />
<cfset var adventOfCode = CreateObject('component', 'AdventOfCode') />
<cfset var grid = {} />
<cfset var rowIndex = '' />
<cfset var knotHash = '' />
<cfset var colIndex = '' />
<cfset var charIndex = '' />
<cfset var char = '' />
<cfset var bits = '' />
<cfset var i = '' />
<cfloop from="0" to="127" index="rowIndex">
<cfset grid[rowIndex] = {} />
<cfset knotHash = adventOfCode.getKnotHash(arguments.input & '-' & rowIndex) />
<cfset colIndex = 0 />
<cfloop from="1" to="#Len(knotHash)#" index="charIndex">
<cfset char = Mid(knotHash, charIndex, 1) />
<cfset bits = Right('0000' & FormatBaseN(InputBaseN(char, 16), 2), 4) />
<cfloop from="1" to="4" index="i">
<cfset grid[rowIndex][colIndex] = Mid(bits, i, 1) />
<cfset colIndex++ />
</cfloop>
</cfloop>
</cfloop>
<cfset var numRegions = 0 />
<cfloop from="0" to="127" index="rowIndex">
<cfloop from="0" to="127" index="colIndex">
<cfif grid[rowIndex][colIndex] eq '1'>
<cfset numRegions++ />
<cfset findAndMarkNeighbours(grid, rowIndex, colIndex) />
</cfif>
</cfloop>
</cfloop>
<cfreturn numRegions />
</cffunction>
<cfset testCases = [
{
input = 'flqrgnkx',
expectedOutput = 1242
},
{
input = Trim(FileRead(ExpandPath('14.txt'))),
expectedOutput = 1141
}
] />
<cfinclude template="test_runner_include.cfm" />