This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay5.fsx
More file actions
63 lines (52 loc) · 1.53 KB
/
Day5.fsx
File metadata and controls
63 lines (52 loc) · 1.53 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
#r "nuget: FParsec"
open FParsec
open System
type Parser<'t> = Parser<'t, unit>
let parser : Parser<_> =
pipe4 (pfloat) (pstring "," >>. pfloat) (pstring " -> " >>. pfloat) (pstring "," >>. pfloat)
(fun x1 y1 x2 y2 -> (Convert.ToInt32 x1, Convert.ToInt32 y1), (Convert.ToInt32 x2, Convert.ToInt32 y2))
let parse p str =
match run p str with
| Success (result, _, _) -> result
| Failure (errorMsg, _, _) -> failwith errorMsg
let input =
IO.File.ReadLines "Day5.txt"
|> Seq.map (parse parser)
type LineType =
| Vertical
| Horizontal
| Diagonal
type Line = {
Type : LineType
Points : (int * int) list
}
let allBetween first last =
if first < last then
[first .. last]
else
List.rev [last .. first]
let lines =
input
|> Seq.map (fun line ->
let (x1, y1), (x2, y2) = line
if x1 = x2 then
{Type = Horizontal; Points = allBetween y1 y2 |> List.map (fun y -> (x1, y))}
elif y1 = y2 then
{Type = Vertical; Points = allBetween x1 x2 |> List.map (fun x -> (x, y1))}
else
{Type = Diagonal; Points = List.zip (allBetween x1 x2) (allBetween y1 y2)}
)
let countIntersections lines =
lines
|> Seq.map (fun line -> line.Points)
|> Seq.reduce List.append
|> List.countBy id
|> List.filter (fun (_, count) -> count > 1)
|> List.length
let problem1 =
lines
|> Seq.filter (fun line -> line.Type <> Diagonal)
|> countIntersections
let problem2 =
lines
|> countIntersections