ben
/
aoc
1
0
Fork 0

use raw string literals for Test2019.cs
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2022-12-11 19:23:23 -05:00
parent e2fd0cb29b
commit 85e2eaaed9
5 changed files with 49 additions and 53 deletions

View File

@ -5,6 +5,24 @@ namespace AOC.Test;
[TestClass]
public class Test2019
{
private const string Day8Actual = """
xxx xx xxx xxxx xxx
x x x x x x x x x
xxx x x x x xxx
x x x xxx x x x
x x x x x x x x
xxx xx x xxxx xxx
""";
private const string Day11Actual = """
# # ### #### #### ## ## # # ###
# # # # # # # # # # # # #
## # # # ### # # # #### ###
# # ### # # #### # # # # #
# # # # # # # # # # # # # #
# # # # #### #### # # ## # # ###
""";
[DataTestMethod]
[DataRow(typeof(Day01), "3394106", "5088280")]
[DataRow(typeof(Day02), "3085697", "9425")]
@ -13,12 +31,10 @@ public class Test2019
[DataRow(typeof(Day05), "7692125", "14340395")]
[DataRow(typeof(Day06), "145250", "274")]
[DataRow(typeof(Day07), "19650", "35961106")]
[DataRow(typeof(Day08), "2413",
"xxx xx xxx xxxx xxx \nx x x x x x x x x \nxxx x x x x xxx \nx x x xxx x x x \nx x x x x x x x \nxxx xx x xxxx xxx ")]
[DataRow(typeof(Day08), "2413", Day8Actual)]
[DataRow(typeof(Day09), "3409270027", "82760")]
[DataRow(typeof(Day10), "260", "608")]
[DataRow(typeof(Day11), "2054",
" # # ### #### #### ## ## # # ### \n # # # # # # # # # # # # # \n ## # # # ### # # # #### ### \n # # ### # # #### # # # # # \n # # # # # # # # # # # # # # \n # # # # #### #### # # ## # # ### ")]
[DataRow(typeof(Day11), "2054", Day11Actual)]
[DataRow(typeof(Day12), "10635", "583523031727256")]
[DataRow(typeof(Day13), "361", "after 7133 moves, the score is: 17590")]
[DataRow(typeof(Day14), "397771", "3126714")]

View File

@ -30,8 +30,8 @@ public sealed class Day08 : Day
)
.ToDelimitedString()
.Chunk(25)
.Select(s => new string(s))
.ToDelimitedString("\n")
.Select(s => new string(s).Trim())
.ToDelimitedString(Environment.NewLine)
.Replace('1', 'x');
}
}

View File

@ -87,7 +87,7 @@ public sealed class Day11 : Day
.ToDelimitedString()
)
.Reverse()
.ToDelimitedString("\n");
.ToDelimitedString(Environment.NewLine);
}
private enum Direction

View File

@ -8,32 +8,32 @@ public sealed class Day16 : Day
public Day16() : base(2019, 16, "Flawed Frequency Transmission")
{
}
public override void ProcessInput(){
_initialList = Input.First().Select(c => int.Parse($"{c}")).ToArray();
public override void ProcessInput()
{
_initialList = Input.First().Select(c => c - '0').ToArray();
}
public override object Part1()
{
const int phaseCount = 100;
var signal0 = _initialList!.ToArray();
var signal1 = new int[signal0.Length];
for (var i = 0; i < phaseCount; i++)
CalculateSignal(i % 2 == 0 ? signal0 : signal1, i % 2 == 0 ? signal1 : signal0);
for (var i = 0; i < 100; i++)
if (i % 2 == 0)
CalculateSignal(signal0, signal1);
else
CalculateSignal(signal1, signal0);
return new string(
signal0.Take(8).Select(c => (char)(c + '0'))
.ToArray());
return new string(signal0.Take(8).Select(c => (char)(c + '0')).ToArray());
}
public override object Part2()
{
const int phaseCount = 100;
var messageOffset = _initialList!.Take(7).Aggregate((n, i) => n * 10 + i);
var signal = _initialList!.Repeat(10_000).Skip(messageOffset).ToArray();
for (var p = 0; p < phaseCount; p++)
for (var p = 0; p < 100; p++)
{
signal[^1] %= 10;
for (var i = signal.Length - 2; i >= 0; i--)
@ -43,19 +43,14 @@ public sealed class Day16 : Day
return new string(signal.Take(8).Select(c => (char)(c + '0')).ToArray());
}
private static void CalculateSignal(IReadOnlyList<int> input, IList<int> output)
private static void CalculateSignal(int[] input, int[] output)
{
for (var outputIndex = 0; outputIndex < output.Count; outputIndex++)
output[outputIndex] =
Math.Abs(PatternValues(outputIndex, input.Count).Select((pv, i) => pv * input[i] % 10).Sum()) % 10;
for (var j = 0; j < output.Length; j++)
output[j] = Math.Abs(BasePattern
.SelectMany(v => Enumerable.Repeat(v, j + 1))
.Repeat()
.Skip(1)
.Take(input.Length)
.Select((pv, i) => pv * input[i] % 10).Sum()) % 10;
}
private static IEnumerable<int> PatternValues(int index, int count)
{
return BasePattern
.SelectMany(v => Enumerable.Repeat(v, index + 1))
.Repeat(int.MaxValue)
.Skip(1)
.Take(count);
}
}
}

View File

@ -8,8 +8,8 @@ public class IntCodeVM
Waiting
}
private readonly Queue<long> _input;
public readonly Queue<long> Output;
private readonly Queue<long> _input = new();
public readonly Queue<long> Output = new();
private readonly long[] _program;
private long _i;
@ -22,8 +22,6 @@ public class IntCodeVM
_relativeBase = 0;
_program = tape.Split(',').Select(long.Parse).ToArray();
Memory = _program;
_input = new();
Output = new();
}
public long Result => Output.Dequeue();
@ -37,34 +35,21 @@ public class IntCodeVM
Output.Clear();
}
public void AddInput(params long[] values)
{
foreach (var v in values) AddInput(v);
}
public void AddInput(long value) => _input.Enqueue(value);
public void AddInput(long value)
{
_input.Enqueue(value);
}
private long MemGet(long addr)
{
return addr < Memory.Length ? Memory[addr] : 0;
}
private long MemGet(long addr) => 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);
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;
for (var s = 1; s < idx; s++) mode /= 10;
return mode % 10;
}