mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-01-13 17:22:15 -05:00
Day 12 - Part 1
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day1/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day10/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day11/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day12/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day2/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day3/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=day4/@EntryIndexedValue">True</s:Boolean>
|
||||
|
||||
116
Day12/Day12.cs
Normal file
116
Day12/Day12.cs
Normal file
@@ -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()
|
||||
{
|
||||
/*
|
||||
<x=-4, y=-9, z=-3>
|
||||
<x=-13, y=-11, z=0>
|
||||
<x=-17, y=-7, z=15>
|
||||
<x=-16, y=4, z=2>
|
||||
*/
|
||||
|
||||
var moons = new List<Moon>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,8 @@
|
||||
//Day8.Execute();
|
||||
//Day9.Execute();
|
||||
//Day10.Execute();
|
||||
Day11.Execute();
|
||||
//Day11.Execute();
|
||||
Day12.Execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user