From 3642e40915efe51c561e87f5109c5cc725c716ab Mon Sep 17 00:00:00 2001 From: Ummon Date: Thu, 21 Dec 2017 10:29:59 +0100 Subject: [PATCH] Fix part 1, replace euclidean norm by manhattan norm. --- AdventOfCode2017/AdventOfCode2017.fsproj | 2 +- AdventOfCode2017/Day20.fs | 7 +++---- AdventOfCode2017/Program.fs | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 0229b06..4cc42dc 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -37,7 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 19 + 20 11 diff --git a/AdventOfCode2017/Day20.fs b/AdventOfCode2017/Day20.fs index 3ab08b0..031fadd 100644 --- a/AdventOfCode2017/Day20.fs +++ b/AdventOfCode2017/Day20.fs @@ -5,7 +5,7 @@ open System type Vec = { X : float; Y : float; Z : float } with - member this.SquareNorm = this.X ** 2. + this.Y ** 2. + this.Z ** 2. + member this.ManhattanNorm = abs this.X + abs this.Y + abs this.Z type Particule = { Pos : Vec; V : Vec; A : Vec } @@ -22,9 +22,8 @@ let parseInput (input : string[]) : Particule[] = } ) -// This is wrong with a manhattan distance :( but works in my case :) let nearestZero (particules : Particule[]) : int = - particules |> Array.indexed |> Array.minBy (fun (_, p) -> p.A.SquareNorm, p.V.SquareNorm, p.Pos.SquareNorm) |> fst + particules |> Array.indexed |> Array.minBy (fun (_, p) -> p.A.ManhattanNorm, p.V.ManhattanNorm, p.Pos.ManhattanNorm) |> fst let collide (p1 : Particule) (p2 : Particule) : int option = // https://www.wolframalpha.com/input/?i=solve+a%2B(b%2Bc%2F2)*t%2B1%2F2*c*t%5E2+-+d-(e%2Bf%2F2)*t-1%2F2*f*t%5E2+%3D+0 @@ -52,7 +51,7 @@ let collide (p1 : Particule) (p2 : Particule) : int option = let tsInt = ts |> List.choose ( fun t -> - let tRound = Math.Round (t, 5) * 10000. |> int + let tRound = Math.Round (t, 4) * 10000. |> int if tRound % 10000 = 0 then Some tRound else None ) diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 170adb9..adf50dd 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -89,7 +89,6 @@ let day19 () = sprintf "part1 = %A, part2 = %A" word length let day20 () = - //let input = File.ReadAllLines "../../Data/day20_ben.input" |> Day20.parseInput let input = File.ReadAllLines "Data/day20.input" |> Day20.parseInput sprintf "part1 = %A, part2 = %A" (Day20.nearestZero input) (Day20.nbAlive input) -- 2.45.2