32 lines
913 B
Forth
32 lines
913 B
Forth
module advent_of_code.tasks.day_5.Star1
|
|
|
|
open System
|
|
open System.IO
|
|
|
|
let stream = new StreamReader("tasks/day-5/input.txt")
|
|
|
|
let rec buildFresh() =
|
|
let line = stream.ReadLine()
|
|
if line.Length = 0 then
|
|
List.empty
|
|
else
|
|
let splitLine = line.Split("-")
|
|
let low, high = splitLine[0] |> int64, splitLine[1] |> int64
|
|
List.append [ (low, high) ] (buildFresh())
|
|
|
|
let isFresh (fruit: int64) (range: int64 * int64) =
|
|
let low, high = range.Deconstruct()
|
|
if (fruit >= low && fruit <= high) then 1 else 0
|
|
|
|
let rec checkFresh (freshList: (int64 * int64) list) =
|
|
if stream.EndOfStream then
|
|
0
|
|
else
|
|
let fruit = stream.ReadLine() |> int64
|
|
let freshness = List.sum (freshList |> List.map (isFresh fruit))
|
|
(if freshness = 0 then 0 else 1) + checkFresh freshList
|
|
|
|
let main =
|
|
let freshSet = List.sort (buildFresh())
|
|
checkFresh freshSet
|