mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-02-16 10:48:30 -05:00
Some reorg so I can have the original inputs
This commit is contained in:
61
Day2/Day2.cs
Normal file
61
Day2/Day2.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Advent
|
||||
{
|
||||
public static class Day2
|
||||
{
|
||||
public static void Execute()
|
||||
{
|
||||
for (var noun = 0; noun <= 99; noun++)
|
||||
for (var verb = 0; verb <= 99; verb++)
|
||||
{
|
||||
var result = Calculate(noun, verb);
|
||||
|
||||
if (result == 19690720)
|
||||
Console.WriteLine(100 * noun + verb);
|
||||
}
|
||||
}
|
||||
|
||||
private static int Calculate(int noun, int verb)
|
||||
{
|
||||
var lines = File.ReadAllLines(@".\Day2\input.txt");
|
||||
|
||||
var codes = lines[0].Split(',').Select(int.Parse).ToArray();
|
||||
|
||||
codes[1] = noun;
|
||||
codes[2] = verb;
|
||||
|
||||
var position = 0;
|
||||
var done = false;
|
||||
|
||||
while (!done)
|
||||
{
|
||||
switch (codes[position])
|
||||
{
|
||||
case 1:
|
||||
codes[codes[position + 3]] = codes[codes[position + 1]] + codes[codes[position + 2]];
|
||||
|
||||
position += 4;
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
codes[codes[position + 3]] = codes[codes[position + 1]] * codes[codes[position + 2]];
|
||||
|
||||
position += 4;
|
||||
|
||||
break;
|
||||
|
||||
case 99:
|
||||
done = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return codes[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user