day 13
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
3103a7aef1
commit
edd8e87b4c
|
@ -0,0 +1,90 @@
|
|||
using aoc2019.lib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace aoc2019
|
||||
{
|
||||
internal class Day13 : Day
|
||||
{
|
||||
public override int DayNumber => 13;
|
||||
|
||||
private readonly IntCodeVM vm;
|
||||
private readonly Dictionary<(int x, int y), int> board;
|
||||
|
||||
public Day13()
|
||||
{
|
||||
vm = new IntCodeVM(Input.First());
|
||||
board = new Dictionary<(int, int), int>();
|
||||
}
|
||||
|
||||
private void UpdateTiles(IEnumerable<long> queue)
|
||||
{
|
||||
var input = queue.Select(i => (int)i).ToList();
|
||||
|
||||
for (var i = 0; i < input.Count - 2; i += 3)
|
||||
{
|
||||
var x = input[i];
|
||||
var y = input[i + 1];
|
||||
var val = input[i + 2];
|
||||
|
||||
if (board.ContainsKey((x, y)))
|
||||
board[(x, y)] = val;
|
||||
else
|
||||
board.Add((x, y), val);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void PrintBoard()
|
||||
{
|
||||
foreach (var tile in board)
|
||||
{
|
||||
var (x, y) = tile.Key;
|
||||
if (x < 0 || y < 0) continue;
|
||||
Console.SetCursorPosition(x, y);
|
||||
Console.Write(tile.Value switch
|
||||
{
|
||||
0 => " ",
|
||||
1 => "|",
|
||||
2 => "B",
|
||||
3 => "_",
|
||||
4 => ".",
|
||||
_ => tile.Value
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public override string Part1()
|
||||
{
|
||||
vm.Reset();
|
||||
vm.Run();
|
||||
return $"{vm.output.Where((v, i) => (i + 1) % 3 == 0 && v == 2).Count()}";
|
||||
}
|
||||
|
||||
public override string Part2()
|
||||
{
|
||||
vm.Reset();
|
||||
vm.memory[0] = 2;
|
||||
var printboard = false;
|
||||
var gameTicks = 0;
|
||||
if (printboard) Console.Clear();
|
||||
|
||||
var haltType = IntCodeVM.HaltType.Waiting;
|
||||
while (haltType == IntCodeVM.HaltType.Waiting)
|
||||
{
|
||||
haltType = vm.Run();
|
||||
UpdateTiles(vm.output);
|
||||
|
||||
var (ball, _) = board.First(t => t.Value == 4).Key;
|
||||
var (paddle, _) = board.First(t => t.Value == 3).Key;
|
||||
vm.AddInput(ball > paddle ? 1 : ball < paddle ? -1 : 0);
|
||||
|
||||
gameTicks++;
|
||||
if (printboard) PrintBoard();
|
||||
}
|
||||
|
||||
return $"after {gameTicks} moves, the score is: {board[(-1, 0)]}";
|
||||
}
|
||||
}
|
||||
}
|
2
Day7.cs
2
Day7.cs
|
@ -47,7 +47,7 @@ namespace aoc2019
|
|||
foreach (var (vm, phase) in Amplifiers.Zip(phaseSeq))
|
||||
{
|
||||
vm.Reset();
|
||||
vm.input.Enqueue(phase);
|
||||
vm.AddInput(phase);
|
||||
}
|
||||
|
||||
var vms = new Queue<IntCodeVM>(Amplifiers);
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
<None Update="input\day12.in">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="input\day13.in">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="input\day2.in">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -39,6 +39,11 @@ namespace aoc2019.lib
|
|||
|
||||
public long Result => output.Dequeue();
|
||||
|
||||
public void AddInput(long value)
|
||||
{
|
||||
input.Enqueue(value);
|
||||
}
|
||||
|
||||
private long MemGet(long addr)
|
||||
{
|
||||
return addr < memory.Length ? memory[addr] : 0;
|
||||
|
@ -86,7 +91,7 @@ namespace aoc2019.lib
|
|||
|
||||
public HaltType Run(params long[] additionalInput)
|
||||
{
|
||||
foreach (var s in additionalInput) input.Enqueue(s);
|
||||
foreach (var s in additionalInput) AddInput(s);
|
||||
return Run();
|
||||
}
|
||||
public HaltType Run()
|
||||
|
|
Loading…
Reference in New Issue