-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08_part01.fs
53 lines (47 loc) · 1.69 KB
/
day08_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
module day08_part01
open System
open System.Text.RegularExpressions
open AdventOfCode_2015.Modules
open System.Globalization
type StringData = {
numOfChars: int
numOfDataChars: int
}
let rec replaceHex(original: string) (hexValues: string list) =
match hexValues with
| [] -> original
| head::tail ->
let replaced = Int32.Parse(head.Substring(2, 2), NumberStyles.AllowHexSpecifier) |> char |> string
let escapedQuotes = original.Replace(head, replaced)
replaceHex escapedQuotes tail
let countCharsData(input: string) =
let oldq = $"\\\""
let replacedq = $"\""
let escapedQuotes = input.Replace(oldq, replacedq).Replace("\\\\", "\\")
let hexValuesregex = "\\\x[0-9a-fA-F]{2}"
if escapedQuotes.Contains("\\x") then
let foundValues = Regex.Matches(escapedQuotes, hexValuesregex)
if foundValues.Count > 0 then
let hexValues = foundValues |> Seq.cast<Match> |> Seq.map (fun m -> m.Value)
let replacedHex = replaceHex escapedQuotes (hexValues |> List.ofSeq)
replacedHex.Length - 2
else
escapedQuotes.Length - 2
else
escapedQuotes.Length - 2
let parseInput (input: string list) =
let stringDatas =
seq {
for line in input do
let nChars = line.Length
yield {
numOfChars = nChars
numOfDataChars = countCharsData line
}
}
stringDatas |> List.ofSeq
let execute =
let path = "day08/day08_input.txt"
let lines = LocalHelper.GetLinesFromFile path |> List.ofSeq
let codes = parseInput lines
codes |> List.sumBy (fun c -> c.numOfChars - c.numOfDataChars)