Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #load "xunit"
- #load "AOC Connector"
- #load "AOC 2d Array"
- void Main()
- {
- RunTests(); // Call RunTests() or press Alt+Shift+T to initiate testing.
- //get aoc data
- var aoc = new AdventOfCode(2020, 4);
- var passPorts = GetPassPorts(aoc.InputLines);
- //solve A
- aoc.SubmitAnswer(SolveA(passPorts), Part.A);
- //solve B
- aoc.SubmitAnswer(SolveB(passPorts), Part.B);
- }
- public PassPort[] GetPassPorts(string[] data)
- {
- return string.Join("\r\n", data).Replace("\r\n\r\n", "*").Replace("\r\n", " ").Split('*')
- .Select(x => x.Split(' ').Select(y => y.Split(':')).ToDictionary(z => z[0], z => z[1]))
- .Select(x=> new PassPort(x)).ToArray();//.Dump();
- }
- public int SolveA(PassPort[] data) => data.Count(x => x.validA);
- public int SolveB(PassPort[] data) => data.Count(x => x.validB());
- public record PassPort(Dictionary<string, string> data)
- {
- static HashSet<string> validEyeColors = new HashSet<string>() { "amb", "blu","brn","gry","grn","hzl","oth"};
- public bool validA => data.Count == 8 || (data.Count == 7 && !data.ContainsKey("cid"));
- public bool validB()
- {
- //this.Dump();
- if(!validA) return false;
- if(!TestByr(data["byr"])) return false;
- if(!TestIYR(data["iyr"])) return false;
- if(!TestEYR(data["eyr"])) return false;
- if(!TestHGT(data["hgt"])) return false;
- if(!TestHCL(data["hcl"])) return false;
- if(!TestECL(data["ecl"])) return false;
- if(!TestPID(data["pid"])) return false;
- return true;
- }
- public static bool TestByr(string s) => int.Parse(s) is >= 1920 and <= 2002 ? true : false;
- public static bool TestIYR(string s) => int.Parse(s) is >= 2010 and <= 2020 ? true : false;
- public static bool TestEYR(string s) => int.Parse(s) is >= 2020 and <= 2030 ? true : false;
- public static bool TestHGT(string len)
- {
- if (!(len.EndsWith("in") || len.EndsWith("cm"))) return false;
- int lenN = int.Parse(len.Substring(0, len.Length - 2));
- if (len.EndsWith("cm") && (lenN is < 150 or > 193)) return false;
- if (len.EndsWith("in") && (lenN is < 59 or > 76)) return false;
- return true;
- }
- public static bool TestHCL(string s) => Regex.Match(s, "^#([[0-9a-f]{6})$").Success;
- public static bool TestECL(string s) => validEyeColors.Contains(s);
- public static bool TestPID(string s) => s.Length == 9 && int.TryParse(s, out int _);
- }
- // You can define other methods, fields, classes and namespaces here
- #region private::Tests
- #region test data
- string[] testData = new []
- {
- "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd",
- "byr:1937 iyr:2017 cid:147 hgt:183cm",
- "",
- "iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884",
- "hcl:#cfa07d byr:1929",
- "",
- "hcl:#ae17e1 iyr:2013",
- "eyr:2024",
- "ecl:brn pid:760753108 byr:1931",
- "hgt:179cm",
- "",
- "hcl:#cfa07d eyr:2025 pid:166559648",
- "iyr:2011 ecl:brn hgt:59in",
- };
- string[] invalidP = new []
- {
- "eyr:1972 cid:100",
- "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926",
- "",
- "iyr:2019",
- "hcl:#602927 eyr:1967 hgt:170cm",
- "ecl:grn pid:012533040 byr:1946",
- "",
- "hcl:dab227 iyr:2012",
- "ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277",
- "",
- "hgt:59cm ecl:zzz",
- "eyr:2038 hcl:74454a iyr:2023",
- "pid:3556412378 byr:2007",
- };
- string[] validP = new []
- {
- "pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980",
- "hcl:#623a2f",
- "",
- "eyr:2029 ecl:blu cid:129 byr:1989",
- "iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm",
- "",
- "hcl:#888785",
- "hgt:164cm byr:2001 iyr:2015 cid:88",
- "pid:545766238 ecl:hzl",
- "eyr:2022",
- "",
- "iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719",
- };
- #endregion
- //tests
- [Fact] void TestA() => Assert.Equal(2, SolveA(GetPassPorts(testData)));
- [Fact] void TestValid() => Assert.Equal(4, SolveB(GetPassPorts(validP)));
- [Fact] void TestInvalid() => Assert.Equal(0, SolveB(GetPassPorts(invalidP)));
- [Theory]
- [InlineData(1919, false)]
- [InlineData(1920, true)]
- [InlineData(2002, true)]
- [InlineData(2003, false)]
- void TestBYR(int test, bool validResult) => Assert.Equal(PassPort.TestByr(test.ToString()), validResult);
- [Theory]
- [InlineData(2009, false)]
- [InlineData(2010, true)]
- [InlineData(2020, true)]
- [InlineData(2021, false)]
- void TestIYR(int test, bool validResult) => Assert.Equal(PassPort.TestIYR(test.ToString()), validResult);
- [Theory]
- [InlineData(2019, false)]
- [InlineData(2020, true)]
- [InlineData(2030, true)]
- [InlineData(2031, false)]
- void TestEYR(int test, bool validResult) => Assert.Equal(PassPort.TestEYR(test.ToString()), validResult);
- [Theory]
- [InlineData("149cm", false)]
- [InlineData("150cm", true)]
- [InlineData("193cm", true)]
- [InlineData("194cm", false)]
- [InlineData("58in", false)]
- [InlineData("59in", true)]
- [InlineData("76in", true)]
- [InlineData("77in", false)]
- [InlineData("77", false)]
- void TestHGT(string test, bool validResult) => Assert.Equal(PassPort.TestHGT(test), validResult);
- [Theory]
- [InlineData("#123456", true)]
- [InlineData("#7890ab", true)]
- [InlineData("#cdef01", true)]
- [InlineData("#1234567", false)]
- [InlineData("1#123456", false)]
- void TestHCL(string test, bool validResult) => Assert.Equal(PassPort.TestHCL(test), validResult);
- [Theory]
- [InlineData("amb", true)]
- [InlineData("150cm", false)]
- [InlineData("as", false)]
- void TestECL(string test, bool validResult) => Assert.Equal(PassPort.TestECL(test), validResult);
- [Theory]
- [InlineData("000000001", true)]
- [InlineData("0123456789", false)]
- [InlineData("as", false)]
- void TestPID(string test, bool validResult) => Assert.Equal(PassPort.TestPID(test), validResult);
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement