Skip to content

Instantly share code, notes, and snippets.

@RichardVasquez
Created December 1, 2020 17:12
Show Gist options
  • Save RichardVasquez/8392fd19a4e8be4059a267c1a42307a8 to your computer and use it in GitHub Desktop.
Save RichardVasquez/8392fd19a4e8be4059a267c1a42307a8 to your computer and use it in GitHub Desktop.
Day 1 Advent of Code 2020
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using AdventOfCode.Library;
namespace AdventOfCode
{
// Day 01
internal static class Program
{
private static bool _doPart1 = true;
private static bool _doPart2 = true;
public static void Main()
{
// Process your data here
var data = TextUtility.ReadLines(removeBlank: true);
var finalData = data.ToList();
// Get ready to track everything
var watch1 = new Stopwatch();
var watch2 = new Stopwatch();
var answer1 = "";
var answer2 = "";
// Solve problems.
if (_doPart1)
{
watch1.Start();
answer1 = Part1(finalData);
watch1.Stop();
}
if (_doPart2)
{
watch2.Start();
answer2 = Part2(finalData);
watch2.Stop();
}
// Do output
if (!string.IsNullOrEmpty(answer1) || !string.IsNullOrEmpty(answer2))
{
Console.WriteLine("Results");
if (_doPart1)
{
Console.WriteLine($"PART 1 ANSWER:\t{answer1}\n{watch1.ElapsedMilliseconds} ms");
}
if (_doPart2)
{
Console.WriteLine($"PART 2 ANSWER:\t{answer2}\n{watch2.ElapsedMilliseconds} ms");
}
}
else
{
Console.WriteLine("No results have been activated.");
}
}
private static string Part1(IEnumerable<string> data)
{
var numbers = data.Select(long.Parse).ToList();
const long baseValue = 2020;
foreach (long index in numbers.Where(idx => numbers.Contains(baseValue - idx)))
{
return (index * (baseValue - index)).ToString();
}
return "-1";
}
// ReSharper disable once UnusedParameter.Local
// ReSharper disable once UnusedMember.Local
private static string Part2(IEnumerable<string> data)
{
var numbers = data.Select(long.Parse).ToList();
const long baseValue = 2020;
foreach (long index1 in numbers)
{
long temp = baseValue - index1;
foreach (long index2 in numbers.Where(index => index != index1))
{
if (numbers.Contains(temp - index2))
{
return (index1 * (temp - index2) * index2).ToString();
}
}
}
return "-1";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment