first commit
This commit is contained in:
101
ProblemTwo/Program.cs
Normal file
101
ProblemTwo/Program.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProblemTwo
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
/* PART ONE */
|
||||
var program = LoadProgramIntoMemory(ProgramStringPartOne);
|
||||
program = SeedProgram(program, 12, 2);
|
||||
program = ExecuteProgram(program);
|
||||
Console.WriteLine(program[0]);
|
||||
|
||||
/* PART TWO */
|
||||
for (int noun = 0; noun < 100; noun++)
|
||||
{
|
||||
for (int verb = 0; verb < 100; verb++)
|
||||
{
|
||||
program = LoadProgramIntoMemory(ProgramStringPartOne);
|
||||
program = SeedProgram(program, noun, verb);
|
||||
program = ExecuteProgram(program);
|
||||
|
||||
if (program[0] == 19690720)
|
||||
Console.WriteLine(100 * noun + verb);
|
||||
}
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static int[] LoadProgramIntoMemory(string strProgram)
|
||||
{
|
||||
return strProgram.Split(',').Select(x => int.Parse(x)).ToArray();
|
||||
}
|
||||
|
||||
static int[] SeedProgram(int[] program, int noun, int verb)
|
||||
{
|
||||
program[1] = noun;
|
||||
program[2] = verb;
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
static int[] ExecuteProgram(int[] program)
|
||||
{
|
||||
int parameterCount = 0;
|
||||
for (int i = 0; i < program.Length; i = i + parameterCount)
|
||||
{
|
||||
var operationCode = (Operations)program[i];
|
||||
|
||||
if (operationCode == Operations.Add)
|
||||
{
|
||||
var inputOne = program[i + 1];
|
||||
var inputTwo = program[i + 2];
|
||||
var result = program[i + 3];
|
||||
|
||||
program[result] = program[inputOne] + program[inputTwo];
|
||||
parameterCount = OperationParameterCount.Add;
|
||||
}
|
||||
else if (operationCode == Operations.Multiply)
|
||||
{
|
||||
var inputOne = program[i + 1];
|
||||
var inputTwo = program[i + 2];
|
||||
var result = program[i + 3];
|
||||
|
||||
program[result] = program[inputOne] * program[inputTwo];
|
||||
parameterCount = OperationParameterCount.Multiply;
|
||||
}
|
||||
else if (operationCode == Operations.Exit)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
throw new Exception("Unsupported Operation");
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
enum Operations
|
||||
{
|
||||
Add = 1,
|
||||
Multiply = 2,
|
||||
Exit = 99
|
||||
}
|
||||
|
||||
static class OperationParameterCount
|
||||
{
|
||||
public static int Add => 4;
|
||||
public static int Multiply => 4;
|
||||
public static int Exit => 0;
|
||||
}
|
||||
|
||||
static string ProgramStringPartOne = "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,5,19,23,1,23,6,27,1,5,27,31,1,31,6,35,1,9,35,39,2,10,39,43,1,43,6,47,2,6,47,51,1,5,51,55,1,55,13,59,1,59,10,63,2,10,63,67,1,9,67,71,2,6,71,75,1,5,75,79,2,79,13,83,1,83,5,87,1,87,9,91,1,5,91,95,1,5,95,99,1,99,13,103,1,10,103,107,1,107,9,111,1,6,111,115,2,115,13,119,1,10,119,123,2,123,6,127,1,5,127,131,1,5,131,135,1,135,6,139,2,139,10,143,2,143,9,147,1,147,6,151,1,151,13,155,2,155,9,159,1,6,159,163,1,5,163,167,1,5,167,171,1,10,171,175,1,13,175,179,1,179,2,183,1,9,183,0,99,2,14,0,0";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user