Compare commits
4 commits
53c344b223
...
7ef6dd8af9
| Author | SHA1 | Date | |
|---|---|---|---|
| 7ef6dd8af9 | |||
| 0be78bd0f5 | |||
| 915677df6a | |||
| af7182b161 |
4 changed files with 2060 additions and 0 deletions
20
2023/d01/ex1/ex1.py
Executable file
20
2023/d01/ex1/ex1.py
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def value(line: str) -> int:
|
||||
digits = [int(c) for c in line if c.isdigit()]
|
||||
return digits[0] * 10 + digits[-1]
|
||||
|
||||
return sum(value(line) for line in input)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
1000
2023/d01/ex1/input
Normal file
1000
2023/d01/ex1/input
Normal file
File diff suppressed because it is too large
Load diff
40
2023/d01/ex2/ex2.py
Executable file
40
2023/d01/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def extract_digits(line: str) -> list[int]:
|
||||
# Just do a search and replace to simplify our lives
|
||||
digits = {
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
"four": 4,
|
||||
"five": 5,
|
||||
"six": 6,
|
||||
"seven": 7,
|
||||
"eight": 8,
|
||||
"nine": 9,
|
||||
}
|
||||
# The solution expects "sevenine" to translate to 79, so keep first/last character
|
||||
# as a work-around.
|
||||
for word, value in digits.items():
|
||||
line = line.replace(word, word[0] + str(value) + word[-1])
|
||||
return [int(c) for c in line if c.isdigit()]
|
||||
|
||||
def value(line: str) -> int:
|
||||
digits = extract_digits(line)
|
||||
return digits[0] * 10 + digits[-1]
|
||||
|
||||
return sum(value(line) for line in input)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
1000
2023/d01/ex2/input
Normal file
1000
2023/d01/ex2/input
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue