feat: day 1-a and b WIP
This commit is contained in:
1 parent
d8180ea8e2
commit
bf4ebda10b
7 files changed
+1081
-6
No files matched your search
+55
-1
@@ -1,2 +1,56 @@
|
||||
def run():
|
||||
print("Test")
|
||||
print(f"trebuchet total: {trebuchet(r'./src/assets/day_1.txt')}")
|
||||
print(f"spelled trebuchet total: {spelled_trebuchet(r'./src/assets/day_1.txt')}")
|
||||
|
||||
|
||||
def trebuchet(path):
|
||||
total = 0
|
||||
with open(path) as f:
|
||||
lines = f.readlines()
|
||||
total = count_calibration_lines(lines)
|
||||
return total
|
||||
|
||||
def spelled_trebuchet(path):
|
||||
dictionary = {"one": "1", "two": "2", "three": "3", "four": "4", "five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9", "ten": "10"}
|
||||
|
||||
total = 0
|
||||
with open(path) as f:
|
||||
lines = f.readlines()
|
||||
|
||||
for line in lines:
|
||||
new_line = line
|
||||
# FIXME: wrong
|
||||
for key,value in dictionary.items():
|
||||
new_line = new_line.replace(key, value)
|
||||
print(new_line)
|
||||
total += count_calibration(new_line)
|
||||
|
||||
return total
|
||||
|
||||
|
||||
def count_calibration_lines(lines):
|
||||
total = 0
|
||||
for line in lines:
|
||||
total += count_calibration(line)
|
||||
return total
|
||||
|
||||
|
||||
def count_calibration(line):
|
||||
init = None
|
||||
end = None
|
||||
for c in line:
|
||||
try:
|
||||
next = int(c)
|
||||
if init is None:
|
||||
init = next
|
||||
else:
|
||||
end = next
|
||||
except:
|
||||
pass
|
||||
|
||||
if end is None:
|
||||
end = init
|
||||
if init is not None and end is not None:
|
||||
line_value = init * 10 + end
|
||||
return line_value
|
||||
return 0
|
||||
Reference in new issue
Block a user