From 6c43ae6078caa02bd28f2e61039e466f83ec0a1d Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Thu, 12 Dec 2019 19:16:59 -0500 Subject: [PATCH] Day 12 - Part 1 --- Advent.csproj.DotSettings | 1 + Day12/Day12.cs | 116 ++++++++++++++++++++++++++++++++++++++ Program.cs | 3 +- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 Day12/Day12.cs diff --git a/Advent.csproj.DotSettings b/Advent.csproj.DotSettings index f36e4a8..f9d7a19 100644 --- a/Advent.csproj.DotSettings +++ b/Advent.csproj.DotSettings @@ -2,6 +2,7 @@ True True True + True True True True diff --git a/Day12/Day12.cs b/Day12/Day12.cs new file mode 100644 index 0000000..9d7585a --- /dev/null +++ b/Day12/Day12.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; + +namespace Advent +{ + public static class Day12 + { + private class Coordinates + { + public Coordinates(int x, int y, int z) + { + X = x; + Y = y; + Z = z; + } + + public int X { get; set; } + public int Y { get; set; } + public int Z { get; set; } + } + + private class Moon + { + public Moon(int x, int y, int z) + { + Position = new Coordinates(x, y, z); + Velocity = new Coordinates(0, 0, 0); + } + + public Coordinates Position { get; } + public Coordinates Velocity { get; } + } + + public static void Execute() + { + /* + + + + + */ + + var moons = new List + { + new Moon(-4, -9, -3) , + new Moon(-13, -11, 0), + new Moon(-17, -7, 15 ), + new Moon(-16, 4, 2 ) + }; + + for (var count = 1; count <= 1000; count++) + { + for (var a = 0; a <= 3; a++) + { + for (var b = a + 1; b <= 3; b++) + { + var moonA = moons[a]; + var moonB = moons[b]; + + if (moonA.Position.X > moonB.Position.X) + { + moonB.Velocity.X++; + moonA.Velocity.X--; + } + else if (moonA.Position.X < moonB.Position.X) + { + moonA.Velocity.X++; + moonB.Velocity.X--; + } + + if (moonA.Position.Y > moonB.Position.Y) + { + moonB.Velocity.Y++; + moonA.Velocity.Y--; + } + else if (moonA.Position.Y < moonB.Position.Y) + { + moonA.Velocity.Y++; + moonB.Velocity.Y--; + } + + if (moonA.Position.Z > moonB.Position.Z) + { + moonB.Velocity.Z++; + moonA.Velocity.Z--; + } + else if (moonA.Position.Z < moonB.Position.Z) + { + moonA.Velocity.Z++; + moonB.Velocity.Z--; + } + } + } + + foreach (var moon in moons) + { + moon.Position.X += moon.Velocity.X; + moon.Position.Y += moon.Velocity.Y; + moon.Position.Z += moon.Velocity.Z; + } + } + + var totalEnergy = 0; + + foreach (var moon in moons) + { + var potentialEnergy = Math.Abs(moon.Position.X) + Math.Abs(moon.Position.Y) + Math.Abs(moon.Position.Z); + var kineticEnergy = Math.Abs(moon.Velocity.X) + Math.Abs(moon.Velocity.Y) + Math.Abs(moon.Velocity.Z); + + totalEnergy += (potentialEnergy * kineticEnergy); + } + + Console.WriteLine(totalEnergy); + } + } +} diff --git a/Program.cs b/Program.cs index 406c8a0..6e5b7d0 100644 --- a/Program.cs +++ b/Program.cs @@ -14,7 +14,8 @@ //Day8.Execute(); //Day9.Execute(); //Day10.Execute(); - Day11.Execute(); + //Day11.Execute(); + Day12.Execute(); } } }