Created
December 6, 2023 06:26
-
-
Save object/4748b34323b189581a2c6ac72b1383c1 to your computer and use it in GitHub Desktop.
Advent of Code 2023, December 6
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/input06.txt") | |
| let rec getWinningMoves time record_distance speed acc = | |
| if speed > time then | |
| acc | |
| else | |
| let distance = speed * (time - speed) | |
| if distance > record_distance then | |
| getWinningMoves time record_distance (speed+1L) (acc+1L) | |
| else | |
| getWinningMoves time record_distance (speed+1L) acc | |
| // Part One | |
| let races = | |
| input | |
| |> Array.map (fun str -> (str.Split ':' |> Array.last).Trim().Split ' ' | |
| |> Array.filter (not << String.IsNullOrWhiteSpace) | |
| |> Array.map Int64.Parse) | |
| Array.zip races[0] races[1] | |
| |> Array.map (fun (time, distance) -> getWinningMoves time distance 0L 0L) | |
| |> Array.fold (fun v acc -> v * acc) 1L | |
| // Part Two | |
| let race = | |
| input | |
| |> Array.map (fun str -> (str.Split ':' |> Array.last).Trim()) | |
| |> Array.map (fun str -> str |> Array.ofSeq |> Array.filter (fun c -> c <> ' ')) | |
| |> Array.map (fun chars -> String(chars)) | |
| |> Array.map Int64.Parse | |
| getWinningMoves race[0] race[1] 0L 0L |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment