-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18_part01.fs
47 lines (39 loc) · 1.65 KB
/
day18_part01.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
module day18_part01
open AdventOfCode_2023.Modules
open System.Text.RegularExpressions
open System
type Direction = UP | RIGHT | DOWN | LEFT
type Instruction = { Direction: Direction; Steps: int; Color: string }
let dirRows = [| -1; 0; 1; 0 |]
let dirCols = [| 0; 1; 0; -1 |]
let digHoleFold (ans: Instruction list) (directions: Direction[]) =
let calcArea (acc: int*int*int*int) (input: Instruction) =
let dIdx = Array.findIndex (fun d -> d = input.Direction) directions
let row, col, border, area = acc
let newRow = row + dirRows[dIdx] * input.Steps
let newCol = col + dirCols[dIdx] * input.Steps
let newArea = area + (row - newRow) * (col + newCol) * 1
let newBorder = border + input.Steps
(newRow, newCol, newBorder, newArea)
let _, _, border, area = List.fold calcArea (0, 0, 0, 0) ans
int ((float)border / 2. + Math.Abs((float)area) / 2. + 1. )
let parseInput (input: string list) =
input
|> List.map (fun l ->
let parts = Regex.Match(l, @"([U|D|L|R])\s([\d]+)\s\(#([0-9a-fA-F]+)\)")
{
Direction =
match parts.Groups.[1].Value with
| "U" -> UP
| "D" -> DOWN
| "L" -> LEFT
| "R" -> RIGHT
| _ -> failwith "Unknown direction"
; Steps = int parts.Groups.[2].Value
; Color = parts.Groups.[3].Value
}
)
let execute =
let path = "day18/day18_input.txt"
let instructions = LocalHelper.ReadLines(path) |> List.ofSeq |> parseInput
digHoleFold instructions [| UP; RIGHT; DOWN; LEFT |]