Last active
December 5, 2025 06:25
-
-
Save object/53e037e42b747074073aaac70306f08a to your computer and use it in GitHub Desktop.
AdventOfCode2025.Day05.fsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #time "on" | |
| open System | |
| open System.IO | |
| let input = | |
| File.ReadAllLines(__SOURCE_DIRECTORY__ + "/../data/input05.txt") | |
| |> Seq.toList | |
| let ids = input |> List.skipWhile(fun line -> line <> "") |> List.tail |> List.map Int64.Parse | |
| let ranges = input |> List.take(input.Length - ids.Length - 1) |> List.map (fun line -> | |
| let items = line.Split('-') |> Array.map Int64.Parse | |
| (items[0], items[1])) | |
| // Part One | |
| ids | |
| |> List.fold(fun acc id -> | |
| let fresh = ranges |> List.fold(fun fresh (start,stop) -> | |
| if id >= start && id <= stop then true else fresh) false | |
| if fresh then acc + 1L else acc) 0L | |
| // Part Two | |
| let rec merge_ranges acc ranges = | |
| match ranges with | |
| | [] -> acc | |
| | (start, stop) :: [] -> (start, stop) :: acc | |
| | (start1, stop1) :: (start2, stop2) :: ranges -> | |
| if start2 <= stop1 + 1L then | |
| merge_ranges acc ((start1, max stop1 stop2) :: ranges) | |
| else | |
| merge_ranges ((start1, stop1) :: acc) ((start2, stop2) :: ranges) | |
| ranges | |
| |> List.sortBy fst | |
| |> merge_ranges [] | |
| |> List.fold(fun acc (start, stop) -> acc + (stop - start + 1L)) 0L |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment