about summary refs log tree commit diff
path: root/Day12.cs
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2020-12-02 00:36:24 -0500
committerBen Harris <ben@tilde.team>2020-12-02 00:36:24 -0500
commit36bb28af87e81a9b3cab1abd79d11106cd610f6c (patch)
treed4648bba69ceb5e20753acf54df51d74cff4731b /Day12.cs
parent00c3b51b897c33210679de546eebcd9ec75247a1 (diff)
update gitignore and run resharper cleanup
Diffstat (limited to 'Day12.cs')
-rw-r--r--Day12.cs123
1 files changed, 65 insertions, 58 deletions
diff --git a/Day12.cs b/Day12.cs
index efb380a..b04426d 100644
--- a/Day12.cs
+++ b/Day12.cs
@@ -1,4 +1,3 @@
-using aoc2019.lib;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -7,21 +6,20 @@ namespace aoc2019
 {
     internal sealed class Day12 : Day
     {
-        public override int DayNumber => 12;
-
-        private List<Position> moons;
         private readonly List<Position> startingPositions;
+
+        private readonly List<Position> moons;
         private int step;
 
         public Day12()
         {
             moons = Input
-                .Select(moon => 
+                .Select(moon =>
                     moon
-                    .TrimStart('<')
-                    .TrimEnd('>')
-                    .Split(",")
-                    .Select(val => int.Parse(val.Split("=").Last()))
+                        .TrimStart('<')
+                        .TrimEnd('>')
+                        .Split(",")
+                        .Select(val => int.Parse(val.Split("=").Last()))
                 )
                 .Select(moon => new Position(moon.ToList()))
                 .ToList();
@@ -32,56 +30,11 @@ namespace aoc2019
             startingPositions = moons;
         }
 
-        public class Position
-        {
-            public int x, y, z;
-            public int dx, dy, dz;
-            List<Position> siblings;
-
-            public Position(IList<int> moon)
-            {
-                x = moon[0];
-                y = moon[1];
-                z = moon[2];
-                dx = 0; dy = 0; dz = 0;
-            }
-
-            public void SetSiblings(List<Position> positions)
-            {
-                siblings = positions.Where(p => p != this).ToList();
-            }
-
-            public override string ToString() =>
-                $"pos=<x={x}, y={y}, z={z}> vel=<x={dx}, y={dy}, z={dz}>";
-
-            internal void Gravitate()
-            {
-                foreach (var m in siblings)
-                {
-                    if (x != m.x) dx += x > m.x ? -1 : 1;
-                    if (y != m.y) dy += y > m.y ? -1 : 1;
-                    if (z != m.z) dz += z > m.z ? -1 : 1;
-                }
-            }
-
-            internal void Move()
-            {
-                x += dx;
-                y += dy;
-                z += dz;
-            }
-
-            internal int KineticEnergy =>
-                Math.Abs(x) + Math.Abs(y) + Math.Abs(z);
-            internal int PotentialEnergy =>
-                Math.Abs(dx) + Math.Abs(dy) + Math.Abs(dz);
-            internal int TotalEnergy =>
-                KineticEnergy * PotentialEnergy;
-        }
+        public override int DayNumber => 12;
 
         public static long LCM(long a, long b)
         {
-            return (a * b) / GCD(a, b);
+            return a * b / GCD(a, b);
         }
 
         public static long GCD(long a, long b)
@@ -113,7 +66,8 @@ namespace aoc2019
         {
             int cycleX = 0, cycleY = 0, cycleZ = 0;
 
-            while (cycleX == 0 || cycleY == 0 || cycleZ == 0) {
+            while (cycleX == 0 || cycleY == 0 || cycleZ == 0)
+            {
                 Step();
                 if (cycleX == 0 && moons.All(m => m.dx == 0)) cycleX = step * 2;
                 if (cycleY == 0 && moons.All(m => m.dy == 0)) cycleY = step * 2;
@@ -122,5 +76,58 @@ namespace aoc2019
 
             return $"{LCM(cycleX, LCM(cycleY, cycleZ))}";
         }
+
+        public class Position
+        {
+            public int dx, dy, dz;
+            private List<Position> siblings;
+            public int x, y, z;
+
+            public Position(IList<int> moon)
+            {
+                x = moon[0];
+                y = moon[1];
+                z = moon[2];
+                dx = 0;
+                dy = 0;
+                dz = 0;
+            }
+
+            internal int KineticEnergy =>
+                Math.Abs(x) + Math.Abs(y) + Math.Abs(z);
+
+            internal int PotentialEnergy =>
+                Math.Abs(dx) + Math.Abs(dy) + Math.Abs(dz);
+
+            internal int TotalEnergy =>
+                KineticEnergy * PotentialEnergy;
+
+            public void SetSiblings(List<Position> positions)
+            {
+                siblings = positions.Where(p => p != this).ToList();
+            }
+
+            public override string ToString()
+            {
+                return $"pos=<x={x}, y={y}, z={z}> vel=<x={dx}, y={dy}, z={dz}>";
+            }
+
+            internal void Gravitate()
+            {
+                foreach (var m in siblings)
+                {
+                    if (x != m.x) dx += x > m.x ? -1 : 1;
+                    if (y != m.y) dy += y > m.y ? -1 : 1;
+                    if (z != m.z) dz += z > m.z ? -1 : 1;
+                }
+            }
+
+            internal void Move()
+            {
+                x += dx;
+                y += dy;
+                z += dz;
+            }
+        }
     }
-}
+}
\ No newline at end of file