Skip to content

Commit 3afc4a7

Browse files
committed
Various improvements
1 parent 23f1159 commit 3afc4a7

File tree

7 files changed

+50
-75
lines changed

7 files changed

+50
-75
lines changed

2020/02-Password Philosophy.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,20 @@ def words(s: str):
3939
"expected": ["2", "1"],
4040
}
4141

42-
test = "WD"
42+
test = "real"
4343
input_file = os.path.join(
4444
os.path.dirname(__file__),
4545
"Inputs",
46-
os.path.basename(__file__).replace(".py", ".WD.txt"),
46+
os.path.basename(__file__).replace(".py", ".txt"),
4747
)
4848
test_data[test] = {
4949
"input": open(input_file, "r+").read(),
50-
"expected": ["Unknown", "Unknown"],
51-
}
52-
53-
test = "Twitter"
54-
input_file = os.path.join(
55-
os.path.dirname(__file__),
56-
"Inputs",
57-
os.path.basename(__file__).replace(".py", ".Twitter.txt"),
58-
)
59-
test_data[test] = {
60-
"input": open(input_file, "r+").read(),
61-
"expected": ["447", "Unknown"],
50+
"expected": ["447", "249"],
6251
}
6352

6453
# -------------------------------- Control program execution ------------------------- #
6554

66-
case_to_test = "Twitter"
55+
case_to_test = "real"
6756
part_to_test = 2
6857

6958
# -------------------------------- Initialize some variables ------------------------- #

2020/04-Passport Processing.py

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -106,68 +106,52 @@ def words(s: str):
106106

107107
# -------------------------------- Actual code execution ----------------------------- #
108108

109-
required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
109+
110+
class Passport:
111+
required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
112+
113+
validations = {
114+
"byr": lambda year: year in range(1920, 2002 + 1),
115+
"iyr": lambda year: year in range(2010, 2020 + 1),
116+
"eyr": lambda year: year in range(2020, 2030 + 1),
117+
"hgt": lambda data: (
118+
data[-2:] == "cm" and int(data[:-2]) in range(150, 193 + 1)
119+
)
120+
or (data[-2:] == "in" and int(data[:-2]) in range(59, 76 + 1)),
121+
"hcl": lambda data: re.match("^#[0-9a-f]{6}$", data),
122+
"ecl": lambda data: data in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"],
123+
"pid": lambda data: re.match("^[0-9]{9}$", data),
124+
}
125+
126+
def __init__(self, data):
127+
self.fields = defaultdict(str)
128+
for element in data.split():
129+
if element[:3] in ("byr", "iyr", "eyr"):
130+
try:
131+
self.fields[element[:3]] = int(element[4:])
132+
except:
133+
self.fields[element[:3]] = element[4:]
134+
else:
135+
self.fields[element[:3]] = element[4:]
136+
137+
def has_required_data(self):
138+
return all([x in self.fields for x in self.required_fields])
139+
140+
def is_valid(self):
141+
return all([self.validations[x](self.fields[x]) for x in self.required_fields])
142+
110143

111144
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
145+
for string in puzzle_input.split("\n\n"):
146+
passports.append(Passport(string))
120147

121148
valid_passports = 0
122149

123150
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-
151+
valid_passports = sum([1 for x in passports if x.has_required_data()])
128152

129153
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
154+
valid_passports = sum([1 for x in passports if x.is_valid()])
171155

172156
puzzle_actual_result = valid_passports
173157

2020/07-Handy Haversacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def words(s: str):
145145
gold_contains["total"] += gold_contains[combination["out"]]
146146
del gold_contains[combination["out"]]
147147

148-
print(sum(gold_contains.values()), gold_contains)
148+
# print(sum(gold_contains.values()), gold_contains)
149149

150150
puzzle_actual_result = gold_contains["total"]
151151

2020/08-Handheld Halting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def run(self):
9292

9393
def run_once(self):
9494
instr = self.instructions[self.current_line]
95-
print("Before", self.current_line, self.accumulator, instr)
95+
# print("Before", self.current_line, self.accumulator, instr)
9696
self.operations[instr[0]](instr)
9797

9898
def nop(self, instr):

2020/09-Encoding Error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def words(s: str):
109109
if number_sum < invalid_number:
110110
for b in range(1, len(numbers) - a):
111111
number_sum += numbers[a + b]
112-
print(a, b, number_sum, invalid_number)
112+
# print(a, b, number_sum, invalid_number)
113113
if number_sum == invalid_number:
114114
puzzle_actual_result = min(numbers[a : a + b + 1]) + max(
115115
numbers[a : a + b + 1]

2020/10-Adapter Array.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def words(s: str):
9393
)
9494
test_data[test] = {
9595
"input": open(input_file, "r+").read(),
96-
"expected": ["Unknown", "Unknown"],
96+
"expected": ["2240", "99214346656768"],
9797
}
9898

9999

@@ -142,7 +142,7 @@ def words(s: str):
142142
for joltage in joltages:
143143
edges[joltage] = [x for x in joltages if x < joltage and x >= joltage - 3]
144144

145-
print(edges)
145+
# print(edges)
146146

147147
@lru_cache(maxsize=len(joltages))
148148
def count_paths(position):
@@ -162,3 +162,5 @@ def count_paths(position):
162162
print("Expected result : " + str(puzzle_expected_result))
163163
print("Actual result : " + str(puzzle_actual_result))
164164
# Date created: 2020-12-10 06:00:02.437611
165+
# Part 1: 2020-12-10 06:04:42
166+
# Part 2: 2020-12-10 06:14:12

2020/grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def text_to_dots(self, text, ignore_terrain=""):
8686
The dots will have x - y * 1j as coordinates
8787
8888
:param string text: The text to convert
89-
:param sequence ignore_terrain: The grid to convert
89+
:param sequence ignore_terrain: Types of terrain to ignore (useful for walls)
9090
"""
9191
self.dots = {}
9292

0 commit comments

Comments
 (0)