-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day01.fs
49 lines (39 loc) · 1.28 KB
/
Day01.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
module Year2016Day01
open AdventOfCode.FSharp.Common
type Dir = Left | Right
let parseDir (dir : string) =
if dir.[0] = 'L' then
Left, int dir.[1..]
else
Right, int dir.[1..]
let parse = parseFirstLine (splitBy ", " (Array.map parseDir))
let changeDir dir (dx, dy, px, py) =
let ndx, ndy =
if dir = Left then
(dy, -dx)
else
(-dy, dx)
(ndx, ndy, px, py)
let move dist (dx, dy, px, py) =
(dx, dy, px+dx*dist, py+dy*dist)
let goDir cur (dir, dist) =
cur |> changeDir dir |> move dist
let solvePart1 data =
let _, _, x, y = Seq.fold goDir (0, -1, 0, 0) data
abs x + abs y
let solvePart2 data =
let rec goDir' cur seen steps data =
let _, _, px, py = cur
if Set.contains (px, py) seen then
abs px + abs py
else
if steps > 0 then
goDir' (move 1 cur) (Set.add (px, py) seen) (steps - 1) data
else
match data with
| (dir, dist) :: ds ->
let newPos = changeDir dir cur
goDir' newPos seen (dist) ds
| [] -> abs px + abs py
goDir' (0, -1, 0, 0) Set.empty 0 (data |> Array.toList)
let solver = { parse = parse; part1 = solvePart1; part2 = solvePart2 }