Skip to content

Commit

Permalink
added aoc folder
Browse files Browse the repository at this point in the history
  • Loading branch information
achudnova authored Dec 31, 2023
1 parent 103bbc1 commit 5ba360c
Show file tree
Hide file tree
Showing 3 changed files with 1,068 additions and 0 deletions.
20 changes: 20 additions & 0 deletions advent-of-code/SCRIPTS.md
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.


48 changes: 48 additions & 0 deletions advent-of-code/day01/day01.py
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}")
Loading

0 comments on commit 5ba360c

Please sign in to comment.