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 pathDay2.fsx
More file actions
58 lines (49 loc) · 1.22 KB
/
Day2.fsx
File metadata and controls
58 lines (49 loc) · 1.22 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
open System
let (|Prefix|_|) (p: string) (s: string) =
if s.StartsWith(p) then
Some(s.Substring(p.Length))
else
None
type OpCode =
| Forward of int64
| Up of int64
| Down of int64
let parse s =
match s with
| Prefix "forward " num -> Forward(Int64.Parse num)
| Prefix "up " num -> Up(Int64.Parse num)
| Prefix "down " num -> Down(Int64.Parse num)
| _ -> failwith "failed to parse"
let input =
IO.File.ReadAllLines "Day2.txt" |> Seq.map parse
let depth =
input
|> Seq.fold
(fun acc c ->
acc
+ match c with
| Up num -> (num * -1L)
| Down num -> num
| _ -> 0L)
0L
let horizontal =
input
|> Seq.fold
(fun acc c ->
acc
+ match c with
| Forward num -> num
| _ -> 0L)
0L
let problem1 = depth * horizontal
let depth2 =
input
|> Seq.fold
(fun (aim, currDepth) c ->
match c with
| Up num -> (aim - num, currDepth)
| Down num -> (aim + num, currDepth)
| Forward num -> (aim, currDepth + aim * num))
(0L, 0L)
|> snd
let problem2 = horizontal * depth2