-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay4.fs
More file actions
54 lines (42 loc) · 1.66 KB
/
Day4.fs
File metadata and controls
54 lines (42 loc) · 1.66 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
module Day4
open System.IO
open Common.Grid
let paperRollIsAccessible (floor: Grid<char>) location =
location
|> eightWayNeighbors floor
|> Array.filter (fun neighbor -> (get floor neighbor) = '@')
|> Array.length < 4
let accessiblePaperRolls (floor: Grid<char>) =
floor
|> iter
|> Seq.toArray
|> Array.Parallel.filter (fun loc -> (get floor loc) = '@' && (paperRollIsAccessible floor loc))
let countAccessiblePaperRolls (floor: Grid<char>) =
floor |> accessiblePaperRolls |> _.Length
let removePaperRolls (floor: Grid<char>) (rolls: (int * int) array) : unit =
for x, y in rolls do
floor[y][x] <- '.'
let countRemovedPaperRolls (floor: Grid<char>) =
let mutable rollsToRemove = accessiblePaperRolls floor
let mutable rollsRemoved = rollsToRemove.Length
while rollsToRemove.Length > 0 do
removePaperRolls floor rollsToRemove
rollsToRemove <- accessiblePaperRolls floor
rollsRemoved <- rollsRemoved + rollsToRemove.Length
rollsRemoved
let parse filename =
filename |> File.ReadAllLines |> Array.map _.ToCharArray()
module Tests =
open Xunit
[<Theory>]
[<InlineData("Inputs/Day4/test.txt", 13)>]
[<InlineData("Inputs/Day4/input.txt", 1416)>]
let ``Part 1: The number of accessible paper rolls`` (filename: string, expected: int) =
let result = filename |> parse |> countAccessiblePaperRolls
Assert.Equal(expected, result)
[<Theory>]
[<InlineData("Inputs/Day4/test.txt", 43)>]
[<InlineData("Inputs/Day4/input.txt", 9086)>]
let ``Part 2: The number of paper rolls which can be removed`` (filename: string, expected: int) =
let result = filename |> parse |> countRemovedPaperRolls
Assert.Equal(expected, result)