-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03_part02.fs
65 lines (61 loc) · 2.55 KB
/
day03_part02.fs
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
module day03_part02
open AdventOfCode_2017.Modules.LocalHelper
let getValueForPosition (position: int[]) (spiral: int[,]) =
let rowRange = [-1..1]
let columnRange = [-1..1]
let value = [|0|]
for row in rowRange do
for column in columnRange do
value.[0] <- value.[0] + spiral[position.[0] + row, position.[1] + column]
value.[0]
let rec buildSpiral (mid: int) (input: int) (result: int[]) (ring: int) (spiral: int[,]) =
if result.[0] > input then
result.[0]
else
if ring = 0 then
spiral[mid, mid] <- 1
else
// building right side
let rightRange = [(-ring + 1) .. (ring - 1)] |> Seq.rev
for row in rightRange do
let valueToAdd = getValueForPosition [|mid + row; mid + ring|] spiral
spiral[mid + row, mid + ring] <- valueToAdd
if valueToAdd > input && result.[0] = 0 then
result.[0] <- valueToAdd
else
()
// building top side
let topRange = [-ring .. ring] |> Seq.rev
for column in topRange do
let valueToAdd = getValueForPosition [|mid - ring; mid + column|] spiral
spiral[mid - ring, mid + column] <- valueToAdd
if valueToAdd > input && result.[0] = 0 then
result.[0] <- valueToAdd
else
()
// building left side
let leftRange = [(-ring + 1) .. (ring - 1)]
for row in leftRange do
let valueToAdd = getValueForPosition [|mid + row; mid - ring|] spiral
spiral[mid + row, mid - ring] <- valueToAdd
if valueToAdd > input && result.[0] = 0 then
result.[0] <- valueToAdd
else
()
// building bottom side
let downRange = [-ring .. ring]
for column in downRange do
let valueToAdd = getValueForPosition [|mid + ring; mid + column|] spiral
spiral[mid + ring, mid + column] <- valueToAdd
if valueToAdd > input && result.[0] = 0 then
result.[0] <- valueToAdd
else
()
buildSpiral mid input result (ring + 1) spiral
let execute() =
let path = "day03/day03_input.txt"
let input = GetContentFromFile path |> int
let length = 1000
let spiral = Array2D.create length length 0
let mid = length / 2
buildSpiral mid input [|0|] 0 spiral