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 pathpuzzle10.fs
75 lines (63 loc) · 1.71 KB
/
puzzle10.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
//
// https://adventofcode.com/2020/day/10
//
#if !INTERACTIVE
module Puzzle10
#else
#load "common.fs"
#endif
open System
open System.IO
open common
let input =
Path.Combine(__SOURCE_DIRECTORY__, "puzzle10.txt")
|> readLines
|> Seq.map int
|> Seq.sort
|> Seq.toArray
let max =
input.[input.Length - 1]
let diffs adapters =
let allSeq = seq {
yield 0
yield! adapters
yield max + 3
}
allSeq
|> Seq.pairwise
|> Seq.map (fun (x, y) -> y - x)
let countPaths adapters =
let allSeq =
seq {
yield -10
yield -10 // extra for correct windows
yield 0
yield! adapters
yield max + 3
}
allSeq
|> Seq.windowed 4
|> Seq.fold (
// (-10 -> -10 -> 0) -> 1 -> 4 -> 5 -> 6 -> 7 -> 10 -> 11 -> 12 -> 15 -> 16 -> 19
// 1 1 1 1 1 2 4 4 4 8 8 8 8
// \_______________? \___________? \_______?
fun paths window ->
let current = window.[3]
let pathsToCurrent =
window
|> Array.map (fun x -> current - x)
|> Seq.zip paths
|> Seq.where (fun (_, distance) -> distance <= 3)
|> Seq.map fst
|> Seq.sum
[|paths.[1]; paths.[2]; pathsToCurrent|]
) [|1L; 1L; 1L|]
|> Array.last
let puzzle10_1 =
let counts =
diffs input
|> Seq.countBy id
|> Map.ofSeq
counts.[1] * counts.[3]
let puzzle10_2 =
countPaths input