-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.fs
56 lines (46 loc) · 1.28 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace Adventofcode2021
module Day2 =
[<Literal>]
let InputFile = "Day2Input.txt"
type Command =
| Forward of int
| Down of int
| Up of int
let parseLine (s: string) =
let c = s.Split(' ')
let x = int c[1]
match c[0] with
| "forward" -> Forward x
| "down" -> Down x
| "up" -> Up x
| _ -> failwith $"unknown command {s}"
let doCommand c (h, d) =
match c with
| Forward x -> (h + x, d)
| Down x -> (h, d + x)
| Up x -> (h, d - x)
let rec drive f pos cmds =
match cmds with
| c :: rest ->
let pos' = f c pos
drive f pos' rest
| [] -> pos
let day2 () =
InputFile
|> System.IO.File.ReadAllLines
|> Array.map parseLine
|> List.ofArray
|> drive doCommand (0, 0)
|> fun (h, d) -> h * d
let doCommandPart2 c (h, d, a) =
match c with
| Forward x -> (h + x, d + a * x, a)
| Down x -> (h, d, a + x)
| Up x -> (h, d, a - x)
let day2Part2 () =
InputFile
|> System.IO.File.ReadAllLines
|> Array.map parseLine
|> List.ofArray
|> drive doCommandPart2 (0, 0, 0)
|> fun (h, d, _) -> h * d