Skip to content

Commit ca6aacc

Browse files
committed
New version for 2020-04
1 parent bb18e8e commit ca6aacc

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

2020/04-Passport Processing.v1.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
38+
byr:1937 iyr:2017 cid:147 hgt:183cm
39+
40+
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
41+
hcl:#cfa07d byr:1929
42+
43+
hcl:#ae17e1 iyr:2013
44+
eyr:2024
45+
ecl:brn pid:760753108 byr:1931
46+
hgt:179cm
47+
48+
hcl:#cfa07d eyr:2025 pid:166559648
49+
iyr:2011 ecl:brn hgt:59in""",
50+
"expected": ["2", "Unknown"],
51+
}
52+
test = 2
53+
test_data[test] = {
54+
"input": """eyr:1972 cid:100
55+
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
56+
57+
iyr:2019
58+
hcl:#602927 eyr:1967 hgt:170cm
59+
ecl:grn pid:012533040 byr:1946
60+
61+
hcl:dab227 iyr:2012
62+
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277
63+
64+
hgt:59cm ecl:zzz
65+
eyr:2038 hcl:74454a iyr:2023
66+
pid:3556412378 byr:2007
67+
68+
pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
69+
hcl:#623a2f
70+
71+
eyr:2029 ecl:blu cid:129 byr:1989
72+
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm
73+
74+
hcl:#888785
75+
hgt:164cm byr:2001 iyr:2015 cid:88
76+
pid:545766238 ecl:hzl
77+
eyr:2022
78+
79+
iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719""",
80+
"expected": ["Unknown", "4"],
81+
}
82+
83+
test = "real"
84+
input_file = os.path.join(
85+
os.path.dirname(__file__),
86+
"Inputs",
87+
os.path.basename(__file__).replace(".py", ".txt"),
88+
)
89+
test_data[test] = {
90+
"input": open(input_file, "r+").read(),
91+
"expected": ["235", "194"],
92+
}
93+
94+
95+
# -------------------------------- Control program execution ------------------------- #
96+
97+
case_to_test = "real"
98+
part_to_test = 2
99+
100+
# -------------------------------- Initialize some variables ------------------------- #
101+
102+
puzzle_input = test_data[case_to_test]["input"]
103+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
104+
puzzle_actual_result = "Unknown"
105+
106+
107+
# -------------------------------- Actual code execution ----------------------------- #
108+
109+
required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
110+
111+
passports = []
112+
i = 0
113+
for string in puzzle_input.split("\n"):
114+
if len(passports) >= i:
115+
passports.append("")
116+
if string == "":
117+
i = i + 1
118+
else:
119+
passports[i] = passports[i] + " " + string
120+
121+
valid_passports = 0
122+
123+
if part_to_test == 1:
124+
for passport in passports:
125+
if all([x + ":" in passport for x in required_fields]):
126+
valid_passports = valid_passports + 1
127+
128+
129+
else:
130+
for passport in passports:
131+
if all([x + ":" in passport for x in required_fields]):
132+
fields = passport.split(" ")
133+
score = 0
134+
for field in fields:
135+
data = field.split(":")
136+
if data[0] == "byr":
137+
year = int(data[1])
138+
if year >= 1920 and year <= 2002:
139+
score = score + 1
140+
elif data[0] == "iyr":
141+
year = int(data[1])
142+
if year >= 2010 and year <= 2020:
143+
score = score + 1
144+
elif data[0] == "eyr":
145+
year = int(data[1])
146+
if year >= 2020 and year <= 2030:
147+
score = score + 1
148+
elif data[0] == "hgt":
149+
size = ints(data[1])[0]
150+
if data[1][-2:] == "cm":
151+
if size >= 150 and size <= 193:
152+
score = score + 1
153+
elif data[1][-2:] == "in":
154+
if size >= 59 and size <= 76:
155+
score = score + 1
156+
elif data[0] == "hcl":
157+
if re.match("#[0-9a-f]{6}", data[1]) and len(data[1]) == 7:
158+
score = score + 1
159+
print(data[0], passport)
160+
elif data[0] == "ecl":
161+
if data[1] in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
162+
score = score + 1
163+
print(data[0], passport)
164+
elif data[0] == "pid":
165+
if re.match("[0-9]{9}", data[1]) and len(data[1]) == 9:
166+
score = score + 1
167+
print(data[0], passport)
168+
print(passport, score)
169+
if score == 7:
170+
valid_passports = valid_passports + 1
171+
172+
puzzle_actual_result = valid_passports
173+
174+
# -------------------------------- Outputs / results --------------------------------- #
175+
176+
print("Case :", case_to_test, "- Part", part_to_test)
177+
print("Expected result : " + str(puzzle_expected_result))
178+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)