Day 13 - Part 2

This commit is contained in:
2019-12-13 18:30:18 -05:00
parent 581a342318
commit 0d745d282c
4 changed files with 88 additions and 6 deletions

View File

@@ -13,6 +13,9 @@
<None Update="Day10\input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Day13\input2.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Day13\input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
@@ -7,13 +8,15 @@ namespace Advent
{
public static class Day13
{
private static Dictionary<Tuple<long, long>, long> _tiles;
public static void Execute()
{
var lines = File.ReadAllLines(@".\Day13\input.txt");
var lines = File.ReadAllLines(@".\Day13\input2.txt");
var game = new IntcodeComputer(lines[0], 0);
var game = new IntcodeComputer(lines[0], 0, OnInput);
var tiles = new Dictionary<Tuple<long, long>, long>();
_tiles = new Dictionary<Tuple<long, long>, long>();
while (!game.Halted)
{
@@ -21,10 +24,78 @@ namespace Advent
var y = game.Execute(0);
var tile = game.Execute(0);
tiles[new Tuple<long, long>(x, y)] = tile;
_tiles[new Tuple<long, long>(x, y)] = tile;
}
Console.WriteLine(tiles.Values.Count(t => t == 2));
var score = _tiles[new Tuple<long, long>(-1, 0)];
Console.WriteLine(score);
//Console.WriteLine(_tiles.Values.Count(t => t == 2));
}
private static long OnInput()
{
//DrawScreen(_tiles);
var ballPos = _tiles.First(t => t.Value == 4);
var paddlePos = _tiles.First(t => t.Value == 3);
if (ballPos.Key.Item1 > paddlePos.Key.Item1)
return 1;
if (ballPos.Key.Item1 < paddlePos.Key.Item1)
return -1;
return 0;
//Console.WriteLine();
//Console.WriteLine();
//Console.Write("Input: ");
//var key = Console.ReadKey();
//if (key.Key == ConsoleKey.LeftArrow)
// return -1;
//if (key.Key == ConsoleKey.RightArrow)
// return 1;
//return 0;
}
private static void DrawScreen(Dictionary<Tuple<long, long>, long> tiles)
{
foreach (var tile in tiles)
{
if (tile.Key.Item1 == -1 && tile.Key.Item2 == 0)
{
Debug.WriteLine($"Score: {tile.Value}");
continue;
}
Console.SetCursorPosition((int)tile.Key.Item1, (int)tile.Key.Item2);
var c = ' ';
switch (tile.Value)
{
case 1:
c = 'W';
break;
case 2:
c = 'B';
break;
case 3:
c = 'P';
break;
case 4:
c = 'O';
break;
}
Console.Write(c);
}
}
}
}

1
Day13/input2.txt Normal file

File diff suppressed because one or more lines are too long

View File

@@ -19,16 +19,20 @@ namespace Advent
private long _instructionPointer;
private long _output;
private long _relativeBase;
private InputDelegate _onInput;
public bool Halted { get; private set; }
public IntcodeComputer(string memoryString, long phase)
public delegate long InputDelegate();
public IntcodeComputer(string memoryString, long phase, InputDelegate onInput = null)
{
_memory = new long[5000];
memoryString.Split(',').Select(long.Parse).ToArray().CopyTo(_memory, 0);
_phase = phase;
_onInput = onInput;
}
private long GetValue(long parameter, ParameterMode mode)
@@ -96,6 +100,9 @@ namespace Advent
_setPhase = true;
if (_onInput != null)
inputValue = _onInput();
SetValue(inputValue, _memory[_instructionPointer + 1], mode1);
_instructionPointer += 2;