quest 2 complete
This commit is contained in:
@@ -19,9 +19,26 @@
|
|||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
|
||||||
|
<Content Include="Inputs\Quest02\Q02_P01.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
|
||||||
|
<Content Include="Inputs\Quest02\Q02_P02.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
|
||||||
|
<Content Include="Inputs\Quest02\Q02_P03.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
|
||||||
<Compile Include="Helpers.fs" />
|
<Compile Include="Helpers.fs" />
|
||||||
<Compile Include="Quest01.fs" />
|
<Compile Include="Quest01.fs" />
|
||||||
|
<Compile Include="Quest02.fs" />
|
||||||
<Compile Include="Program.fs"/>
|
<Compile Include="Program.fs"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FSharp.Collections.ParallelSeq" Version="1.2.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
A=[154,53]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
A=[-3284,68783]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
A=[-3284,68783]
|
||||||
@@ -5,9 +5,10 @@ open EveryBodyCodes2025
|
|||||||
let main argv =
|
let main argv =
|
||||||
|
|
||||||
try
|
try
|
||||||
printfn $"{Quest01.part1()}"
|
printfn $"{Quest02.part1()}"
|
||||||
printfn $"{Quest01.part2()}"
|
//Quest02.test1
|
||||||
printfn $"{Quest01.part3()}"
|
printfn $"{Quest02.part2()}"
|
||||||
|
printfn $"{Quest02.part3()}"
|
||||||
with exn ->
|
with exn ->
|
||||||
printfn $"{exn}"
|
printfn $"{exn}"
|
||||||
0
|
0
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
module EveryBodyCodes2025.Quest02
|
||||||
|
|
||||||
|
open System.IO
|
||||||
|
open System.Text.RegularExpressions
|
||||||
|
open FSharp.Collections.ParallelSeq
|
||||||
|
open Checked
|
||||||
|
|
||||||
|
[<Struct>]
|
||||||
|
type ComplexNumber =
|
||||||
|
{X: int64; Y: int64}
|
||||||
|
static member (+) (a: ComplexNumber, b: ComplexNumber) = {X = a.X + b.X; Y = a.Y + b.Y }
|
||||||
|
static member (*) (a: ComplexNumber, b: ComplexNumber) = { X = ( a.X * b.X ) - (a.Y * b.Y); Y = (a.X * b.Y) + (a.Y * b.X) }
|
||||||
|
static member (/) (a: ComplexNumber, b: ComplexNumber) = { X = a.X / b.X; Y = a.Y / b.Y }
|
||||||
|
override this.ToString() = $"[{this.X},{this.Y}]"
|
||||||
|
|
||||||
|
let parseInput file =
|
||||||
|
File.ReadAllLines file
|
||||||
|
|> fun lines ->
|
||||||
|
let reg = Regex(@"A=\[(-?\d+),(-?\d+)\]")
|
||||||
|
let res = reg.Match(lines[0])
|
||||||
|
match res.Success with
|
||||||
|
| true ->
|
||||||
|
let x = int64 res.Groups[1].Value
|
||||||
|
let y = int64 res.Groups[2].Value
|
||||||
|
{X = x; Y = y}
|
||||||
|
| false -> failwith "failed to parse"
|
||||||
|
|
||||||
|
|
||||||
|
let part1 () =
|
||||||
|
parseInput "Inputs/Quest02/Q02_P01.txt"
|
||||||
|
|> fun a ->
|
||||||
|
[1..3]
|
||||||
|
|> List.fold (fun acc _ -> (acc * acc) / { X = 10; Y = 10 } + a ) {X = 0; Y = 0}
|
||||||
|
|
||||||
|
let cycle p =
|
||||||
|
let rec iterate current iteration =
|
||||||
|
if iteration = 100 then Some current
|
||||||
|
else
|
||||||
|
let next = (current * current) / {X = 100_000; Y = 100_000 } + p
|
||||||
|
if abs(next.X) > 1_000_000 || abs(next.Y) > 1_000_000 then
|
||||||
|
None
|
||||||
|
else iterate next (iteration + 1)
|
||||||
|
iterate { X = 0; Y = 0 } 0
|
||||||
|
|
||||||
|
let part2 () =
|
||||||
|
parseInput "Inputs/Quest02/Q02_P02.txt"
|
||||||
|
|> fun a ->
|
||||||
|
seq {
|
||||||
|
for y in 0..100 do
|
||||||
|
for x in 0..100 do
|
||||||
|
yield {X = a.X + (int64 x * 10L); Y = a.Y + (int64 y * 10L)}
|
||||||
|
}
|
||||||
|
|> PSeq.choose cycle
|
||||||
|
|> PSeq.distinct
|
||||||
|
|> PSeq.length
|
||||||
|
|
||||||
|
let part3 () =
|
||||||
|
parseInput "Inputs/Quest02/Q02_P03.txt"
|
||||||
|
|> fun a ->
|
||||||
|
seq {
|
||||||
|
for y in 0..1000 do
|
||||||
|
for x in 0..1000 do
|
||||||
|
yield {X = a.X + (int64 x); Y = a.Y + (int64 y)}
|
||||||
|
}
|
||||||
|
|> PSeq.choose cycle
|
||||||
|
|> PSeq.length
|
||||||
Reference in New Issue
Block a user