42 lines
1.2 KiB
Forth
42 lines
1.2 KiB
Forth
module advent_of_code.tasks.day_2.Star2
|
|
|
|
open System.IO
|
|
|
|
let stream = new StreamReader("tasks/day-2/input.txt")
|
|
|
|
let convertToString (i: int64) = i |> string
|
|
|
|
let divideString (str: string) (n: int) =
|
|
let count = str.Length / n
|
|
let mutable parts = Set.empty
|
|
for i in 0 .. count - 1 do
|
|
let start = i * n
|
|
parts <- parts.Add(str.Substring(start, n))
|
|
parts
|
|
|
|
let rec isInvalid (id: string) (chars: int) =
|
|
let biggerSlices = if chars = id.Length then 0 |> int64 else isInvalid id (chars + 1)
|
|
if (chars = id.Length) then
|
|
0
|
|
else if (biggerSlices <> 0) then
|
|
biggerSlices
|
|
else if (id.Length % chars <> 0) then
|
|
0
|
|
else
|
|
let parts = divideString id chars
|
|
if parts.Count = 1 then id |> int64 else 0
|
|
|
|
let checkValid (id: string) = isInvalid id 1
|
|
|
|
let rec getInvalid (lines: string list) =
|
|
let line, tail = lines.Head, lines.Tail
|
|
let els = line.Split("-")
|
|
let low, high = els[0] |> int64, els[1] |> int64
|
|
let invalids = [low .. high] |> List.map convertToString |> List.map checkValid
|
|
|
|
List.sum invalids + if tail.IsEmpty then 0 |> int64 else getInvalid tail
|
|
|
|
let main =
|
|
let lines = stream.ReadLine().Split(",") |> Array.toList
|
|
getInvalid lines
|