Skip to content

Instantly share code, notes, and snippets.

@RichardVasquez
Created December 3, 2020 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardVasquez/6ef03fc1b1c7124d65c3d1b40cb04138 to your computer and use it in GitHub Desktop.
Save RichardVasquez/6ef03fc1b1c7124d65c3d1b40cb04138 to your computer and use it in GitHub Desktop.
Day 3 Advent of Code 2020
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using AdventOfCode.Library;
namespace AdventOfCode
{
internal static class Program
{
private const bool DoPart1 = true;
private const bool DoPart2 = true;
// Day 03
public static void Main()
{
var data = TextUtility.ReadLines(removeBlank: true);
ProcessPart1(DoPart1, data);
ProcessPart2(DoPart2, data);
}
private static string SolverList1(List<string> data)
{
var x = 0;
var trees = 0;
foreach (string line in data)
{
if (line[x] == '#')
{
trees++;
}
x += 3;
x %= data[0].Length;
}
return $"{trees}";
}
private static string SolverList2(List<string> data)
{
var xVals = new List<int> {1, 3, 5, 7, 1};
var yVals = new List<int> {1, 1, 1, 1, 2};
var treesHit = new List<int> {0, 0, 0, 0, 0};
for (var i = 0; i < xVals.Count; i++)
{
var x = 0;
for (var y = 0; y < data.Count; y += yVals[i])
{
if (data[y][x] == '#')
{
treesHit[i]++;
}
x += xVals[i];
x %= data[0].Length;
}
}
long product = treesHit.Aggregate<int, long>(1, (current, i) => current * i);
return $"{product}";
}
private static void ProcessPart1(bool flag, object data)
{
if (!flag)
{
return;
}
var watch = new Stopwatch();
watch.Start();
string answer = data switch
{
List<string> simpleList => SolverList1(simpleList),
List<List<string>> jaggedList => SolverJagged1(jaggedList),
string plainString => SolverPlain1(plainString),
_ => "-X1"
};
watch.Stop();
WriteOutput(1, answer, watch.ElapsedMilliseconds);
}
private static void ProcessPart2(bool flag, object data)
{
if (!flag)
{
return;
}
var watch = new Stopwatch();
watch.Start();
string answer = data switch
{
List<string> simpleList => SolverList2(simpleList),
List<List<string>> jaggedList => SolverJagged2(jaggedList),
string plainString => SolverPlain2(plainString),
_ => "-X2"
};
watch.Stop();
WriteOutput(2, answer, watch.ElapsedMilliseconds);
}
private static void WriteOutput(int index, string answer, long milliseconds)
{
Console.WriteLine($"PART {index} ANSWER:\t{answer}\n{milliseconds} ms");
}
private static string SolverPlain1(string data)
{
return "-S1-";
}
private static string SolverPlain2(string data)
{
return "-S2-";
}
private static string SolverJagged1(List<List<string>> data)
{
return "-J1-";
}
private static string SolverJagged2(List<List<string>> data)
{
return "-J2-";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment