-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
1,068 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# YT-Scripts - Advent of Code 2023 | ||
|
||
## Day 1 - Trebuchet!? | ||
|
||
Hey there! Today, we're diving into the first challenge of Advent of Code 2023. Let's get started! | ||
|
||
In part one, the Elves discover that their calibration document has been enhanced by an enthusiastic young Elf. The calibration values are now hidden within lines of text. We need to recover these values by combining the first and last digits in each line. | ||
|
||
Let's take a look at my Python solution: | ||
|
||
This code reads the document, extracts digits from each line, combines the first and last digits, and calculates the sum of all calibration values. The result? A total calibration sum. | ||
|
||
|
||
In Part 2, the challenge becomes a bit trickier. Some digits are now spelled out with letters. Our task is to find the real first and last digits on each line. Let's explore the solution: | ||
|
||
This code introduces a dictionary mapping word representations to their numerical values. The calc function handles the conversion of digits or words to numerical values. We then iterate through each line, calculate the calibration values, and find the total sum. | ||
|
||
And there you have it! We've successfully solved the first challenge. If you enjoyed this video, don't forget to like, share, and subscribe for more content. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Day 1 - Trebuchet!? | ||
|
||
#=================PART 1=================# | ||
document = open("materials/input1.txt").read().splitlines() | ||
|
||
total_sum = 0 | ||
|
||
for line in document: | ||
digits = [ ] | ||
|
||
for char in line: | ||
if char.isdigit(): | ||
digits.append(char) | ||
|
||
value = int(digits[0] + digits[-1]) | ||
total_sum += value | ||
|
||
print(f"Calibration value for line '{line}': {value}") | ||
|
||
print("The solution is:", total_sum) | ||
|
||
|
||
#=================PART 2=================# | ||
document = open("materials/input1.txt") | ||
|
||
str_to_num = { | ||
"one": "1", "two": "2", "three": "3", "four": "4", "five": "5", | ||
"six": "6", "seven": "7", "eight": "8", "nine": "9", | ||
} | ||
|
||
def calc(line: str): | ||
nums = [] | ||
|
||
for i in range(len(line)): | ||
if line[i].isdigit(): | ||
nums.append(line[i]) | ||
else: | ||
for num_str in str_to_num: | ||
if line[i:].startswith(num_str): | ||
nums.append(str_to_num[num_str]) | ||
return int(nums[0] + nums[-1]) | ||
|
||
solution = 0 | ||
|
||
for row in document: | ||
solution += calc(row) | ||
|
||
print(f"The solution is {solution}") |
Oops, something went wrong.