From e424a1ebf38576b2d41e0e14999dab281ff33434 Mon Sep 17 00:00:00 2001 From: Akumatic Date: Fri, 4 Dec 2020 14:14:48 +0100 Subject: [PATCH] 2020 Day 04 --- 2020/04/README.md | 128 ++++++ 2020/04/code.py | 37 ++ 2020/04/input.txt | 1031 ++++++++++++++++++++++++++++++++++++++++++ 2020/04/passport.py | 60 +++ 2020/04/solution.txt | 2 + 2020/04/test_code.py | 42 ++ 2020/README.md | 2 +- README.md | 2 +- 8 files changed, 1302 insertions(+), 2 deletions(-) create mode 100644 2020/04/README.md create mode 100644 2020/04/code.py create mode 100644 2020/04/input.txt create mode 100644 2020/04/passport.py create mode 100644 2020/04/solution.txt create mode 100644 2020/04/test_code.py diff --git a/2020/04/README.md b/2020/04/README.md new file mode 100644 index 0000000..ff56e94 --- /dev/null +++ b/2020/04/README.md @@ -0,0 +1,128 @@ +# 2020 Day 4: Passport Processing +Copyright (c) Eric Wastl +#### [Direct Link](https://adventofcode.com/2020/day/4) + +## Part 1 + +You arrive at the airport only to realize that you grabbed your North Pole Credentials instead of your passport. While these documents are extremely similar, North Pole Credentials aren't issued by a country and therefore aren't actually valid documentation for travel in most of the world. + +It seems like you're not the only one having problems, though; a very long line has formed for the automatic passport scanners, and the delay could upset your travel itinerary. + +Due to some questionable network security, you realize you might be able to solve both of these problems at the same time. + +The automatic passport scanners are slow because they're having trouble **detecting which passports have all required fields**. The expected fields are as follows: + +- `byr` (Birth Year) +- `iyr` (Issue Year) +- `eyr` (Expiration Year) +- `hgt` (Height) +- `hcl` (Hair Color) +- `ecl` (Eye Color) +- `pid` (Passport ID) +- `cid` (Country ID) + +Passport data is validated in batch files (your puzzle input). Each passport is represented as a sequence of `key:value` pairs separated by spaces or newlines. Passports are separated by blank lines. + +Here is an example batch file containing four passports: + +``` +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 +``` + +The first passport is **valid** - all eight fields are present. The second passport is **invalid** - it is missing `hgt` (the Height field). + +The third passport is interesting; the **only missing field** is `cid`, so it looks like data from North Pole Credentials, not a passport at all! Surely, nobody would mind if you made the system temporarily ignore missing `cid` fields. Treat this "passport" as **valid**. + +The fourth passport is missing two fields, `cid` and `byr`. Missing `cid` is fine, but missing any other field is not, so this passport is **invalid**. + +According to the above rules, your improved system would report **`2`** valid passports. + +Count the number of **valid** passports - those that have all required fields. Treat `cid` as optional. **In your batch file, how many passports are valid?** + +## Part 2 + +The line is moving more quickly now, but you overhear airport security talking about how passports with invalid data are getting through. Better add some data validation, quick! + +You can continue to ignore the `cid` field, but each other field has strict rules about what values are valid for automatic validation: + +- `byr` (Birth Year) - four digits; at least `1920` and at most `2002`. +- `iyr` (Issue Year) - four digits; at least `2010` and at most `2020`. +- `eyr` (Expiration Year) - four digits; at least `2020` and at most `2030`. +- `hgt` (Height) - a number followed by either `cm` or `in`: + - If `cm`, the number must be at least `150` and at most `193`. + - If `in`, the number must be at least `59` and at most `76`. +- `hcl` (Hair Color) - a `#` followed by exactly six characters `0`-`9` or `a`-`f`. +- `ecl` (Eye Color) - exactly one of: `amb` `blu` `brn` `gry` `grn` `hzl` `oth`. +- `pid` (Passport ID) - a nine-digit number, including leading zeroes. +- `cid` (Country ID) - ignored, missing or not. + +Your job is to count the passports where all required fields are both **present** and **valid** according to the above rules. Here are some example values: + +``` +byr valid: 2002 +byr invalid: 2003 + +hgt valid: 60in +hgt valid: 190cm +hgt invalid: 190in +hgt invalid: 190 + +hcl valid: #123abc +hcl invalid: #123abz +hcl invalid: 123abc + +ecl valid: brn +ecl invalid: wat + +pid valid: 000000001 +pid invalid: 0123456789 +``` + +Here are some invalid passports: + +``` +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 +``` + +Here are some valid passports: + +``` +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 +``` + +Count the number of **valid** passports - those that have all required fields **and valid values**. Continue to treat `cid` as optional. **In your batch file, how many passports are valid?** \ No newline at end of file diff --git a/2020/04/code.py b/2020/04/code.py new file mode 100644 index 0000000..400be86 --- /dev/null +++ b/2020/04/code.py @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2020 Akumatic +# +# https://adventofcode.com/2020/day/4 + +import passport + +def readFile() -> list: + with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f: + return [line[:-1] for line in f.readlines()] + +def parse_data(vals: list) -> list: + result = list() + cur = list() + for line in vals: + if line: + cur += line.split(" ") + else: + result.append(passport.Pass(cur)) + cur.clear() + + # since last line is not "", add data from cur once more + result.append(passport.Pass(cur)) + return result + +def part1(passports: list) -> int: + return sum(p.valid_fields() for p in passports) + +def part2(passports: list) -> int: + return sum(p.valid_data() for p in passports) + +if __name__ == "__main__": + from test_code import test + test() + passports = parse_data(readFile()) + print(f"Part 1: {part1(passports)}") + print(f"Part 2: {part2(passports)}") \ No newline at end of file diff --git a/2020/04/input.txt b/2020/04/input.txt new file mode 100644 index 0000000..3fd512c --- /dev/null +++ b/2020/04/input.txt @@ -0,0 +1,1031 @@ +byr:1937 +eyr:2030 pid:154364481 +hgt:158cm iyr:2015 ecl:brn hcl:#c0946f cid:155 + +cid:279 +eyr:2029 +pid:675014709 ecl:amb +byr:1985 hgt:179in hcl:z iyr:2025 + +iyr:2011 hgt:181cm hcl:#341e13 pid:282499883 byr:1953 +eyr:2023 +ecl:brn + +eyr:2040 iyr:1984 pid:2371396209 byr:1951 cid:283 hgt:164cm +hcl:#623a2f + +iyr:2014 byr:1966 hgt:153cm pid:900693718 eyr:2020 ecl:gry hcl:#866857 + +eyr:2020 hgt:162cm +byr:1939 pid:900852891 iyr:2020 +ecl:oth hcl:#b6652a + +iyr:1956 +hcl:2385ac +hgt:167in +ecl:#9b82e8 +eyr:2003 +cid:214 byr:2012 pid:483285062 + +cid:146 byr:1938 hcl:#c0946f pid:136823471 +hgt:180cm iyr:2011 +eyr:2020 ecl:gry + +cid:100 hgt:187cm +byr:1973 ecl:blu +iyr:2020 +hcl:#888785 eyr:2030 +pid:386799181 + +ecl:#dbba4f +hcl:z iyr:2028 pid:193cm +byr:2029 +eyr:1962 hgt:113 + +cid:269 byr:1928 pid:604152679 +iyr:2017 eyr:2027 +hcl:#c0946f +hgt:189cm +ecl:blu + +pid:653548437 hcl:z cid:166 iyr:2011 eyr:1967 +hgt:162 ecl:xry byr:2019 + +byr:1979 ecl:hzl iyr:2018 hcl:#7d3b0c +eyr:2026 +pid:981366150 hgt:151cm + +pid:055334062 cid:272 eyr:2025 ecl:grn iyr:2016 hgt:159cm hcl:#a97842 byr:1989 + +eyr:2033 +hgt:170cm ecl:brn pid:#299cf2 hcl:#602927 byr:2004 iyr:2023 + +iyr:2020 cid:273 +pid:038972962 +hcl:#c0946f hgt:152cm +eyr:2021 ecl:blu byr:1986 + +pid:015622106 hgt:154cm iyr:2012 byr:1922 +eyr:2028 hcl:#9dc667 ecl:blu + +iyr:2017 eyr:2025 hgt:179cm cid:324 +pid:579870519 byr:1934 + +hcl:#6b5442 eyr:2021 +pid:104071480 ecl:brn +hgt:162cm cid:89 byr:1949 +iyr:2017 + +pid:388871409 cid:163 hgt:190cm ecl:amb eyr:2022 iyr:2019 byr:1963 hcl:#fffffd + +cid:190 byr:1968 iyr:2018 +hgt:155cm hcl:#6b5442 ecl:grn +pid:891458997 eyr:2030 + +hcl:z +cid:103 iyr:2026 hgt:171 byr:1979 +pid:076174078 +eyr:2031 ecl:#fc4638 + +hcl:#f8063e pid:684921958 ecl:gry eyr:2020 iyr:2011 +byr:1958 hgt:189cm +cid:131 + +ecl:gry cid:90 iyr:2010 hgt:165cm +pid:683095019 byr:1953 +hcl:#341e13 +eyr:2030 + +pid:000303662 +ecl:amb iyr:2014 hcl:#733820 byr:1964 hgt:184cm cid:149 +eyr:2027 + +hcl:#341e13 hgt:150cm byr:1999 eyr:2022 pid:950260967 cid:72 +ecl:grn iyr:2012 + +cid:159 byr:1987 +hcl:#623a2f pid:144877169 eyr:2021 ecl:brn hgt:164cm + +pid:759578668 +cid:235 ecl:amb hgt:154cm +eyr:2023 byr:1941 hcl:#fffffd +iyr:2023 + +pid:18101245 hcl:z hgt:172in iyr:1947 byr:1956 ecl:grn eyr:1964 + +eyr:1980 pid:#8d62b1 ecl:#3e8868 cid:235 +iyr:1978 hcl:#efcc98 +byr:2025 hgt:181in + +hgt:64cm hcl:3bc533 iyr:2019 byr:2015 pid:158cm eyr:2027 ecl:#1abbd7 + +eyr:2020 iyr:2011 +pid:739854643 byr:1941 hgt:179cm hcl:#602927 ecl:gry cid:330 + +hcl:#a97842 iyr:2011 hgt:167cm pid:527353688 byr:1927 cid:256 eyr:2026 ecl:grn + +iyr:2011 pid:350783151 hgt:168cm +byr:1961 ecl:amb hcl:#341e13 +cid:181 + +eyr:2025 hgt:160cm +ecl:brn hcl:#cfa07d pid:136929481 +iyr:2017 + +ecl:oth byr:1994 iyr:2020 hgt:162cm pid:578918570 eyr:2027 hcl:#fffffd +cid:325 + +hgt:63in byr:1925 +ecl:amb iyr:2014 cid:224 +hcl:#341e13 pid:208286938 + +ecl:#76434f hgt:97 +hcl:z eyr:1968 pid:#871dc8 iyr:1955 +byr:2007 + +byr:2011 +eyr:1962 hcl:9ac7b8 pid:6836105558 +ecl:#d7084f +iyr:2023 hgt:76cm + +eyr:2016 iyr:2030 hgt:107 pid:948192703 byr:2013 +hcl:b80af7 ecl:#d47439 + +eyr:2027 iyr:2017 +pid:#27d2a6 byr:1921 ecl:brn hcl:#c0946f hgt:59in + +byr:1982 hcl:#ceb3a1 ecl:brn eyr:2020 hgt:63in +iyr:2017 + +ecl:brn hgt:152cm byr:1962 +iyr:2013 +pid:381950515 hcl:#2afabd eyr:2020 + +hgt:158cm ecl:gry pid:252068098 +cid:281 eyr:2023 byr:1950 iyr:2016 +hcl:#ceb3a1 + +byr:1922 +hgt:187cm cid:74 +hcl:#a97842 +ecl:amb +pid:045338811 +eyr:2025 iyr:2019 + +hgt:176cm eyr:2022 iyr:2019 byr:1938 ecl:blu pid:610286298 +hcl:#a97842 + +iyr:2028 byr:2025 +eyr:2039 pid:72530856 ecl:#a819ad hgt:69 hcl:z + +iyr:2018 eyr:2030 +hcl:#8b1e1f ecl:hzl byr:1971 +pid:189005628 + +pid:531154383 eyr:2020 hcl:#733820 ecl:oth +hgt:72in +iyr:2016 +byr:1983 + +hgt:178cm hcl:#623a2f pid:562756202 +eyr:2020 +byr:1929 ecl:oth +iyr:2013 + +iyr:2013 byr:1983 pid:510998389 ecl:gry +cid:99 hcl:#a97842 + +ecl:gry eyr:2026 +cid:202 hgt:191cm byr:1981 +pid:414853225 +hcl:#7d3b0c + +hcl:#866857 byr:1951 eyr:2020 hgt:169cm pid:844496997 +iyr:2016 ecl:brn + +pid:083641628 iyr:2019 eyr:2030 +byr:1938 ecl:hzl hcl:#b6652a hgt:68in + +hcl:#18171d +byr:1981 +pid:425911593 cid:309 eyr:2026 ecl:brn hgt:168cm + +hcl:c84fda pid:#80d5e6 byr:1925 hgt:162cm iyr:2026 +eyr:2036 ecl:#62c166 + +pid:461660733 eyr:2028 +hgt:167cm ecl:hzl hcl:#cfa07d iyr:2010 +cid:183 + +eyr:1930 byr:2021 iyr:2016 pid:163cm +hcl:79428f cid:136 +ecl:amb + +hcl:#efcc98 hgt:176cm eyr:2022 ecl:brn iyr:2013 pid:541033681 byr:1952 + +hgt:59cm +ecl:#d9b0bc pid:#3ab4b4 hcl:7aa29f +eyr:2034 +byr:2011 cid:266 iyr:2004 + +byr:1991 hcl:#cfa07d cid:259 hgt:67cm eyr:2024 pid:631601343 ecl:brn iyr:2011 + +byr:2020 +ecl:gry iyr:1944 +pid:421649652 hgt:107 +hcl:6d8593 eyr:2026 + +ecl:blu +byr:1953 hgt:66in hcl:#888785 eyr:2021 pid:174284708 + +ecl:#8f2c20 +pid:27713962 iyr:2010 cid:149 byr:1952 hcl:#866857 eyr:2030 +hgt:186cm + +eyr:2023 byr:1989 ecl:blu +hcl:#18171d pid:480641 iyr:2020 +hgt:74in + +hcl:#efcc98 +eyr:2023 hgt:157cm byr:1968 +iyr:2013 +ecl:amb pid:436599087 + +eyr:2021 +hgt:175cm hcl:#efcc98 ecl:hzl pid:701245439 +byr:1943 iyr:2015 + +hgt:164cm pid:310546869 +eyr:2021 hcl:#b6652a ecl:oth iyr:2011 cid:181 +byr:2001 + +byr:2016 hcl:94f4b9 eyr:1922 hgt:155cm ecl:hzl iyr:2009 pid:83440078 + +iyr:2014 hgt:60in +eyr:2031 +hcl:#c0946f +byr:1948 ecl:grt pid:117909137 + +byr:1965 hgt:186cm iyr:2010 cid:117 hcl:#602927 pid:806868279 +eyr:2029 +ecl:grn + +ecl:amb hgt:187cm hcl:#c0946f byr:1935 +cid:139 iyr:2019 eyr:2023 + +pid:925865182 +byr:1952 hgt:158cm hcl:#619e03 ecl:gry eyr:2026 +iyr:2016 + +iyr:2012 hgt:73in +cid:199 +byr:1959 +ecl:hzl hcl:#fcd2f7 pid:268810283 + +eyr:2025 +pid:317239854 ecl:brn byr:1955 hcl:#fffffd cid:220 hgt:175cm +iyr:2015 + +ecl:blu +byr:1939 hcl:#3332c0 +iyr:2019 pid:832588457 eyr:2030 hgt:192cm + +hgt:64cm cid:326 ecl:oth byr:2025 eyr:2025 hcl:z iyr:2012 pid:0394683801 + +ecl:amb byr:1966 +pid:734257450 hgt:65in hcl:#fffffd iyr:2018 eyr:2025 +cid:287 + +cid:249 +eyr:2034 ecl:#7b7bf5 byr:2009 pid:171cm hcl:709fe4 + +hcl:d7c488 ecl:#b9bbb2 +pid:55277333 +cid:90 byr:2022 iyr:1978 +hgt:69cm eyr:1981 + +hcl:z +byr:1949 pid:057966198 eyr:1941 +iyr:2025 + +hgt:168cm ecl:hzl hcl:#866857 iyr:2011 pid:052204052 eyr:2030 +byr:1970 + +pid:318707043 eyr:2029 ecl:hzl hcl:#888785 +byr:1932 iyr:2010 +hgt:166cm + +hcl:z iyr:2010 ecl:oth byr:2006 cid:120 pid:#15d5ef eyr:2039 hgt:73cm + +hcl:96c534 hgt:193in eyr:2036 byr:2008 ecl:#090691 cid:109 pid:603339169 iyr:2027 + +iyr:2029 +ecl:utc +pid:5845500732 hcl:2a32b8 +byr:2009 eyr:2040 +cid:305 hgt:71cm + +byr:1994 iyr:2015 +eyr:2026 +hcl:#623a2f pid:678627790 +hgt:176cm ecl:blu + +byr:2005 hcl:z +ecl:zzz pid:#47729a eyr:2039 hgt:62cm iyr:1983 + +hcl:#cfa07d ecl:blu +iyr:2014 eyr:2025 pid:824483836 hgt:174cm byr:1933 + +hgt:70in +iyr:2010 cid:53 eyr:2023 pid:093704222 ecl:amb hcl:#a97842 byr:1945 + +ecl:brn hcl:#602927 byr:1986 eyr:2029 +pid:422160398 +cid:134 +hgt:156cm +iyr:2020 + +iyr:2014 hgt:187cm byr:1921 eyr:2022 ecl:amb + +pid:765750572 eyr:2021 +ecl:hzl +iyr:1997 hgt:66 + +iyr:2016 +ecl:blu +cid:335 +hgt:175cm pid:297297703 hcl:#b6652a +eyr:2030 + +hgt:64in hcl:#866857 pid:159cm byr:1972 +cid:321 eyr:2021 ecl:brn +iyr:2017 + +pid:884038003 +iyr:2016 hgt:188cm ecl:grn +byr:1928 eyr:2027 hcl:#341e13 + +pid:020315021 hgt:60in ecl:amb iyr:2018 +hcl:#602927 +eyr:2028 +byr:1935 + +byr:1946 eyr:2021 hcl:#efcc98 iyr:2015 +cid:52 +hgt:177cm +pid:841348804 + +ecl:gry eyr:2027 +byr:1959 pid:482204272 +hcl:#888785 +iyr:2013 + +hcl:#623a2f +eyr:2027 iyr:2011 hgt:189cm pid:767923916 ecl:hzl +byr:1987 + +hgt:152in hcl:#efcc98 ecl:hzl eyr:2030 byr:1971 iyr:1978 pid:499148724 +cid:194 + +pid:#87513f hcl:e2a5de byr:2015 iyr:2016 hgt:155cm eyr:2024 +ecl:gry cid:123 + +byr:1977 +iyr:2028 +hcl:#888785 pid:69801913 eyr:2026 hgt:165in ecl:brn + +byr:1938 hcl:#efcc98 hgt:154cm pid:621714851 eyr:1972 ecl:gry iyr:2014 cid:161 + +hcl:#a97842 +hgt:184cm ecl:oth pid:009682326 +cid:233 eyr:2023 iyr:2019 byr:1982 + +hcl:#ceb3a1 pid:700761574 eyr:2020 byr:1940 iyr:2017 +ecl:gry hgt:76in + +iyr:2017 cid:73 +ecl:hzl +hcl:#efcc98 pid:363441284 +hgt:177cm byr:1924 eyr:2028 + +hcl:#a97842 eyr:2023 byr:1989 pid:589573553 +ecl:oth +iyr:2017 hgt:162cm + +ecl:oth +byr:2001 hgt:167cm hcl:#18171d eyr:2028 iyr:2015 pid:253666913 + +byr:1943 ecl:amb +hcl:#888785 pid:163cm iyr:2025 hgt:76 eyr:2026 + +byr:1963 eyr:2030 +hgt:158in iyr:2023 pid:399153486 hcl:#733820 + +pid:43290252 hgt:167cm cid:302 iyr:2016 hcl:f81436 +byr:1992 ecl:gry +eyr:1922 + +pid:487300118 hgt:167cm hcl:#888785 +iyr:2018 ecl:oth cid:286 byr:1971 eyr:2021 + +eyr:2036 +hgt:94 +byr:2013 +cid:162 hcl:37408d pid:527721797 ecl:dne +iyr:2022 + +hcl:5e67df byr:1973 pid:48217944 hgt:65in eyr:2023 +iyr:2001 ecl:gry + +hcl:717ef8 eyr:1996 pid:154cm ecl:#e2c90e +iyr:2028 +byr:2009 hgt:184cm + +iyr:2015 pid:549968053 hcl:#efcc98 +hgt:151cm eyr:2025 +byr:1993 +ecl:gry + +hgt:165cm ecl:gry +iyr:2020 +byr:1927 hcl:#602927 pid:278946330 +eyr:2027 + +eyr:2031 +ecl:#b01aae pid:151cm +cid:188 iyr:2025 +hgt:60cm byr:2015 + +eyr:2028 pid:928740840 +byr:1931 +hgt:191cm iyr:2014 cid:305 +hcl:#6b5442 +ecl:oth + +hcl:#7d3b0c cid:283 byr:1923 ecl:amb +pid:781290431 iyr:2019 +eyr:2021 +hgt:191cm + +byr:2027 +pid:51702036 +iyr:2028 hgt:165in ecl:gry hcl:z +eyr:2036 + +byr:1971 +ecl:blu iyr:2012 pid:986042111 +eyr:2021 hcl:#888785 +hgt:68in + +hgt:183cm byr:2030 +iyr:2013 +cid:209 pid:989002507 +ecl:hzl eyr:2028 hcl:59c2ce + +ecl:gry iyr:2020 pid:895856989 hcl:#888785 +hgt:185cm byr:1964 eyr:2025 + +ecl:gry +hgt:183cm cid:97 hcl:#93be9b eyr:2021 +byr:1995 pid:668647173 + +hcl:#733820 eyr:2027 +byr:1977 pid:242773849 hgt:184cm iyr:2017 +cid:76 ecl:brn + +hgt:167 hcl:z byr:2012 eyr:2027 +pid:590438786 ecl:hzl +cid:101 iyr:1994 + +iyr:2015 +hcl:#888785 eyr:2024 +ecl:blu pid:279218875 cid:178 byr:1944 hgt:74in + +ecl:oth cid:334 pid:169599381 +hcl:#7d3b0c eyr:2028 hgt:172cm iyr:2010 + +eyr:2033 byr:1973 cid:333 iyr:2019 hcl:z ecl:utc +hgt:178cm pid:#e7abc0 + +eyr:2026 ecl:blu cid:264 byr:1998 hgt:177cm hcl:#602927 +pid:770052780 +iyr:2019 + +ecl:blu +iyr:2016 cid:232 eyr:2023 +pid:643597545 hgt:167cm byr:1986 hcl:#cfa07d + +pid:017818204 +iyr:2017 eyr:2028 hgt:168cm hcl:#602927 byr:1931 ecl:oth + +byr:1984 ecl:grn hgt:191cm hcl:#efcc98 pid:408382086 +iyr:2010 eyr:2025 + +hgt:158cm ecl:#25e195 byr:1929 pid:0367744553 eyr:1957 iyr:2017 +hcl:z + +hgt:168cm byr:1977 pid:006609828 +eyr:2024 +iyr:2015 +ecl:amb hcl:#b6652a + +pid:635167847 iyr:2010 hcl:#18171d hgt:163cm ecl:grn +eyr:2026 + +pid:423029177 byr:1995 +iyr:2014 ecl:hzl +hgt:185cm hcl:#c0946f eyr:2024 cid:230 + +pid:#3e3109 ecl:#a8a0aa iyr:2020 hcl:#733820 eyr:2021 byr:2010 hgt:179in + +iyr:2017 byr:1938 eyr:2022 hcl:#733820 ecl:amb hgt:157cm pid:162425330 + +hgt:160cm +cid:339 byr:1938 +hcl:#341e13 +eyr:2025 iyr:2015 +pid:562755634 + +cid:283 byr:1954 ecl:blu hcl:#ceb3a1 hgt:169cm iyr:2013 pid:616439723 + +pid:830940520 eyr:2022 +byr:1981 hgt:193cm hcl:#18171d iyr:2013 +cid:117 + +hcl:#866857 iyr:2019 ecl:hzl byr:1962 hgt:72in eyr:2020 + +byr:2015 pid:4530685490 ecl:#c7604e hcl:z +hgt:151 eyr:1972 iyr:1948 + +pid:0800674852 hcl:z +ecl:#2a275e hgt:61cm +iyr:1950 eyr:1938 + +iyr:2028 +eyr:1998 +pid:#c604ca +hgt:122 hcl:z +ecl:dne cid:160 byr:2018 + +hcl:z +cid:67 iyr:2029 +byr:1973 +ecl:amb eyr:2030 +hgt:66 + +ecl:hzl +pid:483557353 eyr:2021 +hgt:172cm iyr:2011 +hcl:#888785 +byr:1963 + +eyr:2030 ecl:brn hcl:#fffffd byr:1969 +hgt:161cm pid:632930895 +iyr:2017 + +iyr:2015 byr:2000 +pid:525557240 hgt:156cm eyr:2029 +ecl:brn hcl:#64bf85 + +byr:2007 +iyr:2012 eyr:2029 ecl:#7c1d6f pid:612320497 hgt:165in + +eyr:2026 ecl:grn cid:93 +hcl:#888785 hgt:134 pid:745552484 iyr:2019 byr:1999 + +byr:2001 pid:984678649 hgt:173cm ecl:oth iyr:2012 cid:345 hcl:#fffffd +eyr:2028 + +hcl:#341e13 pid:308853935 +byr:1972 hgt:160cm iyr:2012 eyr:2026 +ecl:amb cid:97 + +hcl:z hgt:151 +ecl:grn pid:132866718 +iyr:2021 byr:1932 +eyr:2024 + +eyr:2027 byr:1923 +ecl:oth hgt:174cm hcl:#733820 +pid:099185839 +iyr:2017 + +iyr:2017 ecl:gry byr:1941 +hgt:74in +pid:540930352 +hcl:#888785 eyr:2023 + +hgt:161cm cid:277 +hcl:#866857 +byr:1928 +eyr:2030 ecl:blu +iyr:2020 pid:358953579 + +hgt:155cm eyr:1930 +iyr:2015 ecl:utc +pid:163cm +byr:2008 hcl:#733820 cid:156 + +byr:1946 hgt:164cm +cid:234 iyr:2015 +pid:168804387 eyr:2025 +ecl:hzl hcl:#a97842 + +hgt:184cm +hcl:#7d3b0c byr:1996 cid:193 iyr:2011 ecl:amb eyr:2030 pid:940497049 + +hcl:#733820 iyr:2016 hgt:168 +eyr:2020 +cid:118 pid:9871436 ecl:gry + +pid:416683862 +hgt:162in +hcl:#602927 byr:2027 +eyr:2027 ecl:gmt + +ecl:#771e11 +iyr:2024 +cid:81 pid:741843390 +hcl:#866857 byr:2012 eyr:1964 +hgt:157in + +pid:720946735 +byr:2002 hcl:#18171d iyr:2026 hgt:70cm +ecl:brn eyr:2025 + +byr:1987 hcl:#623a2f +hgt:165cm pid:087526326 ecl:grn +iyr:2017 eyr:2021 + +byr:2014 hgt:193cm +iyr:2018 hcl:z +eyr:2027 pid:792398819 + +cid:233 +hcl:z ecl:#18002d hgt:174cm iyr:2027 eyr:1944 + +pid:142615460 ecl:blu eyr:2021 +hcl:#c88693 iyr:2017 cid:182 hgt:152cm byr:1966 + +hcl:z hgt:65cm +eyr:1944 ecl:dne byr:2029 pid:1066768764 +iyr:2015 +cid:149 + +hcl:z pid:25144284 hgt:152in +byr:2009 +cid:276 eyr:2037 +iyr:2010 ecl:gmt + +cid:88 hcl:ed6b8a ecl:xry hgt:178in pid:36832491 byr:2020 iyr:2012 eyr:1993 + +byr:1972 iyr:2009 pid:324213657 hgt:160 hcl:z cid:259 eyr:2021 ecl:grn + +hcl:#7d3b0c pid:164496070 eyr:2024 ecl:brn hgt:165cm + +pid:955035528 +byr:1967 eyr:2021 iyr:2015 hcl:#fffffd hgt:156cm +cid:237 +ecl:blu + +hcl:z +eyr:2038 iyr:1973 ecl:grt hgt:193in +pid:41385449 byr:2011 +cid:119 + +byr:1934 pid:713339056 hgt:157cm cid:250 iyr:2016 ecl:blu hcl:#866857 eyr:2026 + +hgt:186cm byr:2012 +eyr:2022 +iyr:2013 hcl:#733820 pid:226693031 ecl:hzl + +cid:118 pid:56750275 hcl:#cfa07d iyr:2028 eyr:2033 byr:1933 hgt:166cm + +byr:1973 +ecl:hzl hgt:167 hcl:z +iyr:2015 pid:70648423 + +byr:1929 +iyr:2012 pid:083005744 ecl:brn hgt:162cm +eyr:2024 +hcl:#cfa07d + +hcl:#623a2f hgt:174cm +eyr:2026 iyr:2020 +ecl:grn +byr:1981 + +cid:236 byr:1979 iyr:2013 ecl:amb eyr:2030 hcl:#6b5442 pid:768869830 + +hcl:#341e13 byr:1998 iyr:2020 +pid:857980686 +ecl:hzl hgt:175cm eyr:2028 + +hgt:169in +cid:305 eyr:2023 +ecl:gry pid:833031763 hcl:#ceb3a1 iyr:2015 byr:1974 + +pid:370671195 hgt:162cm +iyr:2018 ecl:oth byr:2001 hcl:#7d3b0c +eyr:2021 + +eyr:2026 +ecl:brn +cid:288 +hcl:#866857 iyr:2010 byr:1922 +pid:309109383 + +byr:1949 iyr:2015 pid:832774982 eyr:2029 +hgt:182cm +hcl:#b7144c ecl:amb + +eyr:2029 iyr:2020 +hcl:#c0946f +pid:953788134 +byr:1954 hgt:167cm + +pid:981508704 eyr:2023 iyr:2013 hgt:66in cid:68 hcl:#fffffd byr:1996 ecl:oth + +iyr:2011 byr:1932 cid:232 eyr:2030 +hcl:#c0946f +hgt:169cm pid:994763987 +ecl:hzl + +ecl:gmt eyr:2031 hcl:z +byr:2006 cid:86 hgt:59cm iyr:1998 pid:#349a7f + +hcl:#7d3b0c iyr:2013 pid:549500594 eyr:2024 byr:1971 ecl:oth +hgt:153cm + +iyr:2015 +byr:1940 pid:798926275 eyr:2027 +hcl:#cfa07d +hgt:158cm ecl:amb + +ecl:brn pid:077141823 +eyr:1945 iyr:2023 hcl:#602927 byr:1956 +hgt:152 + +iyr:2022 +byr:2007 +hcl:f87f81 pid:8404831740 eyr:2033 hgt:170cm +ecl:gmt + +iyr:1958 +ecl:dne hcl:840fbe +cid:55 eyr:2032 pid:19403738 hgt:170cm +byr:2029 + +ecl:brn pid:864743117 +eyr:2025 +hgt:188cm byr:1934 hcl:#623a2f +iyr:2011 + +iyr:2010 pid:877675472 hcl:#7d3b0c eyr:2027 byr:1954 hgt:182cm +ecl:oth + +iyr:2015 hcl:#a97842 pid:719192774 ecl:brn +byr:1993 +hgt:186cm + +eyr:2021 hgt:176cm pid:395161956 hcl:#602927 +ecl:grn iyr:2020 byr:1972 + +hcl:#fffffd hgt:167cm byr:1945 ecl:grn iyr:2017 eyr:2026 + +hcl:#6b5442 ecl:gry cid:208 hgt:155cm iyr:2010 byr:1933 pid:040244553 +eyr:2029 + +iyr:1960 cid:281 ecl:#8f7c45 hcl:z pid:11609858 eyr:1955 hgt:150in +byr:1985 + +hgt:150cm byr:2002 hcl:#fffffd +pid:337375886 iyr:2011 ecl:amb +eyr:2037 + +iyr:2014 ecl:oth hgt:155cm hcl:#866857 eyr:2025 pid:614859979 cid:338 +byr:1952 + +hcl:#623a2f hgt:156cm +iyr:2010 ecl:amb byr:1997 +cid:285 +pid:225568504 eyr:2020 + +iyr:2021 hcl:#ceb3a1 ecl:#d6db5a pid:185cm eyr:2020 cid:130 byr:2009 + +byr:1956 +cid:245 iyr:1954 ecl:dne hcl:#341e13 eyr:2040 hgt:71cm pid:838134394 + +eyr:2027 +byr:1994 ecl:amb iyr:2014 hgt:168cm +cid:278 +hcl:#602927 pid:758176176 + +hcl:#941519 ecl:gry eyr:2030 byr:1921 +pid:9477831046 +iyr:2018 hgt:189cm + +cid:339 hcl:#6b5442 +byr:1972 +ecl:oth +iyr:2014 hgt:183cm eyr:2020 + +cid:165 +eyr:1926 hcl:#6b5442 iyr:1956 hgt:65cm byr:2029 +pid:9240052093 ecl:xry + +ecl:grn +eyr:2027 +hcl:#fffffd byr:1975 iyr:2011 hgt:177cm pid:825090410 cid:121 + +byr:1934 +hcl:#733820 pid:133141914 eyr:2027 +hgt:66in +ecl:amb +iyr:2010 cid:262 + +iyr:1951 +ecl:#bb9733 hcl:89fcbf +byr:2018 pid:#13265f hgt:85 +eyr:1977 + +hcl:#324bed ecl:hzl hgt:155cm +iyr:2013 byr:1942 eyr:2021 +pid:758516520 + +ecl:gry +pid:#567782 iyr:2015 hcl:276a6d eyr:2028 +hgt:60cm +byr:2005 + +hcl:#18171d hgt:191cm eyr:2030 +ecl:grn pid:748729106 byr:1922 iyr:2017 + +byr:1984 hcl:#733820 eyr:2020 iyr:2013 hgt:64in pid:444446114 +ecl:hzl + +byr:2006 ecl:dne iyr:2029 hcl:z +eyr:2023 hgt:68cm +pid:131910019 + +iyr:1976 hcl:45191a eyr:2017 hgt:68cm +ecl:oth pid:153cm byr:1968 + +ecl:gry byr:1959 eyr:2023 pid:781945569 +iyr:2015 hcl:#ceb3a1 hgt:167cm + +hgt:151cm +byr:1959 hcl:#7d3b0c +cid:76 pid:069205247 ecl:gry eyr:2029 +iyr:2013 + +pid:055193304 +hcl:#fffffd +hgt:67in ecl:grn iyr:2011 + +eyr:2026 +byr:1975 +pid:681437369 +hgt:173cm hcl:#a97842 +iyr:2017 ecl:brn + +pid:298214774 hgt:158cm ecl:hzl +hcl:#866857 byr:1993 iyr:2013 +eyr:2028 + +hcl:#b6652a eyr:2023 pid:879418046 hgt:193cm +iyr:2014 +ecl:brn + +hgt:164cm +eyr:2028 hcl:fbd462 ecl:gry +iyr:2011 byr:1938 pid:029003659 + +byr:1999 hgt:171cm eyr:2024 cid:195 pid:041799326 ecl:gry iyr:2012 hcl:#a97842 + +byr:1963 pid:7471206322 iyr:2015 ecl:brn hcl:#623a2f hgt:72cm +cid:63 + +byr:1961 +ecl:hzl hcl:#efcc98 +pid:617022365 +eyr:2020 hgt:155cm iyr:2018 + +pid:385763432 byr:1977 cid:228 hcl:#733820 eyr:2020 +hgt:163cm ecl:grt +iyr:2015 + +byr:1979 hgt:186in eyr:2037 +ecl:dne +iyr:2025 hcl:d93e3e +pid:187cm + +iyr:2018 +cid:158 eyr:2020 +byr:1929 +pid:334374178 hgt:181cm hcl:z ecl:grn + +cid:135 ecl:oth hcl:#623a2f +iyr:2017 +eyr:2024 byr:1940 +hgt:66in + +cid:92 eyr:2026 hcl:#888785 pid:827526879 byr:1990 +hgt:186cm ecl:amb iyr:2014 + +pid:575519103 +iyr:2016 byr:1964 hcl:#7d3b0c cid:252 hgt:172cm + +hcl:#c0946f +ecl:grn hgt:75in +eyr:2021 pid:426544544 + +cid:171 byr:2004 +hcl:z +hgt:191cm eyr:2028 pid:#292b3b ecl:brn + +iyr:2016 ecl:hzl +byr:2007 +hgt:167cm hcl:#653b4c cid:124 eyr:2022 +pid:#27024c + +hgt:177cm byr:2025 +hcl:z cid:303 iyr:2016 eyr:2000 ecl:dne +pid:345555418 + +eyr:2040 +hcl:70fbf6 cid:318 +byr:2027 iyr:2022 +ecl:#3a037c pid:644624005 +hgt:62cm + +ecl:brn iyr:2016 hgt:150cm pid:680594195 cid:324 hcl:#602927 +byr:1944 + +hcl:z eyr:2022 byr:2020 cid:112 ecl:dne iyr:2029 +pid:#3a3560 hgt:153in + +cid:86 +ecl:gry byr:1972 hcl:#866857 pid:758976621 hgt:188cm + +pid:#850e8b eyr:2025 hgt:163in +ecl:#71e9e2 +hcl:#623a2f + +ecl:amb byr:1958 hgt:193cm +pid:343803154 iyr:2010 eyr:2029 cid:86 hcl:#602927 + +hgt:178cm byr:1987 ecl:blu hcl:#866857 iyr:2017 +pid:222443921 eyr:2027 + +byr:1964 iyr:2001 +hgt:180cm eyr:1945 +hcl:#fffffd pid:305189916 ecl:#ac3c49 +cid:142 + +byr:1979 hgt:166cm ecl:brn +cid:202 hcl:#5311ca eyr:2028 pid:874418818 iyr:2014 + +hgt:161cm byr:1987 ecl:grn hcl:#6b5442 pid:986096798 +cid:74 +eyr:2027 iyr:2019 + +byr:1964 iyr:1979 eyr:2022 ecl:#77ada3 +hcl:#733820 +cid:56 + +hgt:185cm iyr:2010 pid:214991920 eyr:2023 cid:236 hcl:#866857 ecl:brn byr:1965 + +pid:885169578 iyr:2010 byr:1948 ecl:hzl hcl:#18171d +eyr:2025 hgt:175cm + +ecl:oth pid:34655368 +hgt:186 +byr:1968 +hcl:2653c9 +eyr:2030 iyr:2024 + +eyr:2022 iyr:2015 ecl:gry hgt:65in byr:1994 +pid:766704860 + +hcl:#c0946f cid:104 iyr:2015 +pid:739274357 +ecl:#ef45c8 +byr:1960 hgt:61in eyr:2026 + +pid:849044092 eyr:2020 hgt:186cm iyr:2014 +byr:1991 hcl:#866857 + +hgt:159cm iyr:1950 +eyr:2021 pid:325442644 hcl:#888785 +ecl:gry byr:1986 + +eyr:2023 hgt:188cm +iyr:2014 +pid:945115479 byr:1979 ecl:blu hcl:#b6652a + +eyr:2024 +cid:274 pid:390115952 byr:1934 hgt:161cm iyr:2017 hcl:#b95b0d + +ecl:#a16ec8 pid:187cm hcl:z iyr:2029 hgt:170 +byr:2008 diff --git a/2020/04/passport.py b/2020/04/passport.py new file mode 100644 index 0000000..389cb56 --- /dev/null +++ b/2020/04/passport.py @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2020 Akumatic + +class Pass: + def __init__(self, input): + tmp = dict() + for data in input: + d = data.split(":") + tmp[d[0]] = d[1] + self.byr = int(tmp["byr"]) if "byr" in tmp else None + self.iyr = int(tmp["iyr"]) if "iyr" in tmp else None + self.eyr = int(tmp["eyr"]) if "eyr" in tmp else None + self.hgt = tmp["hgt"] if "hgt" in tmp else None + self.hcl = tmp["hcl"] if "hcl" in tmp else None + self.ecl = tmp["ecl"] if "ecl" in tmp else None + self.pid = tmp["pid"] if "pid" in tmp else None + self.cid = tmp["cid"] if "cid" in tmp else None + + def valid_fields(self) -> bool: + return all((self.byr, self.iyr, self.eyr, \ + self.hgt, self.hcl, self.ecl, self.pid)) + + def valid_data(self): + if not self.valid_fields(): + return False + return all(( + validate_byr(self.byr), + validate_iyr(self.iyr), + validate_eyr(self.eyr), + validate_hgt(self.hgt), + validate_hcl(self.hcl), + validate_ecl(self.ecl), + validate_pid(self.pid) + )) + +def validate_byr(byr: int) -> bool: + return 1920 <= byr <= 2002 + +def validate_iyr(iyr: int) -> bool: + return 2010 <= iyr <= 2020 + +def validate_eyr(eyr: int) -> bool: + return 2020 <= eyr <= 2030 + +def validate_hgt(hgt: str) -> bool: + if not(hgt[-2:] in ("cm","in") and hgt[:-2].isnumeric()): + return False + i = int(hgt[:-2]) + return 150 <= i <= 193 if hgt[-2:] == "cm" else 59 <= i <= 76 + +def validate_hcl(hcl: str) -> bool: + if not(len(hcl) == 7 and hcl[0] == "#"): + return False + return all(47 < ord(c) < 58 or 96 < ord(c) < 103 for c in hcl[1:]) + +def validate_ecl(ecl: str) -> bool: + return ecl in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth") + +def validate_pid(pid: str) -> bool: + return len(pid) == 9 and pid.isnumeric() \ No newline at end of file diff --git a/2020/04/solution.txt b/2020/04/solution.txt new file mode 100644 index 0000000..dec2c91 --- /dev/null +++ b/2020/04/solution.txt @@ -0,0 +1,2 @@ +Part 1: 200 +Part 2: 116 \ No newline at end of file diff --git a/2020/04/test_code.py b/2020/04/test_code.py new file mode 100644 index 0000000..2bbd02f --- /dev/null +++ b/2020/04/test_code.py @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2020 Akumatic + +from code import parse_data, part1 +import passport + +def test(): + input = ["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"] + passports = parse_data(input) + assert len(passports) == 4 + assert part1(passports) == 2 + + input = ["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"] + passports = parse_data(input) + assert all((not p.valid_data() for p in passports)) + + input = ["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"] + passports = parse_data(input) + assert all((p.valid_data() for p in passports)) + + assert passport.validate_byr(2002) == True + assert passport.validate_byr(2003) == False + assert passport.validate_hgt("60in") == True + assert passport.validate_hgt("190cm") == True + assert passport.validate_hgt("190in") == False + assert passport.validate_hgt("190") == False + assert passport.validate_hcl("#123abc") == True + assert passport.validate_hcl("#123abz") == False + assert passport.validate_hcl("123abc") == False + assert passport.validate_ecl("brn") == True + assert passport.validate_ecl("wat") == False + assert passport.validate_pid("000000001") == True + assert passport.validate_pid("0123456789") == False \ No newline at end of file diff --git a/2020/README.md b/2020/README.md index cc0109d..b80037f 100644 --- a/2020/README.md +++ b/2020/README.md @@ -16,7 +16,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day | Day | Part 1 | Part 2 ||Day | Part 1 | Part 2 | | --- | --- | --- |---| --- | --- | --- | | 01 | :white_check_mark: | :white_check_mark: || 02 | :white_check_mark: | :white_check_mark: | -| 03 | | || 04 | | | +| 03 | :white_check_mark: | :white_check_mark: || 04 | :white_check_mark: | :white_check_mark: | | 05 | | || 06 | | | | 07 | | || 08 | | | | 09 | | || 10 | | | diff --git a/README.md b/README.md index f8a3371..158a5dc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This repository contains my solutions for [Advent of Code](https://adventofcode. The folder for each day contains: - Task of the day - Input for the given task -- Code to solve the given task +- Code to solve the given task (main file is always "code.py") - My solutions to the given input Please note that all task descriptions on the AoC website could've been changed since they were added to this repository to clarify certain passages.