Created
December 8, 2020 17:16
-
-
Save RichardVasquez/34057262a3f528b19155669dd46911ae to your computer and use it in GitHub Desktop.
Day 8 Advent of Code 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.