2023 Day 01

This commit is contained in:
Akumatic
2023-12-07 00:35:21 +01:00
parent 5377d8d653
commit 2bd5bf0a19
7 changed files with 1144 additions and 0 deletions

44
2023/01/README.md Normal file
View File

@ -0,0 +1,44 @@
# 2023 Day 01: Trebuchet?!
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2023/day/1)
## Part 1
You try to ask why they can't just use a [weather machine](https://adventofcode.com/2015/day/1) ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a [trebuchet](https://en.wikipedia.org/wiki/Trebuchet) ("please hold still, we need to strap you in").
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been **amended** by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific **calibration value** that the Elves now need to recover. On each line, the calibration value can be found by combining the **first digit** and the **last digit** (in that order) to form a single **two-digit number**.
For example:
```
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
```
In this example, the calibration values of these four lines are `12`, `38`, `15`, and `77`. Adding these together produces **`142`**.
Consider your entire calibration document. **What is the sum of all of the calibration values?**
## Part 2
Your calculation isn't quite right. It looks like some of the digits are actually **spelled out with letters**: `one`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight`, and `nine` **also** count as valid "digits".
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
```
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
```
In this example, the calibration values are `29`, `83`, `13`, `24`, `42`, `14`, and `76`. Adding these together produces **`281`**.
**What is the sum of all of the calibration values?**

32
2023/01/code.py Normal file
View File

@ -0,0 +1,32 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2023 Akumatic
#
# https://adventofcode.com/2023/day/1
import re
def read_file(filename: str = "input.txt") -> list:
with open(f"{__file__.rstrip('code.py')}{filename}", "r") as f:
return [line.strip() for line in f.readlines()]
def get_simple_calibration_value(line: str) -> int:
nums = re.findall(r'\d', line)
return 10 * int(nums[0]) + int(nums[-1])
def get_real_calibration_value(line: str) -> int:
mapping = {"one": 1, "two": 2, "three": 3, "four": 4,
"five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9}
nums = re.findall(r'(?=(one|two|three|four|five|six|seven|eight|nine|\d))', line)
return 10 * (mapping.get(nums[0]) or int(nums[0])) + (mapping.get(nums[-1]) or int(nums[-1]))
def part1(vals: list) -> int:
return sum(get_simple_calibration_value(val) for val in vals)
def part2(vals: list) -> int:
return sum(get_real_calibration_value(val) for val in vals)
if __name__ == "__main__":
vals = read_file()
print(f"Part 1: {part1(vals)}")
print(f"Part 2: {part2(vals)}")

1000
2023/01/input.txt Normal file

File diff suppressed because it is too large Load Diff

2
2023/01/solution.txt Normal file
View File

@ -0,0 +1,2 @@
Part 1: 53974
Part 2: 52840

14
2023/01/test_code.py Normal file
View File

@ -0,0 +1,14 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2023 Akumatic
from code import read_file, part1, part2
def test():
vals = read_file("test_input.txt")
assert part1(vals[:4]) == 142
print("Passed Part 1")
assert part2(vals[4:]) == 281
print("Passed Part 2")
if __name__ == "__main__":
test()

11
2023/01/test_input.txt Normal file
View File

@ -0,0 +1,11 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen