-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day08.fs
55 lines (46 loc) · 1.74 KB
/
Day08.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
module Year2015Day08
open AdventOfCode.FSharp.Common
let parse = parseEachLine asString
let getStrLength str =
let l = String.length str
let rec parsePosition (count : int) i =
if i >= l then count
else
let isLast = i = l - 1
match str.[i] with
| '\\' ->
if isLast then count + 1
else
match str.[i + 1] with
| '\\' -> parsePosition (count + 1) (i + 2)
| 'x' when i <= l - 4 -> parsePosition (count + 1) (i + 4)
| '"' -> parsePosition (count + 1) (i + 2)
| _ -> parsePosition (count + 1) (i + 1)
| '"' -> parsePosition count (i + 1)
| _ -> parsePosition (count + 1) (i + 1)
parsePosition 0 0
let encodeString str =
let l = String.length str
let rec parsePosition (count : int) i =
if i >= l then count
else
let isLast = i = l - 1
match str.[i] with
| '\\' ->
if isLast then count + 1
else
match str.[i + 1] with
| '\\' -> parsePosition (count + 4) (i + 2)
| 'x' when i <= l - 4 -> parsePosition (count + 5) (i + 4)
| '"' -> parsePosition (count + 4) (i + 2)
| _ -> parsePosition (count + 1) (i + 1)
| '"' -> parsePosition (count + 2) (i + 1)
| _ -> parsePosition (count + 1) (i + 1)
parsePosition 2 0
let solvePart1 lines =
lines
|> Seq.sumBy (fun str -> String.length str - getStrLength str)
let solvePart2 lines =
lines
|> Seq.sumBy (fun str -> encodeString str - String.length str)
let solver = { parse = parse; part1 = solvePart1; part2 = solvePart2 }