This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle03.fs
78 lines (58 loc) · 1.73 KB
/
puzzle03.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
//
// https://adventofcode.com/2020/day/3
//
#if !INTERACTIVE
module Puzzle3
#else
#load "common.fs"
#endif
open System.IO
open common
module Slope =
[<Struct>]
type Cell =
| Empty
| Tree
type Location =
| In of Cell
| Out
type Slope = private T of Cell [,]
let private unwrap =
function
| T cells -> cells
let create matrix =
matrix
|> Array2D.map (fun c -> match c with | '#' -> Tree | _ -> Empty)
|> T
let checkLocation (slope: Slope) x y =
let cells = unwrap slope
if (y < 0 || y >= Array2D.length1 cells) then
Out
else
Array2D.get cells y (x % Array2D.length2 cells)
|> In
let move (fromX, fromY) (right, down) = fromX + right, fromY + down
let moves (fromX, fromY) (right, down) =
let generateMove (stateX, stateY) =
let newPoint = move (stateX, stateY) (right, down)
Some (newPoint, newPoint)
Seq.unfold generateMove (fromX, fromY)
let startPoint = 0, 0
let countTrees slope (right, down) =
moves startPoint (right, down)
|> Seq.map (fun (x, y) -> Slope.checkLocation slope x y)
|> Seq.takeWhile ((<>) Slope.Out)
|> Seq.filter ((=) (Slope.In Slope.Tree))
|> Seq.length
let slope =
Path.Combine(__SOURCE_DIRECTORY__, "puzzle03.txt")
|> read2d
|> Slope.create
let puzzle3_1 =
countTrees slope (3, 1)
let puzzle3_2 =
(countTrees slope (1, 1) |> int64)
* (countTrees slope (3, 1) |> int64)
* (countTrees slope (5, 1) |> int64)
* (countTrees slope (7, 1) |> int64)
* (countTrees slope (1, 2) |> int64)