diff --git a/src/18.cs b/src/18.cs index b377a13..26eaef7 100644 --- a/src/18.cs +++ b/src/18.cs @@ -95,20 +95,26 @@ internal class Day18 : Day } } + public static bool testing; + [Conditional("DEBUG")] private static void DoTests() { + testing = true; + DoExplodeTests(); DoSplitTests(); DoMagnitudeTests(); DoAddTests(); DoReduceTests(); DoPart1Tests(); + + testing = false; } private static void DoExplodeTests() { - Logger.Log("test: explode"); + Util.StartTestSet("explode"); var tests = new List() { "[[[[[9,8],1],2],3],4]", @@ -134,15 +140,13 @@ internal class Day18 : Day for (int i = 0; i < tests.Count; i++) { - Logger.Log($"1.{(i + 1)}"); + Util.StartTest($"1.{(i + 1)}"); + SFNum test = ParseSFNum(tests[i]); SFNum result = ParseSFNum(exploded[i]); var didExplode = CheckExplodes(test); - if (!didExplode || test.ToString() != result.ToString()) - { - throw new Exception(); - } - Logger.Log(""); + + Util.TestCondition(() => didExplode && test.ToString() == result.ToString()); } var twoStepTests = new List() @@ -156,26 +160,19 @@ internal class Day18 : Day for (int i = 0; i < twoStepTests.Count; i++) { - Logger.Log($"2.{(i + 1)}"); + Util.StartTest($"2.{(i + 1)}"); SFNum test = ParseSFNum(twoStepTests[i]); SFNum result = ParseSFNum(twoStepExploded[i]); var didExplode = CheckExplodes(test); - if (!didExplode) - { - throw new Exception(); - } + Util.TestCondition(() => didExplode, false); didExplode = CheckExplodes(test); - if (!didExplode || test.ToString() != result.ToString()) - { - throw new Exception(); - } - Logger.Log(""); + Util.TestCondition(() => didExplode || test.ToString() != result.ToString()); } } private static void DoSplitTests() { - Logger.Log("test: split"); + Util.StartTestSet("split"); var tests = new List() { "[[[[0,7],4],[15,[0,13]]],[1,1]]", @@ -191,22 +188,17 @@ internal class Day18 : Day for (int i = 0; i < tests.Count; i++) { - Logger.Log($"{(i + 1)}"); + Util.StartTest($"{(i + 1)}"); SFNum test = ParseSFNum(tests[i]); SFNum result = ParseSFNum(split[i]); var didSplit = CheckSplits(test); - if (!didSplit || test.ToString() != result.ToString()) - { - throw new Exception(); - } - // todo: add tests verifying owners are correct - Logger.Log(""); + Util.TestCondition(() => didSplit && test.ToString() == result.ToString()); } } private static void DoMagnitudeTests() { - Logger.Log("test: magnitude"); + Util.StartTestSet("magnitude"); var tests = new List() { "[9,1]", @@ -234,20 +226,16 @@ internal class Day18 : Day for (int i = 0; i < tests.Count; i++) { - Logger.Log($"{(i + 1)}"); + Util.StartTest($"{(i + 1)}"); SFNum test = ParseSFNum(tests[i]); var magnitude = test.Magnitude; - if (magnitude != magnitudes[i]) - { - throw new Exception(); - } - Logger.Log(""); + Util.TestCondition(() => magnitude == magnitudes[i]); } } private static void DoAddTests() { - Logger.Log("test: add"); + Util.StartTest("add"); var tests = new List>() { new() @@ -263,28 +251,21 @@ internal class Day18 : Day for (int i = 0; i < tests.Count; i++) { - Logger.Log($"{(i + 1)}"); + Util.StartTest($"{(i + 1)}"); SFNum test = ParseSFNum(tests[i][0]); for (int j = 1; j < tests[i].Count; j++) { test = Add(test, ParseSFNum(tests[i][j])); } SFNum result = ParseSFNum(results[i]); - if (test.ToString() != result.ToString()) - { - throw new Exception(); - } - if (result.leftNum!.owner != result || result.rightNum!.owner != result) - { - throw new Exception(); - } - Logger.Log(""); + Util.TestCondition(() => test.ToString() == result.ToString(), false); + Util.TestCondition(() => result.leftNum!.owner == result && result.rightNum!.owner == result); } } private static void DoReduceTests() { - Logger.Log("test: reduce"); + Util.StartTestSet("reduce"); var tests = new List() { "[[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]", @@ -296,21 +277,17 @@ internal class Day18 : Day for (int i = 0; i < tests.Count; i++) { - Logger.Log($"{(i + 1)}"); + Util.StartTest($"{(i + 1)}"); SFNum test = ParseSFNum(tests[i]); SFNum result = ParseSFNum(results[i]); Reduce(test); - if (test.ToString() != result.ToString()) - { - throw new Exception(); - } - Logger.Log(""); + Util.TestCondition(() => test.ToString() == result.ToString()); } } private static void DoPart1Tests() { - Logger.Log("test: add+reduce"); + Util.StartTestSet("add+reduce"); var vals = new List>() { new() @@ -407,7 +384,7 @@ internal class Day18 : Day for (int i = 0; i < vals.Count; i++) { - Logger.Log($"{(i + 1)}"); + Util.StartTest($"{(i + 1)}"); SFNum curr = vals[i][0]; for (int j = 1; j < vals[i].Count; j++) { @@ -415,12 +392,7 @@ internal class Day18 : Day Reduce(curr); } - if (curr.ToString() != results[i].ToString()) - { - Logger.Log($" expected: {results[i]}"); - throw new Exception(); - } - Logger.Log(""); + Util.TestCondition(() => curr.ToString() == results[i].ToString()); } } @@ -639,10 +611,11 @@ internal class Day18 : Day private static void Explode(SFNum num) { -#if DEBUG - num.exploding = true; - Logger.Log($"exploding: {num.Root}"); -#endif + if (testing) + { + num.exploding = true; + Logger.Log($"exploding: {num.Root}"); + } var curr = num; var last = num; @@ -719,9 +692,10 @@ internal class Day18 : Day num.owner.right = 0; } -#if DEBUG - Logger.Log($" -> {num.Root}"); -#endif + if (testing) + { + Logger.Log($" -> {num.Root}"); + } num.owner = null; } @@ -756,10 +730,12 @@ internal class Day18 : Day { if (num.left != null && num.left >= 10) { - num.splittingLeft = true; -#if DEBUG - Logger.Log($"splitting: {num.Root}"); -#endif + if (testing) + { + num.splittingLeft = true; + Logger.Log($"splitting: {num.Root}"); + } + num.leftNum = new() { left = (int)Math.Floor((int)num.left / 2.0), @@ -767,19 +743,24 @@ internal class Day18 : Day owner = num, }; num.left = null; - num.splittingLeft = false; -#if DEBUG - Logger.Log($" -> {num.Root}"); -#endif + + if (testing) + { + num.splittingLeft = false; + Logger.Log($" -> {num.Root}"); + } + return true; } if (num.right != null && num.right >= 10) { - num.splittingRight = true; -#if DEBUG - Logger.Log($"splitting: {num.Root}"); -#endif + if (testing) + { + num.splittingRight = true; + Logger.Log($"splitting: {num.Root}"); + } + num.rightNum = new() { left = (int)Math.Floor((int)num.right / 2.0), @@ -787,10 +768,13 @@ internal class Day18 : Day owner = num, }; num.right = null; - num.splittingRight = false; -#if DEBUG - Logger.Log($" -> {num.Root}"); -#endif + + if (testing) + { + num.splittingRight = false; + Logger.Log($" -> {num.Root}"); + } + return true; } diff --git a/src/Util.cs b/src/Util.cs index ce6f01c..346f046 100644 --- a/src/Util.cs +++ b/src/Util.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Diagnostics; +using System.Text; namespace aoc2021; @@ -30,4 +31,33 @@ internal static class Util return File.ReadAllLines(filename); } + + internal static void StartTestSet(string name) + { + Logger.Log($"test: {name}"); + } + + internal static void StartTest(string label) + { + Logger.Log($"{label}"); + } + + internal static void TestCondition(Func a, bool printResult = true) + { + if (a?.Invoke() == false) + { + Debug.Assert(false); + if (printResult) + { + Logger.Log("x"); + } + } + else + { + if (printResult) + { + Logger.Log(""); + } + } + } }