-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22_part01.fs
92 lines (79 loc) · 2.89 KB
/
day22_part01.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module day22_part01
open AdventOfCode_2017.Modules
open System.Collections.Generic
type Status =
| Clean
| Infected
type Direction =
| Up
| Down
| Left
| Right
let parseContent (lines: string array) =
let (maxrows, maxcols) = (lines.Length / 2, lines[0].Length / 2)
let map = Dictionary<(int * int), Status>()
let (startrow, startcol) = (0 - maxrows, 0 - maxcols)
let mutable row = 0
let mutable col = 0
for r' in startrow..maxrows do
col <- 0
for c' in startcol..maxcols do
let status =
match lines[row][col] with
| '.' -> Clean
| '#' -> Infected
| _ -> failwith "error"
map.Add((r', c'), status)
col <- col + 1
row <- row + 1
map
let turn (current: Direction) (switch: Direction) =
match (current, switch) with
| (Up, Left) -> ((0, -1), Left)
| (Down, Left) -> ((0, 1), Right)
| (Up, Right) -> ((0, 1), Right)
| (Down, Right) -> ((0, -1), Left)
| (Left, Left) -> ((1, 0), Down)
| (Right, Left) -> ((-1, 0), Up)
| (Left, Right) -> ((-1, 0), Up)
| (Right, Right) -> ((1, 0), Down)
| _ -> failwith "error turning"
let printMap(map: Dictionary<(int*int), Status>) =
let minRows = map.Keys |> Seq.map fst |> Seq.min
let maxRows = map.Keys |> Seq.map fst |> Seq.max
let minCols = map.Keys |> Seq.map snd |> Seq.min
let maxCols = map.Keys |> Seq.map snd |> Seq.max
for row in minRows..maxRows do
for col in minCols..maxCols do
if map.ContainsKey((row, col)) then
printf "%s" (if map[(row, col)].IsClean then "." else "#")
else
printf "%s" "."
printfn "%s" System.Environment.NewLine
let startBurst (map: Dictionary<(int*int), Status>) ((carrier, dir): (int*int)*Direction) (numOfBurst: int) =
let rec burst ((c, d): (int*int)*Direction) (currentBurst: int) (numInfections: int)=
if currentBurst = numOfBurst then
numInfections
else
let currentnode =
match map.TryGetValue(c) with
| true, node -> node
| false, _ ->
map.Add(c, Clean)
map[c]
let ((dr, dc), newdir), newinfections =
if currentnode.IsInfected then
map[c] <- Clean
(turn d Right, numInfections)
else
map[c] <- Infected
(turn d Left, numInfections+1)
let (r, c) = c
burst ((r + dr, c + dc), newdir) (currentBurst + 1) newinfections
burst (carrier, dir) 0 0
let execute() =
let path = "day22/day22_input.txt"
let content = LocalHelper.GetLinesFromFile path
let map = parseContent content
let (initcarrier, dir) = ((0, 0), Up)
startBurst map (initcarrier, dir) 10000