Created
June 21, 2018 11:16
-
-
Save Ratstail91/5121401c68280d010cd645f70b4b8c6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| namespace Ducci { | |
| static class DucciSolver { | |
| ///<summary> | |
| ///The entry point of the program | |
| ///</summary> | |
| static void Main(string[] args) { | |
| //initial arguments | |
| List<int> arguments = ParseArguments(args); | |
| List<int> zeroes = ReturnZeroesList(arguments.Count); | |
| //the collection of lists | |
| List<List<int>> uberList = new List<List<int>> (); | |
| uberList.Add(arguments); | |
| //the process | |
| for (;;) { | |
| List<int> iterator = DucciProcess(uberList[uberList.Count - 1]); | |
| if (CheckAny(iterator, uberList) || CheckEqual(iterator, zeroes)) { | |
| uberList.Add(iterator); | |
| break; | |
| } | |
| //I don't like code duplication | |
| uberList.Add(iterator); | |
| } | |
| //print the output | |
| foreach (List<int> list in uberList) { | |
| Console.WriteLine("[" + String.Join("; ", list) + "]"); | |
| } | |
| Console.WriteLine(uberList.Count + " steps"); | |
| } | |
| ///<summary> | |
| ///Parse each argument into an integer, throwing an exception on error | |
| ///</summary> | |
| static List<int> ParseArguments(string[] args) { | |
| List<int> result = new List<int> (); | |
| foreach (string arg in args) { | |
| result.Add(int.Parse(arg)); | |
| } | |
| return result; | |
| } | |
| ///<summary> | |
| ///Return a list filled with zeroes | |
| ///</summary> | |
| static List<int> ReturnZeroesList(int count) { | |
| List<int> result = new List<int> (); | |
| for (int i = 0; i < count; i++) { | |
| result.Add(0); | |
| } | |
| return result; | |
| } | |
| ///<summary> | |
| ///Compare two lists, return true if they're identical | |
| ///</summary> | |
| static bool CheckEqual<T>(IList<T> a, IList<T> b) { | |
| if (a == null || b == null) | |
| return (a == null && b == null); | |
| if (a.Count != b.Count) | |
| return false; | |
| return a.SequenceEqual(b); | |
| } | |
| ///<summary> | |
| ///Compare an iterator to an uber list, and return true if the iterator is within that uber list | |
| ///</summary> | |
| static bool CheckAny(List<int> iterator, List<List<int>> uberList) { | |
| foreach (List<int> list in uberList) { | |
| if (CheckEqual(iterator, list)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| ///<summary> | |
| ///Perform a single step of the Ducci Process | |
| ///</summary> | |
| static List<int> DucciProcess(List<int> arg) { | |
| //the result | |
| List<int> result = new List<int>(); | |
| //crunch each pair, not including the first and last | |
| for (int i = 1; i < arg.Count; i++) { | |
| result.Add(Math.Abs(arg[i] - arg[i-1])); | |
| } | |
| //crunch the first and last | |
| result.Add(Math.Abs(arg[0] - arg[arg.Count-1])); | |
| //finally | |
| return result; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment