Skip to content

Instantly share code, notes, and snippets.

@Logxn
Last active December 8, 2019 23:21
Show Gist options
  • Save Logxn/18e01fc17c15c81d1a25cf99bac5d85a to your computer and use it in GitHub Desktop.
Save Logxn/18e01fc17c15c81d1a25cf99bac5d85a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using RestSharp;
namespace Advent_of_Code
{
internal class Program
{
private static RestClient _client;
private static int[] _input;
private static void Main()
{
_client = new RestClient("https://adventofcode.com/2019/day/");
_client.AddDefaultHeader("cookie",
"session=53616c7465645f5fd648f66a011c9683fefcd8bb01fd179f0d7fda674f632de6397118eefeb55757648333b605b4a724");
DayOne();
DayTwo();
DayFour();
Console.ReadLine();
}
private static void DayOne()
{
var request = new RestRequest("1/input");
var input = _client.Execute(request, Method.GET).Content.Split("\n");
var complexSum = input.Where(val => !string.IsNullOrWhiteSpace(val)).Sum(val => ComplexFuelConsumption(Convert.ToDouble(val)));
var simpleSum = input.Where(val => !string.IsNullOrWhiteSpace(val))
.Sum(val => SimpleFuelConsumption(Convert.ToDouble(val)));
Console.WriteLine($"[Day 1] Part I: Simple Fuel Consumption -> {simpleSum}");
Console.WriteLine($"[Day 1] Part II: Complex Fuel Consumption -> {complexSum}");
}
private static void DayTwo()
{
var request = new RestRequest("2/input");
_input = _client.Execute(request, Method.GET).Content.Split(",").Select(int.Parse).ToArray();
_input[1] = 12;
_input[2] = 2;
// ToDo: Fix order
var result = NounVerb();
Console.WriteLine($"[Day 2] Part II: 100 * noun + verb is {result}");
result = Opcode(_input, 0);
Console.WriteLine($"[Day 2] Part I: The value left at position 0 is: {result}");
}
/*
* Password Criteria:
* - 6 digits
* - Withing range
* - Adjacent digits are the same
* - L -> R digits never decrease
*
* Part 2:
* - Adjacent digits are never a part of a lager group (adjacent !> 2)
*/
private static void DayFour()
{
var result = 0;
for (var i = 264793; i <= 803935; i++)
{
var previous = -1;
var found = false;
foreach (var digit in i.ToString())
{
if (previous > Convert.ToInt32(digit))
{
found = false;
break;
}
if (!Convert.ToInt32(digit).Equals(previous))
{
previous = Convert.ToInt32(digit);
}
else
{
found = true;
}
}
if (found) result++;
}
Console.WriteLine($"[Day 4] Part I: Found {result} different passwords within the given range.");
}
/*
* Instructions
* Index 0: Opcode -> 1 = Add, 2 = Multiply
* Index 1: Index to where first num is stored to add/multiply
* Index 2: Index to where second num is stored to add/multiply
* Index 3: Index to where the calculation result should be stored
*/
private static int Opcode(IList<int> input, int startIndex)
{
while (true)
{
var opcode = input[startIndex];
switch (opcode)
{
case 1:
// Addition
var result = input[input[startIndex + 1]] + input[input[startIndex + 2]];
var indexToStore = input[startIndex + 3];
input[indexToStore] = result;
startIndex += 4;
continue;
case 2:
// Multiplication
result = input[input[startIndex + 1]] * input[input[startIndex + 2]];
indexToStore = input[startIndex + 3];
input[indexToStore] = result;
startIndex += 4;
continue;
case 99:
// End Opcode
return input[0];
default:
// Unknown Opcode
Console.WriteLine($"[Day 2] - Error: Received opcode <{opcode}> at index <{startIndex}>");
return -1337;
}
}
}
private static int NounVerb()
{
var verb = 0;
while (verb < 99)
{
var noun = 0;
while (noun < 99)
{
var newInput = new int[_input.Length];
Array.Copy(_input, newInput, _input.Length);
newInput[1] = noun;
newInput[2] = verb;
var result = Opcode(newInput, 0);
if (result == 19690720)
{
return 100 * noun + verb;
}
noun++;
}
verb++;
}
return -1337;
}
/*
* Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
*/
private static double SimpleFuelConsumption(double mass)
{
return Math.Floor(mass / 3) - 2;
}
/*
So, for each module mass, calculate its fuel and add it to the total.
Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative.
*/
private static double ComplexFuelConsumption(double mass)
{
var sum = 0.0;
var solution = Math.Floor(mass / 3) - 2;
if (solution <= 0) return 0.0;
sum += solution;
while (Math.Abs(solution) > 0)
{
solution = Math.Floor(solution / 3) - 2;
if (solution < 0) break;
sum += solution;
}
return sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment