You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.5 KiB

using AdventOfCode.Contracts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace AoC.TwentyTwenty
{
namespace AoC.TwentyTwenty
{
class Four : Day
{
public override int Year => 2020;
public override int DayNumber => 4;
public override void BigBoi(string[] input)
{
throw new NotImplementedException();
}
public override void Part1(string[] input)
{
IEnumerable<string> inputbutforreal = input.ToList();
inputbutforreal = inputbutforreal.Append("");
HashSet<string> keys = new HashSet<string>();
int valid = 0;
foreach (string s in inputbutforreal)
{
if (s == string.Empty)
{
if (keys.Count == 7 && !keys.Contains("cid") || keys.Count > 7)
{
valid += 1;
}
keys = new HashSet<string>();
continue;
}
keys.Add(s.Split(":")[0]);
}
Print("1", valid);
}
public override void Part2(string[] input)
{
IEnumerable<string> inputbutforreal = input.ToList();
inputbutforreal = inputbutforreal.Append("");
HashSet<string> keys = new HashSet<string>();
int valid = 0;
bool isValid = true;
foreach (string s in inputbutforreal)
{
if (s == string.Empty)
{
if (isValid && (keys.Count == 7 && !keys.Contains("cid") || keys.Count > 7))
{
valid += 1;
}
isValid = true;
keys = new HashSet<string>();
continue;
}
if (!isValid)
{
continue;
}
string key = s.Split(":")[0];
string value = s.Split(":")[1];
isValid = key switch
{
"byr" => Regex.IsMatch(value, "19([2-9][0-9])|200[0-2]"),
"iyr" => Regex.IsMatch(value, "20(1[0-9]|20)"),
"eyr" => Regex.IsMatch(value, "20(2[0-9]|30)"),
"hgt" => Regex.IsMatch(value, "^(1([5-8][0-9]|9[0-3])cm|(59|6[0-9]|7[0-3])in)$"),
"hcl" => Regex.IsMatch(value, "#[0-9a-f]{6}"),
"ecl" => Regex.IsMatch(value, "amb|blu|brn|gry|grn|hzl|oth"),
"pid" => Regex.IsMatch(value, "^\\d{9}$"),
"cid" => true,
_ => throw new Exception("NIGGERS")
};
if (key == "pid" && !Regex.IsMatch(value, "^\\d{9}$") && Regex.IsMatch(value, "\\d{9}"))
{
Console.WriteLine(value);
}
keys.Add(key);
}
Print("2", valid);
}
}
}
}