diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Extensions.cs | 48 | ||||
-rw-r--r-- | lib/IntCodeVM.cs | 160 |
2 files changed, 0 insertions, 208 deletions
diff --git a/lib/Extensions.cs b/lib/Extensions.cs deleted file mode 100644 index eb8c3cb..0000000 --- a/lib/Extensions.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; - -namespace aoc2019.lib -{ - public static class Extensions - { - public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> list) - { - if (list.Count() == 1) return new[] {list}; - return list.SelectMany(t => Permute(list.Where(x => !x.Equals(t))), (v, p) => p.Prepend(v)); - } - - public static IEnumerable<string> Chunk(this string str, int chunkSize) - { - for (var i = 0; i < str.Length; i += chunkSize) - yield return str.Substring(i, chunkSize); - } - - public static string ToDelimitedString<T>(this IEnumerable<T> enumerable, string delimiter = "") - { - return string.Join(delimiter, enumerable); - } - - public static IEnumerable<T> Repeat<T>(this IEnumerable<T> sequence, int? count = null) - { - while (count == null || count-- > 0) - foreach (var item in sequence) - yield return item; - } - - /// <summary> - /// increased accuracy for stopwatch based on frequency. - /// <see - /// href="http://geekswithblogs.net/BlackRabbitCoder/archive/2012/01/12/c.net-little-pitfalls-stopwatch-ticks-are-not-timespan-ticks.aspx"> - /// blog - /// details here - /// </see> - /// </summary> - /// <param name="stopwatch"></param> - /// <returns></returns> - public static double ScaleMilliseconds(this Stopwatch stopwatch) - { - return 1_000 * stopwatch.ElapsedTicks / (double) Stopwatch.Frequency; - } - } -} \ No newline at end of file diff --git a/lib/IntCodeVM.cs b/lib/IntCodeVM.cs deleted file mode 100644 index 2190579..0000000 --- a/lib/IntCodeVM.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace aoc2019.lib -{ - public class IntCodeVM - { - public enum HaltType - { - Terminate, - Waiting - } - - private readonly long[] program; - private long i; - public Queue<long> input, output; - public long[] memory; - private long relbase; - - public IntCodeVM(string tape) - { - i = 0; - relbase = 0; - program = tape.Split(',').Select(long.Parse).ToArray(); - memory = program; - input = new Queue<long>(); - output = new Queue<long>(); - } - - public long Result => output.Dequeue(); - - public void Reset() - { - i = 0; - relbase = 0; - memory = program; - input.Clear(); - output.Clear(); - } - - public void AddInput(params long[] values) - { - foreach (var v in values) AddInput(v); - } - - public void AddInput(long value) - { - input.Enqueue(value); - } - - private long MemGet(long addr) - { - return addr < memory.Length ? memory[addr] : 0; - } - - private void MemSet(long addr, long value) - { - if (addr < 0) addr = 0; - if (addr >= memory.Length) - Array.Resize(ref memory, (int) addr + 1); - memory[addr] = value; - } - - private long Mode(long idx) - { - var mode = MemGet(i) / 100; - for (var s = 1; s < idx; s++) - mode /= 10; - return mode % 10; - } - - private long Get(long idx) - { - var param = MemGet(i + idx); - switch (Mode(idx)) - { - case 0: return MemGet(param); - case 1: return param; - case 2: return MemGet(relbase + param); - default: throw new Exception("invalid parameter mode"); - } - } - - private void Set(long idx, long val) - { - var param = MemGet(i + idx); - switch (Mode(idx)) - { - case 0: - MemSet(param, val); - break; - case 1: throw new Exception("cannot set in immediate mode"); - case 2: - MemSet(relbase + param, val); - break; - default: throw new Exception("invalid parameter mode"); - } - } - - public HaltType Run(params long[] additionalInput) - { - foreach (var s in additionalInput) AddInput(s); - return Run(); - } - - public HaltType Run() - { - while (i < memory.Length) - { - var op = MemGet(i) % 100; - switch (op) - { - case 1: - Set(3, Get(1) + Get(2)); - i += 4; - break; - case 2: - Set(3, Get(1) * Get(2)); - i += 4; - break; - case 3: - if (!input.Any()) - return HaltType.Waiting; - Set(1, input.Dequeue()); - i += 2; - break; - case 4: - output.Enqueue(Get(1)); - i += 2; - break; - case 5: - i = Get(1) == 0 ? i + 3 : Get(2); - break; - case 6: - i = Get(1) != 0 ? i + 3 : Get(2); - break; - case 7: - Set(3, Get(1) < Get(2) ? 1 : 0); - i += 4; - break; - case 8: - Set(3, Get(1) == Get(2) ? 1 : 0); - i += 4; - break; - case 9: - relbase += Get(1); - i += 2; - break; - case 99: - return HaltType.Terminate; - default: - throw new Exception($"unknown op {op} at {i}"); - } - } - - return HaltType.Terminate; - } - } -} \ No newline at end of file |