2023: Day 1.

This commit is contained in:
Mikael CAPELLE 2023-12-01 10:41:13 +01:00
parent 10f67e6bfd
commit e991cd8b04
5 changed files with 1064 additions and 2 deletions

45
2023/day1.py Normal file
View File

@ -0,0 +1,45 @@
import string
import sys
lines = sys.stdin.read().split("\n")
lookups_1 = {d: int(d) for d in string.digits}
lookups_2 = lookups_1 | {
d: i + 1
for i, d in enumerate(
(
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
)
)
}
def find_values(lookups: dict[str, int]) -> list[int]:
values: list[int] = []
for line in filter(bool, lines):
first_digit = min(
lookups,
key=lambda lookup: index
if (index := line.find(lookup)) >= 0
else len(line),
)
last_digit = max(
lookups,
key=lambda lookup: index if (index := line.rfind(lookup)) >= 0 else 0,
)
values.append(10 * lookups[first_digit] + lookups[last_digit])
return values
print(f"answer 1 is {sum(find_values(lookups_1))}")
print(f"answer 2 is {sum(find_values(lookups_2))}")

1000
2023/inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

7
2023/tests/day1.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

4
2023/tests/day1_p1.txt Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

10
run.ps1
View File

@ -1,5 +1,11 @@
param([switch]$Test, $day)
param(
[switch]$Test,
[PSDefaultValue()]
[Parameter(Mandatory = $false)]
$Year = 2023,
[Parameter(Mandatory = $true, Position = 0)]
$Day)
$folder = $Test ? "tests" : "inputs"
Get-Content ".\2022\$folder\day$day.txt" | python ".\2022\day$day.py"
Get-Content ".\$Year\$folder\day$Day.txt" | python ".\$Year\day$Day.py"