-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04_part01.fs
39 lines (29 loc) · 1.57 KB
/
day04_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
module day04_part01
open System
open System.IO
let path = "day04_input.txt"
let inputLines = File.ReadAllText(__SOURCE_DIRECTORY__ + @"../../" + path)
let splitStringBySeparator (content: string) (separator: string) =
content.Split([|separator|], StringSplitOptions.None)
let buildBoard (content: string) =
let rows = splitStringBySeparator content "\r\n"
rows |> Array.map (fun r -> r.Split(' ') |> Array.filter(fun r -> r <> "") |> Array.map int)
let playBoard(value:int, board: int[][]) =
board |> Array.map(fun b -> b |> Array.map(fun bb -> if bb = value then -1 else bb))
let IsWinningBoard(board: int[][]) =
(board |> Array.exists(fun r -> r |> Array.forall(fun a -> a = -1))) ||
[0..board.[0].Length - 1] |> List.exists(fun i -> (board |> Array.forall(fun b -> b.[i] = -1)))
let getBoardScore(board: int[][]) =
board |> Array.sumBy(fun x -> x |> Array.filter(fun v -> v <> -1) |> Array.sum)
let rec round (values:int list, listOfBoards: int[][] list) =
let newBoards = listOfBoards |> List.map (fun b -> playBoard(values.Head, b))
match newBoards |> List.filter(fun b -> IsWinningBoard(b)) with
| [x] -> (x, values.Head)
| _ -> round(values.Tail, newBoards)
let execute =
let inputParts = splitStringBySeparator inputLines "\r\n\r\n" |> Array.toList
let playingnumbers = inputParts.Head.Split([|','|]) |> Array.map int |> Array.toList
let boards = inputParts.Tail |> List.map (fun p -> buildBoard p)
let winningBoard = round(playingnumbers, boards)
let score = getBoardScore(fst winningBoard)
(score*snd winningBoard)