-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13_part01.fs
61 lines (53 loc) · 2.31 KB
/
day13_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module day13_part01
open System.IO
open AdventOfCode_Utilities
let createBoard(coords: int[] list) =
let maxY = 1500//((coords |> List.sortByDescending(fun c -> c.[1])) |> List.head).[1] + 1
let maxX = 1500//((coords |> List.sortByDescending(fun c -> c.[0])) |> List.head).[0] + 1
let myboard = Array2D.create maxY maxX "."
coords |> List.iter(fun c -> myboard[c.[1], c.[0]] <- "#")
myboard
let foldBoard(myboard: string[,], fold: string * int) =
let newBoard =
match (fst fold) with
| "y" ->
Array2D.create (snd fold) (myboard.GetLength 1) ""
| "x" ->
Array2D.create (myboard.GetLength 0) (snd fold) ""
| _ -> Array2D.create 0 0 ""
for row in [0..(newBoard.GetLength(0) - 1)] do
for col in [0..(newBoard.GetLength(1) - 1)] do
let mirrored =
match (fst fold) with
| "y" ->
let mRow = (snd fold) * 2 - row
let mCol = col
myboard[mRow, mCol]
| "x" ->
let mRow = row
let mCol = (snd fold) * 2 - col
myboard[mRow, mCol]
| _ -> ""
newBoard[row, col] <-
if mirrored = "#" then
mirrored
else
myboard[row, col]
newBoard
let rec fold(myboard: string[,], folds: (string * int) list) =
match folds with
| [] -> myboard
| x::xs ->
let folded = foldBoard(myboard, x)
fold(folded, xs)
let execute =
let path = "day13_input.txt"
let instructionsParts =
File.ReadLines(__SOURCE_DIRECTORY__ + @"../../" + path) |> Seq.toList
let parts = instructionsParts |> List.splitAt(instructionsParts |> List.findIndex(fun l -> l = ""))
let coordinates = (fst parts) |> List.map(fun c -> [|c.Split(',').[0] |> int; c.Split(',').[1] |> int|])
let folding = (snd parts).Tail |> List.map(fun i -> (i.Split('=').[0].Substring(i.Split('=').[0].Length - 1, 1), i.Split('=').[1] |> int))
let board = createBoard coordinates
let firstFolding = foldBoard(board, folding.Item(0))
let tBoard1 = Utilities.toJagged firstFolding
(tBoard1 |> Array.map(fun e -> e |> Array.filter(fun ee -> ee = "#") |> Array.length)) |> Array.sum