-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.fs
42 lines (34 loc) · 1.1 KB
/
Day2.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
module AoC2021.Day2
open AoC2021.Utils
type Position = { X: int; Depth: int; Aim: int }
let move position instruction =
match instruction with
| [| "forward"; n |] -> { position with X = position.X + int n }
| [| "down"; n |] ->
{ position with
Depth = position.Depth + int n }
| [| "up"; n |] ->
{ position with
Depth = position.Depth - int n }
let move2 position instruction =
match instruction with
| [| "forward"; n |] ->
{ position with
X = position.X + int n
Depth = position.Depth + position.Aim * int n }
| [| "down"; n |] ->
{ position with
Aim = position.Aim + int n }
| [| "up"; n |] ->
{ position with
Aim = position.Aim - int n }
let solve mover fn =
let input =
readInput fn
|> Seq.map (fun (s: string) -> s.Split ' ')
let finalPosition =
input
|> Seq.fold mover { X = 0; Depth = 0; Aim = 0 }
finalPosition.X * finalPosition.Depth |> int64
let day2 fn () = solve move fn
let day2part2 fn () = solve move2 fn