quest 01 complete
This commit is contained in:
@@ -6,6 +6,21 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
||||||
|
<Content Include="Inputs\Quest01\Q01_P01.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
|
||||||
|
<Content Include="Inputs\Quest01\Q01_P02.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
|
||||||
|
<Content Include="Inputs\Quest01\Q01_P03.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
|
||||||
|
<Compile Include="Helpers.fs" />
|
||||||
|
<Compile Include="Quest01.fs" />
|
||||||
<Compile Include="Program.fs"/>
|
<Compile Include="Program.fs"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -1,2 +1,13 @@
|
|||||||
// For more information see https://aka.ms/fsharp-console-apps
|
// For more information see https://aka.ms/fsharp-console-apps
|
||||||
printfn "Hello from F#"
|
open EveryBodyCodes2025
|
||||||
|
|
||||||
|
[<EntryPoint>]
|
||||||
|
let main argv =
|
||||||
|
|
||||||
|
try
|
||||||
|
printfn $"{Quest01.part1()}"
|
||||||
|
printfn $"{Quest01.part2()}"
|
||||||
|
printfn $"{Quest01.part3()}"
|
||||||
|
with exn ->
|
||||||
|
printfn $"{exn}"
|
||||||
|
0
|
||||||
@@ -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"
|
||||||
Binary file not shown.
Reference in New Issue
Block a user