diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/EveryBodyCodes2025.fsproj b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/EveryBodyCodes2025.fsproj index dadc839..c8e8612 100644 --- a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/EveryBodyCodes2025.fsproj +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/EveryBodyCodes2025.fsproj @@ -6,6 +6,21 @@ + + + PreserveNewest + + + + PreserveNewest + + + + PreserveNewest + + + + diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Helpers.fs b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Helpers.fs new file mode 100644 index 0000000..654cddd --- /dev/null +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Helpers.fs @@ -0,0 +1,18 @@ +module EveryBodyCodes2025.Helpers + +let foldUntilNone folder (state: 's) (source: 'a seq) : 's option = + use e = source.GetEnumerator() + let rec loop acc = + if e.MoveNext() then + match folder acc e.Current with + | Some s -> loop s + | None -> Some acc // last good state + else Some acc // finished entire sequence + loop state + +let arraySwap idx0 idx1 (array: 'a array) = + let a = array[idx0] + let b = array[idx1] + array[idx0] <- b + array[idx1] <- a + array diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P01.txt b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P01.txt new file mode 100644 index 0000000..f7ea762 --- /dev/null +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P01.txt @@ -0,0 +1,3 @@ +Krynnfal,Felnnyn,Norakmarn,Glynngnaris,Zorasis,Ulmarsarix,Rynquin,Wyntor,Harnmarn,Selkvel + +L6,R8,L7,R1,L2,R7,L1,R6,L5,R2,L7 \ No newline at end of file diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P02.txt b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P02.txt new file mode 100644 index 0000000..c4bd169 --- /dev/null +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P02.txt @@ -0,0 +1,3 @@ +Ascalthar,Skarkael,Vyrbel,Havisis,Tarlzral,Paltheldrith,Hazthel,Hazwyris,Jarzyth,Mornxal,Vaelacris,Xardaros,Lirzyph,Sylacris,Nexor,Vorndra,Cyndhynd,Ralkyris,Cragkael,Galoryn + +L7,R12,L8,R12,L9,R18,L16,R13,L9,R12,L5,R11,L5,R14,L5,R18,L5,R15,L5,R17,L5,R18,L16,R12,L14,R7,L11,R9,L12 \ No newline at end of file diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P03.txt b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P03.txt new file mode 100644 index 0000000..e21ffa8 --- /dev/null +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Inputs/Quest01/Q01_P03.txt @@ -0,0 +1,3 @@ +Xarsar,Elvareldrin,Zraalcyth,Vyrlfeth,Palthonar,Maralor,Qalirin,Dalnyn,Havryn,Maralxal,Ulthyn,Ascalxeth,Thymfyr,Varinralis,Rythangarath,Ulkkynar,Lazlon,Grimulrix,Draithvash,Voraxmir,Vyrlxal,Elvarmal,Maralsyx,Wyrvash,Vornketh,Torendris,Thalcoryx,Paldeth,Vyrlcion,Tazroth + +L25,R49,L14,R13,L11,R12,L43,R45,L38,R34,L34,R44,L44,R35,L35,R30,L38,R37,L49,R48,L5,R39,L5,R27,L5,R14,L5,R32,L5,R46,L5,R9,L5,R24,L5,R43,L5,R21,L5,R22,L15,R49,L27,R41,L18,R31,L26,R16,L10,R40,L19,R6,L20,R24,L40,R44,L5,R47,L35 \ No newline at end of file diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Program.fs b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Program.fs index da1fbd9..8e426a8 100644 --- a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Program.fs +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Program.fs @@ -1,2 +1,13 @@ // For more information see https://aka.ms/fsharp-console-apps -printfn "Hello from F#" \ No newline at end of file +open EveryBodyCodes2025 + +[] +let main argv = + + try + printfn $"{Quest01.part1()}" + printfn $"{Quest01.part2()}" + printfn $"{Quest01.part3()}" + with exn -> + printfn $"{exn}" + 0 \ No newline at end of file diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Quest01.fs b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Quest01.fs new file mode 100644 index 0000000..11960f9 --- /dev/null +++ b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes2025/Quest01.fs @@ -0,0 +1,81 @@ +module EveryBodyCodes2025.Quest01 + +open System +open System.IO +open EveryBodyCodes2025.Helpers + +type Direction = Left | Right +type Instruction = { Direction: Direction; Distance: int } +let parseInstruction (segment:string) = + let direction = if segment[0] = 'L' then Left else Right + let distance = Int32.Parse(segment.AsSpan().Slice(1)) + { Direction = direction; Distance = distance } + +let inline normalize (array: 'a array) idx = + if idx >= 0 then idx % array.Length else array.Length - (-idx % array.Length) + +let inline nextIndex instruction idx = + match instruction.Direction with + | Left -> idx - instruction.Distance + | Right -> idx + instruction.Distance + +let readFile file = + File.ReadAllLines(file) + |> fun lines -> + let names = lines.[0] |> _.Split(",", StringSplitOptions.RemoveEmptyEntries ||| StringSplitOptions.TrimEntries) + let instructions = + lines[2] + |> _.Split(",", StringSplitOptions.RemoveEmptyEntries ||| StringSplitOptions.TrimEntries) + |> Array.map parseInstruction + (names, instructions) + +let part1File file : string = + readFile file + |> fun (names, instructions) -> + instructions + |> Seq.fold (fun (idx, _) instruction -> + let next = nextIndex instruction idx |> fun v -> Math.Clamp(v, 0, names.Length - 1) + + match Array.tryItem next names with + | Some n -> next, n + | None -> failwith "off array" + ) (0, "") + |> snd + +let part1 () = part1File "Inputs/Quest01/Q01_P01.txt" + +let part2File file = + readFile file + |> fun (names, instructions) -> + instructions + |> Seq.fold (fun (idx, _) instruction -> + let next = nextIndex instruction idx |> normalize names + match Array.tryItem next names with + | Some n -> next, n + | None -> failwith "off array" + ) (0, "") + |> snd + +let part2() = part2File "Inputs/Quest01/Q01_P02.txt" + +let part3File file = + readFile file + |> fun (names, instructions) -> + // need to preserve a reference to an array that can be mutated + let mutable nameArray = names + instructions + |> Seq.fold (fun _ instruction -> + // for this challenge the index doesn't matter + let next = match instruction.Direction with + | Left -> names.Length - instruction.Distance + | Right -> instruction.Distance + |> normalize names + match Array.tryItem next nameArray with + | Some n -> + nameArray <- arraySwap 0 next names + 0, n + | None -> failwith "off array" + ) (0, "") + |> snd + +let part3() = part3File "Inputs/Quest01/Q01_P03.txt" \ No newline at end of file diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.dll b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.dll index c898d64..8fcea48 100644 Binary files a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.dll and b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.dll differ diff --git a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.pdb b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.pdb index ba4d769..05dadb1 100644 Binary files a/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.pdb and b/everybody-codes/2025/EveryBodyCodes2025/EveryBodyCodes_WrongOne/bin/Debug/net9.0/EveryBodyCodes_WrongOne.pdb differ