Rough day 4 implementation

This commit is contained in:
2020-12-07 07:59:52 -06:00
parent f2545d7373
commit 2969e6ff44
4 changed files with 1253 additions and 0 deletions

1104
04input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -16,6 +16,9 @@
<None Update="03input.txt"> <None Update="03input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="04input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -7,6 +7,7 @@
Q01.Go(); Q01.Go();
Q02.Go(); Q02.Go();
Q03.Go(); Q03.Go();
Q04.Go();
} }
} }
} }

145
Q04.cs Normal file
View File

@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace _2020
{
class Q04
{
static List<Dictionary<string, string>> Passports = new List<Dictionary<string, string>>();
static Regex PassportRegex = new Regex(@"(?<key>[a-z]{3}):(?<val>\S+)", RegexOptions.Compiled);
static Regex HTMLColor = new Regex("^#[0-9a-f]{6}$", RegexOptions.Compiled);
static Regex PassportID = new Regex("^[0-9]{9}$", RegexOptions.Compiled);
public static void Go()
{
MakeList();
Part1();
Part2();
}
static void MakeList()
{
using var fs = new FileStream("04input.txt", FileMode.Open);
using var sr = new StreamReader(fs);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length == 0 || Passports.Count == 0)
{
Passports.Add(new Dictionary<string, string>());
}
var matches = PassportRegex.Matches(line);
for (int i = 0; i < matches.Count; i++)
{
Passports[Passports.Count - 1].Add(matches[i].Groups["key"].Value, matches[i].Groups["val"].Value);
}
}
}
static bool IsValid_Basic(Dictionary<string, string> passport)
{
if (passport.Count >= 8)
{
return true;
}
else if (passport.Count == 7 && !passport.ContainsKey("cid"))
{
return true;
}
return false;
}
static void Part1()
{
int numValid = 0;
foreach (var passport in Passports)
{
if (IsValid_Basic(passport))
{
numValid++;
}
}
Debug.WriteLine($"Q04Part1: num valid={numValid}");
}
static void Part2()
{
int numValid = 0;
foreach (var passport in Passports)
{
bool isValid = IsValid_Basic(passport);
foreach (var set in passport)
{
switch (set.Key)
{
case "byr":
{
var year = Convert.ToInt32(set.Value);
isValid = isValid && year >= 1920 && year <= 2002;
}
break;
case "iyr":
{
var year = Convert.ToInt32(set.Value);
isValid = isValid && year >= 2010 && year <= 2020;
}
break;
case "eyr":
{
var year = Convert.ToInt32(set.Value);
isValid = isValid && year >= 2020 && year <= 2030;
}
break;
case "hgt":
{
var val = 0;
var unit = "";
if (set.Value.Length > 2)
{
val = Convert.ToInt32(set.Value[..^2]);
unit = set.Value[^2..];
}
isValid = isValid && ((unit == "cm" && val >= 150 && val <= 193) || (unit == "in" && val >= 59 && val <= 76));
}
break;
case "hcl":
{
isValid = isValid && HTMLColor.IsMatch(set.Value);
}
break;
case "ecl":
{
isValid = isValid && (set.Value == "amb" || set.Value == "blu" || set.Value == "brn" || set.Value == "gry" || set.Value == "grn" || set.Value == "hzl" || set.Value == "oth");
}
break;
case "pid":
{
isValid = isValid && PassportID.IsMatch(set.Value);
}
break;
}
}
if (isValid)
{
numValid++;
}
}
Debug.WriteLine($"Q04Part2: num valid={numValid}");
}
}
}