added day 1 code and updated organiser
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import re
|
||||
|
||||
with open('advent_of_code_2023\day_1\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
def grab_first_and_last_digit(line: str) -> (int):
|
||||
'''Returns the first and last digit of the given line
|
||||
input: line - string of characters
|
||||
output: a 2 digit integer
|
||||
|
||||
example:
|
||||
input: 'pqr3stu8vwx'
|
||||
output: 38
|
||||
'''
|
||||
first_digit = re.findall(r'\d', line)[0]
|
||||
last_digit = re.findall(r'\d', line)[-1]
|
||||
return int(first_digit + last_digit)
|
||||
|
||||
def day_1_solution(input:str) -> int:
|
||||
'''Returns the sum of the first and last digit of each line
|
||||
input: input - string of characters
|
||||
output: an integer
|
||||
|
||||
example:
|
||||
input: '1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet'
|
||||
output: 48
|
||||
'''
|
||||
list = []
|
||||
for line in input.split('\n'):
|
||||
list.append(grab_first_and_last_digit(line))
|
||||
|
||||
return sum(list)
|
||||
|
||||
|
||||
test_input = '1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet'
|
||||
day_1_test = day_1_solution(test_input)
|
||||
print(day_1_test)
|
||||
assert day_1_test == 142
|
||||
|
||||
day_1_answer = day_1_solution(input)
|
||||
print(day_1_answer)
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import re
|
||||
|
||||
with open('advent_of_code_2023\day_1\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
def text_to_num(line: str) -> str:
|
||||
'''Returns the line with the text numbers replaced with digits
|
||||
input: line - string of characters
|
||||
output: a string
|
||||
|
||||
example:
|
||||
input: 'eightwothree'
|
||||
output: '8wo3'
|
||||
'''
|
||||
text_to_num = {
|
||||
"zero": "0",
|
||||
"one": "one1one",
|
||||
"two": "two2two",
|
||||
"three": "three3three",
|
||||
"four": "four4four",
|
||||
"five": "five5five",
|
||||
"six": "six6six",
|
||||
"seven": "seven7seven",
|
||||
"eight": "eight8eight",
|
||||
"nine": "nine9nine"
|
||||
}
|
||||
#replace a text number with a digit surrounded by the same text numberm to resolve overlaps
|
||||
for key, value in text_to_num.items():
|
||||
if key in line:
|
||||
line = line.replace(key, value)
|
||||
return line
|
||||
|
||||
def grab_first_and_last_digit(line: str) -> (int):
|
||||
'''Returns the first and last digit of the given line
|
||||
input: line - string of characters
|
||||
output: a 2 digit integer
|
||||
|
||||
example:
|
||||
input: 'pqr3stu8vwx'
|
||||
output: 38
|
||||
'''
|
||||
first_digit = re.findall(r'\d', line)[0]
|
||||
last_digit = re.findall(r'\d', line)[-1]
|
||||
return int(first_digit + last_digit)
|
||||
|
||||
def day_1_solution(input:str) -> int:
|
||||
'''Returns the sum of the first and last digit of each line
|
||||
input: input - string of characters
|
||||
output: an integer
|
||||
|
||||
example:
|
||||
input: '1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet'
|
||||
output: 48
|
||||
'''
|
||||
list = []
|
||||
for line in input.split('\n'):
|
||||
line = text_to_num(line)
|
||||
list.append(grab_first_and_last_digit(line))
|
||||
|
||||
return sum(list)
|
||||
|
||||
|
||||
test_input = 'two1nine\neightwothree\nabcone2threexyz\nxtwone3four\n4nineeightseven2\nzoneight234\n7pqrstsixteen'
|
||||
day_1_test = day_1_solution(test_input)
|
||||
print(day_1_test)
|
||||
assert day_1_test == 281
|
||||
|
||||
day_1_answer = day_1_solution(input)
|
||||
print(day_1_answer)
|
||||
|
||||
+20
-1
@@ -22,4 +22,23 @@ 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?
|
||||
Consider your entire calibration document. What is the sum of all of the calibration values?
|
||||
|
||||
|
||||
--- Part Two ---
|
||||
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?
|
||||
@@ -1,6 +0,0 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_1\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_10\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_10\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_11\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_11\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_12\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_12\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_13\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_13\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_14\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_14\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_15\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_15\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_16\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_16\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_17\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_17\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_18\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_18\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_19\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_19\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_2\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_2\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_20\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_20\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_21\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_21\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_22\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_22\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_23\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_23\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_24\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_24\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_25\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_25\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_3\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_3\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_4\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_4\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_5\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_5\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_6\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_6\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_7\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_7\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_8\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_8\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_9\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
@@ -0,0 +1,6 @@
|
||||
import os
|
||||
|
||||
with open('advent_of_code_2023\day_9\input.txt', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
+12
-3
@@ -4,7 +4,7 @@ import os
|
||||
base_dir = 'advent_of_code_2023'
|
||||
|
||||
# Loop over the range 1 to 25 (inclusive)
|
||||
for i in range(1, 26):
|
||||
for i in range(2, 26):
|
||||
# Construct the folder name
|
||||
folder_name = f"day_{i}"
|
||||
|
||||
@@ -17,7 +17,8 @@ for i in range(1, 26):
|
||||
# Construct the full path to the placeholder file
|
||||
input_file_path = os.path.join(folder_path, "input.txt")
|
||||
puzzle_file_path = os.path.join(folder_path, "puzzle_text.md")
|
||||
solution_file_path = os.path.join(folder_path, "solution.py")
|
||||
solution_file_path_1 = os.path.join(folder_path, "part 1 solution.py")
|
||||
solution_file_path_2 = os.path.join(folder_path, "part 2 solution.py")
|
||||
|
||||
# Create the placeholder file
|
||||
with open(input_file_path, "w") as file:
|
||||
@@ -28,7 +29,15 @@ for i in range(1, 26):
|
||||
file.write( f"# Day {i} Puzzle Text.")
|
||||
|
||||
# Create the solution file
|
||||
with open(solution_file_path, "w") as file:
|
||||
with open(solution_file_path_1, "w") as file:
|
||||
file.write(f"""import os
|
||||
|
||||
with open('{input_file_path}', 'r') as file:
|
||||
input = file.read()
|
||||
|
||||
print(input)
|
||||
""")
|
||||
with open(solution_file_path_2, "w") as file:
|
||||
file.write(f"""import os
|
||||
|
||||
with open('{input_file_path}', 'r') as file:
|
||||
|
||||
Reference in New Issue
Block a user