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

41
2023/README.md Normal file
View File

@ -0,0 +1,41 @@
# Advent of Code - 2023
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2023)
## Intro
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
You've been doing this long enough to know that to restore snow operations, you need to check all **fifty stars** by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants **one star**. Good luck!
## Overview
| Day | Part 1 | Part 2 | My Solution | Official Website |
| --- | --- | --- |---| --- |
| 01 | :white_check_mark: | :white_check_mark: | [Solution](01/code.py) | [Day 01](https://adventofcode.com/2023/day/1) |
| 02 | | | | [Day 02](https://adventofcode.com/2023/day/2) |
| 03 | | | | [Day 03](https://adventofcode.com/2023/day/3) |
| 04 | | | | [Day 04](https://adventofcode.com/2023/day/4) |
| 05 | | | | [Day 05](https://adventofcode.com/2023/day/5) |
| 06 | | | | [Day 06](https://adventofcode.com/2023/day/6) |
| 07 | | | | [Day 07](https://adventofcode.com/2023/day/7) |
| 08 | | | | [Day 08](https://adventofcode.com/2023/day/8) |
| 09 | | | | [Day 09](https://adventofcode.com/2023/day/9) |
| 10 | | | | [Day 10](https://adventofcode.com/2023/day/10) |
| 11 | | | | [Day 11](https://adventofcode.com/2023/day/11) |
| 12 | | | | [Day 12](https://adventofcode.com/2023/day/12) |
| 13 | | | | [Day 13](https://adventofcode.com/2023/day/13) |
| 14 | | | | [Day 14](https://adventofcode.com/2023/day/14) |
| 15 | | | | [Day 15](https://adventofcode.com/2023/day/15) |
| 16 | | | | [Day 16](https://adventofcode.com/2023/day/16) |
| 17 | | | | [Day 17](https://adventofcode.com/2023/day/17) |
| 18 | | | | [Day 18](https://adventofcode.com/2023/day/18) |
| 19 | | | | [Day 19](https://adventofcode.com/2023/day/19) |
| 20 | | | | [Day 20](https://adventofcode.com/2023/day/20) |
| 21 | | | | [Day 21](https://adventofcode.com/2023/day/21) |
| 22 | | | | [Day 22](https://adventofcode.com/2023/day/22) |
| 23 | | | | [Day 23](https://adventofcode.com/2023/day/23) |
| 24 | | | | [Day 24](https://adventofcode.com/2023/day/24) |
| 25 | | | | [Day 25](https://adventofcode.com/2023/day/25) |