-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay7.fs
More file actions
68 lines (54 loc) · 2.09 KB
/
Day7.fs
File metadata and controls
68 lines (54 loc) · 2.09 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
module Day7
open System.Collections.Generic
open Common.Functools
type TachyonManifold =
{ start: int * int
diagram: char array array }
module TachyonManifold =
let countTachyonSplits (manifold: TachyonManifold) =
let mutable splits = 0
let frontier = Queue<int * int>([ manifold.start ])
let seen = HashSet<int * int>()
while frontier.Count > 0 do
let r, c = frontier.Dequeue()
if seen.Contains(r, c) |> not then
seen.Add(r, c) |> ignore
let nr = r + 1
if (nr < manifold.diagram.Length && c >= 0 && c < manifold.diagram[nr].Length) then
if manifold.diagram[nr][c] = '^' then
splits <- splits + 1
frontier.Enqueue(nr, c - 1)
frontier.Enqueue(nr, c + 1)
else
frontier.Enqueue(nr, c)
splits
let countPossibleTimelines (manifold: TachyonManifold) =
let rec count (memoizedCount: int * int -> int64) (r, c) =
if r = manifold.diagram.Length then
1L
elif manifold.diagram[r][c] = '^' then
memoizedCount (r, c - 1) + memoizedCount (r, c + 1)
else
memoizedCount (r + 1, c)
memoizeRec count manifold.start
let parse filename : TachyonManifold =
let diagram = System.IO.File.ReadAllLines filename |> Array.map _.ToCharArray()
let startColumn = diagram[0] |> Array.findIndex (fun c -> c = 'S')
{ start = (0, startColumn)
diagram = diagram }
module Tests =
open Xunit
[<Theory>]
[<InlineData("Inputs/Day7/test.txt", 21)>]
[<InlineData("Inputs/Day7/input.txt", 1560)>]
let ``Part 1: The number of times the tachyon beam splits`` (filename: string, expected: int) =
let result = filename |> parse |> TachyonManifold.countTachyonSplits
Assert.Equal(expected, result)
[<Theory>]
[<InlineData("Inputs/Day7/test.txt", 40)>]
[<InlineData("Inputs/Day7/input.txt", 25592971184998L)>]
let ``Part 2: The number of different timelines a single tachyon particle could take``
(filename: string, expected: int64)
=
let result = filename |> parse |> TachyonManifold.countPossibleTimelines
Assert.Equal(expected, result)