-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22_part02.fs
87 lines (74 loc) · 2.6 KB
/
day22_part02.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module day22_part02
open System.Text.RegularExpressions
open AdventOfCode_2016.Modules
open AdventOfCode_Utilities
type FilePos = {
Row: int
Col: int
}
type FileType = {
Pos: FilePos
Size: int
Used: int
Avail: int
Use: int
}
let parseContent(files: string array) =
files
|> Array.skip(2)
|> Array.map(fun f ->
let regexpattern = @"(\d+).*?(\d+).*?(\d+).*?(\d+).*?(\d+).*?(\d+)"
let m' = Regex.Match(f, regexpattern)
{
Pos = { Row = (int)(m'.Groups[2].Value); Col = (int)(m'.Groups[1].Value) };
Size = (int)(m'.Groups[3].Value);
Used = (int)(m'.Groups[4].Value);
Avail = (int)(m'.Groups[5].Value);
Use = (int)(m'.Groups[6].Value)
}
)
let findValidPairs(files: FileType array) =
let possibleComb =
Utilities.comb 2 (files |> List.ofArray)
|> List.filter(fun f ->
(f.Item(0).Used > 0 &&
f.Item(1).Avail >= f.Item(0).Used)
||
(f.Item(1).Used > 0 &&
f.Item(0).Avail >= f.Item(1).Used)
)
possibleComb.Length
let buildFileMap(files: FileType array) =
let emptyFile = files |> Array.find(fun f -> f.Used = 0)
let walls = files |> Array.filter(fun f -> f.Use > 90)
let maxCol = files |> Array.maxBy(fun f -> f.Pos.Col)
let maxRow = files |> Array.maxBy(fun f -> f.Pos.Row)
let filemap = Array2D.create (maxRow.Pos.Row+1) (maxCol.Pos.Col+1) "."
//let maxRows = filemap.GetLength(0)
//let maxCols = filemap.GetLength(1)
files
|> Array.iter(fun f ->
if f.Use > 90 then
filemap[f.Pos.Row, f.Pos.Col] <- "#"
elif f.Used = 0 then
filemap[f.Pos.Row, f.Pos.Col] <- "_"
)
//for row in 0..maxRows-1 do
// for col in 0..maxCols-1 do
// printf "%s" filemap[row, col]
// printfn ""
// resolved with observation of the map
let mostleftwall = walls |> Array.minBy(fun w -> w.Pos.Col)
let movesfromempty = (emptyFile.Pos.Col - mostleftwall.Pos.Col) + 1 // required to surpass the wall
let movestotop = emptyFile.Pos.Row
let movestogoal = maxCol.Pos.Col - mostleftwall.Pos.Col
// https://en.wikipedia.org/wiki/15_puzzle
// 6 movements to replace one position with another
// repeat X times. where X = topright col - 1
let reps = maxCol.Pos.Col - 1
reps * 5 + movesfromempty + movestotop + movestogoal + 1 // extra for (0,0)
let execute =
let path = "day22/day22_input.txt"
let content = LocalHelper.GetLinesFromFile path
let files = parseContent content
buildFileMap files