Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Day 8 Advent of Code 2020
using System.Collections.Generic;
using System.Linq;
using AdventOfCode.Library;
using AdventOfCode.Library.Interpreter;
namespace AdventOfCode
{
// Day 8
internal static class Program
{
private const bool DoPart1 = true;
private const bool DoPart2 = true;
public static void Main()
{
var data = TextUtility.ReadLines(removeBlank: true);
data.Process(DoPart1, 1, Solver1);
data.Process(DoPart2, 2, Solver2);
}
private static string Solver1(List<string> arg1)
{
var instructions = arg1.Select(k => new Instruction(k)).ToList();
var game = new Machine(instructions);
game.Run();
return $"{game.Accumulator}";
}
private static string Solver2(List<string> arg1)
{
var source = arg1.Select(k => new Instruction(k)).ToList();
var accumulator = 0;
for (var i = 0; i < source.Count; i++)
{
var edited = arg1.Select(k => new Instruction(k)).ToList();
edited[i] = SwapInstruction(source[i]);
var game = new Machine(edited);
game.Run();
if (!game.Error || game.ProgramIndex != game.OpCount)
{
continue;
}
accumulator = game.Accumulator;
break;
}
return $"{accumulator}";
}
private static Instruction SwapInstruction(Instruction i)
{
return i.Operation switch
{
OpCode.Jmp => new Instruction(OpCode.Nop, i.Value),
OpCode.Nop => new Instruction(OpCode.Jmp, i.Value),
_ => i
};
}
}
}
@RichardVasquez
Copy link
Author

RichardVasquez commented Dec 8, 2020

Just the basics for now. My VM is kind of a cluster of files right now, and I'll place it somewhere once it's in a presentable state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment