use raw string literals for Test2019.cs
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
e2fd0cb29b
commit
85e2eaaed9
|
@ -5,6 +5,24 @@ namespace AOC.Test;
|
||||||
[TestClass]
|
[TestClass]
|
||||||
public class Test2019
|
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]
|
[DataTestMethod]
|
||||||
[DataRow(typeof(Day01), "3394106", "5088280")]
|
[DataRow(typeof(Day01), "3394106", "5088280")]
|
||||||
[DataRow(typeof(Day02), "3085697", "9425")]
|
[DataRow(typeof(Day02), "3085697", "9425")]
|
||||||
|
@ -13,12 +31,10 @@ public class Test2019
|
||||||
[DataRow(typeof(Day05), "7692125", "14340395")]
|
[DataRow(typeof(Day05), "7692125", "14340395")]
|
||||||
[DataRow(typeof(Day06), "145250", "274")]
|
[DataRow(typeof(Day06), "145250", "274")]
|
||||||
[DataRow(typeof(Day07), "19650", "35961106")]
|
[DataRow(typeof(Day07), "19650", "35961106")]
|
||||||
[DataRow(typeof(Day08), "2413",
|
[DataRow(typeof(Day08), "2413", Day8Actual)]
|
||||||
"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(Day09), "3409270027", "82760")]
|
[DataRow(typeof(Day09), "3409270027", "82760")]
|
||||||
[DataRow(typeof(Day10), "260", "608")]
|
[DataRow(typeof(Day10), "260", "608")]
|
||||||
[DataRow(typeof(Day11), "2054",
|
[DataRow(typeof(Day11), "2054", Day11Actual)]
|
||||||
" # # ### #### #### ## ## # # ### \n # # # # # # # # # # # # # \n ## # # # ### # # # #### ### \n # # ### # # #### # # # # # \n # # # # # # # # # # # # # # \n # # # # #### #### # # ## # # ### ")]
|
|
||||||
[DataRow(typeof(Day12), "10635", "583523031727256")]
|
[DataRow(typeof(Day12), "10635", "583523031727256")]
|
||||||
[DataRow(typeof(Day13), "361", "after 7133 moves, the score is: 17590")]
|
[DataRow(typeof(Day13), "361", "after 7133 moves, the score is: 17590")]
|
||||||
[DataRow(typeof(Day14), "397771", "3126714")]
|
[DataRow(typeof(Day14), "397771", "3126714")]
|
||||||
|
|
|
@ -30,8 +30,8 @@ public sealed class Day08 : Day
|
||||||
)
|
)
|
||||||
.ToDelimitedString()
|
.ToDelimitedString()
|
||||||
.Chunk(25)
|
.Chunk(25)
|
||||||
.Select(s => new string(s))
|
.Select(s => new string(s).Trim())
|
||||||
.ToDelimitedString("\n")
|
.ToDelimitedString(Environment.NewLine)
|
||||||
.Replace('1', 'x');
|
.Replace('1', 'x');
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -87,7 +87,7 @@ public sealed class Day11 : Day
|
||||||
.ToDelimitedString()
|
.ToDelimitedString()
|
||||||
)
|
)
|
||||||
.Reverse()
|
.Reverse()
|
||||||
.ToDelimitedString("\n");
|
.ToDelimitedString(Environment.NewLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum Direction
|
private enum Direction
|
||||||
|
|
|
@ -8,32 +8,32 @@ public sealed class Day16 : Day
|
||||||
public Day16() : base(2019, 16, "Flawed Frequency Transmission")
|
public Day16() : base(2019, 16, "Flawed Frequency Transmission")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void ProcessInput(){
|
public override void ProcessInput()
|
||||||
_initialList = Input.First().Select(c => int.Parse($"{c}")).ToArray();
|
{
|
||||||
|
_initialList = Input.First().Select(c => c - '0').ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object Part1()
|
public override object Part1()
|
||||||
{
|
{
|
||||||
const int phaseCount = 100;
|
|
||||||
var signal0 = _initialList!.ToArray();
|
var signal0 = _initialList!.ToArray();
|
||||||
var signal1 = new int[signal0.Length];
|
var signal1 = new int[signal0.Length];
|
||||||
|
|
||||||
for (var i = 0; i < phaseCount; i++)
|
for (var i = 0; i < 100; i++)
|
||||||
CalculateSignal(i % 2 == 0 ? signal0 : signal1, i % 2 == 0 ? signal1 : signal0);
|
if (i % 2 == 0)
|
||||||
|
CalculateSignal(signal0, signal1);
|
||||||
|
else
|
||||||
|
CalculateSignal(signal1, signal0);
|
||||||
|
|
||||||
return new string(
|
return new string(signal0.Take(8).Select(c => (char)(c + '0')).ToArray());
|
||||||
signal0.Take(8).Select(c => (char)(c + '0'))
|
|
||||||
.ToArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
const int phaseCount = 100;
|
|
||||||
var messageOffset = _initialList!.Take(7).Aggregate((n, i) => n * 10 + i);
|
var messageOffset = _initialList!.Take(7).Aggregate((n, i) => n * 10 + i);
|
||||||
var signal = _initialList!.Repeat(10_000).Skip(messageOffset).ToArray();
|
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;
|
signal[^1] %= 10;
|
||||||
for (var i = signal.Length - 2; i >= 0; i--)
|
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());
|
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++)
|
for (var j = 0; j < output.Length; j++)
|
||||||
output[outputIndex] =
|
output[j] = Math.Abs(BasePattern
|
||||||
Math.Abs(PatternValues(outputIndex, input.Count).Select((pv, i) => pv * input[i] % 10).Sum()) % 10;
|
.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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,8 +8,8 @@ public class IntCodeVM
|
||||||
Waiting
|
Waiting
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly Queue<long> _input;
|
private readonly Queue<long> _input = new();
|
||||||
public readonly Queue<long> Output;
|
public readonly Queue<long> Output = new();
|
||||||
|
|
||||||
private readonly long[] _program;
|
private readonly long[] _program;
|
||||||
private long _i;
|
private long _i;
|
||||||
|
@ -22,8 +22,6 @@ public class IntCodeVM
|
||||||
_relativeBase = 0;
|
_relativeBase = 0;
|
||||||
_program = tape.Split(',').Select(long.Parse).ToArray();
|
_program = tape.Split(',').Select(long.Parse).ToArray();
|
||||||
Memory = _program;
|
Memory = _program;
|
||||||
_input = new();
|
|
||||||
Output = new();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long Result => Output.Dequeue();
|
public long Result => Output.Dequeue();
|
||||||
|
@ -37,34 +35,21 @@ public class IntCodeVM
|
||||||
Output.Clear();
|
Output.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddInput(params long[] values)
|
public void AddInput(long value) => _input.Enqueue(value);
|
||||||
{
|
|
||||||
foreach (var v in values) AddInput(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddInput(long value)
|
private long MemGet(long addr) => addr < Memory.Length ? Memory[addr] : 0;
|
||||||
{
|
|
||||||
_input.Enqueue(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private long MemGet(long addr)
|
|
||||||
{
|
|
||||||
return addr < Memory.Length ? Memory[addr] : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void MemSet(long addr, long value)
|
private void MemSet(long addr, long value)
|
||||||
{
|
{
|
||||||
if (addr < 0) addr = 0;
|
if (addr < 0) addr = 0;
|
||||||
if (addr >= Memory.Length)
|
if (addr >= Memory.Length) Array.Resize(ref Memory, (int)addr + 1);
|
||||||
Array.Resize(ref Memory, (int)addr + 1);
|
|
||||||
Memory[addr] = value;
|
Memory[addr] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long Mode(long idx)
|
private long Mode(long idx)
|
||||||
{
|
{
|
||||||
var mode = MemGet(_i) / 100;
|
var mode = MemGet(_i) / 100;
|
||||||
for (var s = 1; s < idx; s++)
|
for (var s = 1; s < idx; s++) mode /= 10;
|
||||||
mode /= 10;
|
|
||||||
return mode % 10;
|
return mode % 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue