Created
December 4, 2020 05:51
-
-
Save RichardVasquez/51051f201647ae5c3f916aca2192e7d8 to your computer and use it in GitHub Desktop.
Day 4 Advent of Code 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using AdventOfCode.Library; | |
namespace AdventOfCode | |
{ | |
// Day 04 | |
// THIS IS JUNK! IT WORKS BUT IT IS JUNK! | |
internal static class Program | |
{ | |
private const bool DoPart1 = true; | |
private const bool DoPart2 = true; | |
public static List<string> keys = new List<string> | |
{ | |
"byr", | |
"iyr", | |
"eyr", | |
"hgt", | |
"hcl", | |
"ecl", | |
"pid", | |
"cid" | |
}; | |
// Day 04 | |
public static void Main() | |
{ | |
var data = TextUtility.ReadLines(removeBlank: false); | |
ProcessPart1(DoPart1, data); | |
ProcessPart2(DoPart2, data); | |
} | |
private static string SolverList1(List<string> data) | |
{ | |
List<int> blanks = new List<int> {0}; | |
for (int i = 0; i < data.Count; i++) | |
{ | |
if (data[i] == "") | |
{ | |
blanks.Add(i+1); | |
} | |
} | |
blanks.Add(data.Count); | |
var keyCount = keys.Count; | |
var total = 0; | |
for (int i = 0; i < blanks.Count - 1; i++) | |
{ | |
int k = blanks[i]; | |
int j = blanks[i + 1]; | |
List<string> lines = new List<string>(); | |
for (int m = k; m < j; m++) | |
{ | |
lines.Add(data[m]); | |
} | |
var text = string.Join(" ", lines.ToArray()); | |
var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); | |
List<string> check = new List<string>(); | |
foreach (var key in keys.Where(k=>k!="cid")) | |
{ | |
check.Add(key); | |
} | |
foreach (string part in parts) | |
{ | |
var kv = part.Split(':', StringSplitOptions.RemoveEmptyEntries)[0]; | |
if (check.Contains(kv) && kv != "cid") | |
{ | |
check.Remove(kv); | |
} | |
} | |
if (check.Count == 0) | |
{ | |
total++; | |
} | |
} | |
return $"{total}"; | |
} | |
private static string SolverList2(List<string> data) | |
{ | |
List<int> blanks = new List<int> {0}; | |
for (int i = 0; i < data.Count; i++) | |
{ | |
if (data[i] == "") | |
{ | |
blanks.Add(i+1); | |
} | |
} | |
blanks.Add(data.Count); | |
var keyCount = keys.Count; | |
var total = 0; | |
for (int i = 0; i < blanks.Count - 1; i++) | |
{ | |
int k = blanks[i]; | |
int j = blanks[i + 1]; | |
List<string> lines = new List<string>(); | |
for (int m = k; m < j; m++) | |
{ | |
lines.Add(data[m]); | |
} | |
var text = string.Join(" ", lines.ToArray()); | |
var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); | |
List<string> check = new List<string>(); | |
foreach (var key in keys.Where(k=>k!="cid")) | |
{ | |
check.Add(key); | |
} | |
foreach (string part in parts) | |
{ | |
var kvpair = part.Split(':', StringSplitOptions.RemoveEmptyEntries); | |
var kv = kvpair[0]; | |
var value = kvpair[1]; | |
if (check.Contains(kv) && kv != "cid") | |
{ | |
bool valid = false; | |
switch (kv) | |
{ | |
case "byr": | |
var goodLength1 = value.Length == 4; | |
int testInt1; | |
var goodYear1 = int.TryParse(value, out testInt1); | |
if (goodLength1 && goodYear1 && testInt1 >= 1902 && testInt1 <= 2002) | |
{ | |
valid = true; | |
} | |
break; | |
case "iyr": | |
var goodLength2 = value.Length == 4; | |
int testInt2; | |
var goodYear2 = int.TryParse(value, out testInt2); | |
if (goodLength2 && goodYear2 && testInt2 >= 2010 && testInt2 <= 2020) | |
{ | |
valid = true; | |
} | |
break; | |
case "eyr": | |
var goodLength3 = value.Length == 4; | |
int testInt3; | |
var goodYear3 = int.TryParse(value, out testInt3); | |
if (goodLength3 && goodYear3 && testInt3 >= 2020 && testInt3 <= 2030) | |
{ | |
valid = true; | |
} | |
break; | |
case "hgt": | |
if (!value.EndsWith("cm") && !value.EndsWith("in")) | |
{ | |
break; | |
} | |
var meas = value.Substring(value.Length - 2); | |
var num = value.Substring(0, value.Length - 2); | |
switch (meas) | |
{ | |
case "in": | |
int testInt4; | |
var goodMeas1 = int.TryParse(num, out testInt4); | |
if (goodMeas1 && testInt4 >= 59 && testInt4 <= 76) | |
{ | |
valid = true; | |
} | |
break; | |
case "cm": | |
int testInt5; | |
var goodMeas2 = int.TryParse(num, out testInt5); | |
if (goodMeas2 && testInt5 >= 150 && testInt5 <= 193) | |
{ | |
valid = true; | |
} | |
break; | |
} | |
break; | |
case "hcl": | |
if (string.IsNullOrEmpty(value)) | |
{ | |
break; | |
} | |
if (value[0] != '#') | |
{ | |
break; | |
} | |
var hex = "0123456789abcdef"; | |
int hc = value.Count(c => hex.Contains(c)); | |
if (hc == 6) | |
{ | |
valid = true; | |
} | |
break; | |
case "ecl": | |
if (new[] {"amb", "blu", "brn", "gry", "grn", "hzl", "oth"}.Contains(value)) | |
{ | |
valid = true; | |
} | |
break; | |
case "pid": | |
var digits = "0123456789"; | |
int dc = value.Count(c => digits.Contains(c)); | |
valid = dc == 9; | |
break; | |
} | |
if (valid) | |
{ | |
check.Remove(kv); | |
} | |
} | |
} | |
if (check.Count == 0) | |
{ | |
total++; | |
} | |
} | |
return $"{total}"; | |
} | |
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 SolverList1(List<string> data) | |
{ | |
return "-L1-"; | |
} | |
private static string SolverList2(List<string> data) | |
{ | |
return "-L2-"; | |
} | |
*/ | |
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