Rename for better sorting on github
This commit is contained in:
4
day01/eg1.txt
Normal file
4
day01/eg1.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
day01/eg2.txt
Normal file
7
day01/eg2.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
1000
day01/input.txt
Normal file
1000
day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
34
day01/solution.py
Normal file
34
day01/solution.py
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3.12
|
||||
from typing import Iterable
|
||||
|
||||
def first_digit(s: str):
|
||||
for idx, c in enumerate(s):
|
||||
if "0" <= c <= "9":
|
||||
return idx, int(c)
|
||||
|
||||
def first_word(s: str, words: list[str]):
|
||||
indexes = [(s.find(w), w) for w in words]
|
||||
try:
|
||||
midx = min([idx for idx in indexes if idx[0] != -1])
|
||||
return midx[0], words.index(midx[1]) + 1
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
def not_null[T](i: Iterable[T | None]):
|
||||
return [a for a in i if a is not None]
|
||||
|
||||
digits = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
||||
rdigits = [d[::-1] for d in digits]
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("input.txt") as f:
|
||||
lines = [l for l in f.read().split("\n") if l != ""]
|
||||
print("Part 1:", sum([
|
||||
first_digit(line)[1] * 10 + first_digit(line[::-1])[1]
|
||||
for line in lines
|
||||
]))
|
||||
print("Part 2:", sum([
|
||||
min(not_null([first_digit(line), first_word(line, digits)]))[1] * 10 +
|
||||
min(not_null([first_digit(rline := line[::-1]), first_word(rline, rdigits)]))[1]
|
||||
for line in lines
|
||||
]))
|
Reference in New Issue
Block a user