Skip to content

Instantly share code, notes, and snippets.

@RichardVasquez
Created December 2, 2020 05:29
Show Gist options
  • Save RichardVasquez/14bbd239de4933ecb5c02417096275ea to your computer and use it in GitHub Desktop.
Save RichardVasquez/14bbd239de4933ecb5c02417096275ea to your computer and use it in GitHub Desktop.
Day 2 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 02
public static void Main()
{
// Process your data here
var data = TextUtility.ReadWords(separators: new[] {' ', '-', ':', '\n', '\r'}, 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 parts = data.ToList();
var total = 0;
for (var k = 0; k < parts.Count; k += 4)
{
int count1 = int.Parse(parts[k]);
int count2 = int.Parse(parts[k + 1]);
char rule = parts[k + 2][0];
string pass = parts[k + 3];
int countLetters = pass.Count(x => x == rule);
if (countLetters >= count1 && countLetters <= count2)
{
total++;
}
}
return total.ToString();
}
private static string Part2(IEnumerable<string> data)
{
var parts = data.ToList();
var total = 0;
for (var k = 0; k < parts.Count; k += 4)
{
int count1 = int.Parse(parts[k]);
int count2 = int.Parse(parts[k + 1]);
char rule = parts[k + 2][0];
string pass = parts[k + 3];
if (pass[count1-1] == rule ^ pass[count2-1] == rule)
{
total++;
}
}
return total.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment