day 1
This commit is contained in:
commit
df5c6890e4
48
day1/day1.py
Normal file
48
day1/day1.py
Normal file
@ -0,0 +1,48 @@
|
||||
def part1(lines):
|
||||
res = []
|
||||
|
||||
for line in lines:
|
||||
digits = [c for c in line if c.isnumeric()]
|
||||
res.append(int(digits[0] + digits[-1]))
|
||||
|
||||
print(f"Part 1: {sum(res)}")
|
||||
|
||||
|
||||
spelled_digits = { "one": "o1e", "two": "t2o", "three": "t3e", "four": "f4r", "five": "f5e", "six": "s6x", "seven": "s7n", "eight": "e8t", "nine": "n9e" }
|
||||
|
||||
|
||||
def substitute_digits(s, trans):
|
||||
res = s
|
||||
for word, digit in trans.items():
|
||||
res = res.replace(word, digit)
|
||||
return res
|
||||
|
||||
|
||||
def part2(lines):
|
||||
res = []
|
||||
|
||||
for line in lines:
|
||||
line = line.rstrip()
|
||||
res_line = []
|
||||
|
||||
nline = ""
|
||||
for c in line:
|
||||
nline += c
|
||||
nline = substitute_digits(nline, spelled_digits)
|
||||
|
||||
digits = [c for c in nline if c.isnumeric()]
|
||||
r = int(digits[0] + digits[-1])
|
||||
print(f"{line} => {nline}, {r}")
|
||||
res.append(r)
|
||||
|
||||
print(f"Part 2: {sum(res)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
infile = sys.argv[1]
|
||||
|
||||
with open(infile) as f:
|
||||
lines = f.readlines()
|
||||
part1(lines)
|
||||
part2(lines)
|
1000
day1/input.txt
Normal file
1000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user