This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
generated from mazharenko/aoc-agent-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.fs
61 lines (50 loc) · 1.74 KB
/
day18.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
module impl.day18
#nowarn "25"
open Farkle
open Farkle.Builder
open Farkle.Builder.Regex
let private down = Point (1L,0L)
let private up = Point (-1L,0L)
let private right = Point (0L,1L)
let private left = Point (0L,-1L)
let private direction = "Direction" ||= [
!& "R" =% right
!& "D" =% down
!& "L" =% left
!& "U" =% up
]
let private steps = Terminals.int "Steps"
module Part1 =
let private digInstruction = "Dig" ||= [
!@ direction .>>. steps .>> "(" .>> (any |> atLeast 1 |> terminalU "")
=> fun dir steps -> dir * int64 steps
]
let parse input =
let parser = RuntimeFarkle.build digInstruction
input
|> Pattern1.read (RuntimeFarkle.parseUnsafe parser)
module Part2 =
let parse input =
let digInstruction = "Dig" ||= [
!@ direction .>>. steps .>> "(" .>>. (regexString "[#][0-9a-f]{6}" |> terminal "Color" (T(fun _ c -> new string(c)))) .>> ")"
=> fun _ _ color ->
let dir = match color[^0] with | '0' -> right | '1' -> down | '2' -> left | '3' -> up
let steps = System.Convert.ToInt32(color[1..^1], 16)
dir * int64 steps
]
let parser = RuntimeFarkle.build digInstruction
input
|> Pattern1.read (RuntimeFarkle.parseUnsafe parser)
let solve input =
let points =
input
|> Array.scan (+) (Point(0L,0L))
let p = input |> Array.map (fun p -> int64 <| abs (Point.x p + Point.y p)) |> Array.sum
// shoelace formula
let area =
abs (
Array.pairwise points
|> Array.sumBy (fun (p1, p2) -> (Point.x p1) * (Point.y p2) - (Point.y p1) * (Point.x p2))
) / 2L
// Pick's theorem
area - p/2L + 1L + p