diff --git a/day_1/part 1 solution.py b/day_1/part 1 solution.py new file mode 100644 index 0000000..8e41f15 --- /dev/null +++ b/day_1/part 1 solution.py @@ -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) + diff --git a/day_1/part 2 solution.py b/day_1/part 2 solution.py new file mode 100644 index 0000000..9276340 --- /dev/null +++ b/day_1/part 2 solution.py @@ -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) + diff --git a/day_1/puzzle_text.md b/day_1/puzzle_text.md index 9150944..102158b 100644 --- a/day_1/puzzle_text.md +++ b/day_1/puzzle_text.md @@ -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? \ No newline at end of file +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? \ No newline at end of file diff --git a/day_1/solution.py b/day_1/solution.py deleted file mode 100644 index 89e90e0..0000000 --- a/day_1/solution.py +++ /dev/null @@ -1,6 +0,0 @@ -import os - -with open('advent_of_code_2023\day_1\input.txt', 'r') as file: - input = file.read() - -print(input) diff --git a/day_10/part 1 solution.py b/day_10/part 1 solution.py new file mode 100644 index 0000000..76487f6 --- /dev/null +++ b/day_10/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_10\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_10/part 2 solution.py b/day_10/part 2 solution.py new file mode 100644 index 0000000..76487f6 --- /dev/null +++ b/day_10/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_10\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_11/part 1 solution.py b/day_11/part 1 solution.py new file mode 100644 index 0000000..7d77b2a --- /dev/null +++ b/day_11/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_11\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_11/part 2 solution.py b/day_11/part 2 solution.py new file mode 100644 index 0000000..7d77b2a --- /dev/null +++ b/day_11/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_11\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_12/part 1 solution.py b/day_12/part 1 solution.py new file mode 100644 index 0000000..1e557ee --- /dev/null +++ b/day_12/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_12\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_12/part 2 solution.py b/day_12/part 2 solution.py new file mode 100644 index 0000000..1e557ee --- /dev/null +++ b/day_12/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_12\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_13/part 1 solution.py b/day_13/part 1 solution.py new file mode 100644 index 0000000..d6fb9de --- /dev/null +++ b/day_13/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_13\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_13/part 2 solution.py b/day_13/part 2 solution.py new file mode 100644 index 0000000..d6fb9de --- /dev/null +++ b/day_13/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_13\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_14/part 1 solution.py b/day_14/part 1 solution.py new file mode 100644 index 0000000..e6b7cca --- /dev/null +++ b/day_14/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_14\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_14/part 2 solution.py b/day_14/part 2 solution.py new file mode 100644 index 0000000..e6b7cca --- /dev/null +++ b/day_14/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_14\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_15/part 1 solution.py b/day_15/part 1 solution.py new file mode 100644 index 0000000..e9109fa --- /dev/null +++ b/day_15/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_15\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_15/part 2 solution.py b/day_15/part 2 solution.py new file mode 100644 index 0000000..e9109fa --- /dev/null +++ b/day_15/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_15\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_16/part 1 solution.py b/day_16/part 1 solution.py new file mode 100644 index 0000000..c86f193 --- /dev/null +++ b/day_16/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_16\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_16/part 2 solution.py b/day_16/part 2 solution.py new file mode 100644 index 0000000..c86f193 --- /dev/null +++ b/day_16/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_16\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_17/part 1 solution.py b/day_17/part 1 solution.py new file mode 100644 index 0000000..e61ce57 --- /dev/null +++ b/day_17/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_17\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_17/part 2 solution.py b/day_17/part 2 solution.py new file mode 100644 index 0000000..e61ce57 --- /dev/null +++ b/day_17/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_17\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_18/part 1 solution.py b/day_18/part 1 solution.py new file mode 100644 index 0000000..50fbe4a --- /dev/null +++ b/day_18/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_18\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_18/part 2 solution.py b/day_18/part 2 solution.py new file mode 100644 index 0000000..50fbe4a --- /dev/null +++ b/day_18/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_18\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_19/part 1 solution.py b/day_19/part 1 solution.py new file mode 100644 index 0000000..93ee032 --- /dev/null +++ b/day_19/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_19\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_19/part 2 solution.py b/day_19/part 2 solution.py new file mode 100644 index 0000000..93ee032 --- /dev/null +++ b/day_19/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_19\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_2/part 1 solution.py b/day_2/part 1 solution.py new file mode 100644 index 0000000..1d5aac1 --- /dev/null +++ b/day_2/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_2\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_2/part 2 solution.py b/day_2/part 2 solution.py new file mode 100644 index 0000000..1d5aac1 --- /dev/null +++ b/day_2/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_2\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_20/part 1 solution.py b/day_20/part 1 solution.py new file mode 100644 index 0000000..6bf0bc9 --- /dev/null +++ b/day_20/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_20\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_20/part 2 solution.py b/day_20/part 2 solution.py new file mode 100644 index 0000000..6bf0bc9 --- /dev/null +++ b/day_20/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_20\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_21/part 1 solution.py b/day_21/part 1 solution.py new file mode 100644 index 0000000..7a75f49 --- /dev/null +++ b/day_21/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_21\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_21/part 2 solution.py b/day_21/part 2 solution.py new file mode 100644 index 0000000..7a75f49 --- /dev/null +++ b/day_21/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_21\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_22/part 1 solution.py b/day_22/part 1 solution.py new file mode 100644 index 0000000..ac04b2d --- /dev/null +++ b/day_22/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_22\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_22/part 2 solution.py b/day_22/part 2 solution.py new file mode 100644 index 0000000..ac04b2d --- /dev/null +++ b/day_22/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_22\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_23/part 1 solution.py b/day_23/part 1 solution.py new file mode 100644 index 0000000..754518c --- /dev/null +++ b/day_23/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_23\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_23/part 2 solution.py b/day_23/part 2 solution.py new file mode 100644 index 0000000..754518c --- /dev/null +++ b/day_23/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_23\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_24/part 1 solution.py b/day_24/part 1 solution.py new file mode 100644 index 0000000..e7540fb --- /dev/null +++ b/day_24/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_24\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_24/part 2 solution.py b/day_24/part 2 solution.py new file mode 100644 index 0000000..e7540fb --- /dev/null +++ b/day_24/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_24\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_25/part 1 solution.py b/day_25/part 1 solution.py new file mode 100644 index 0000000..a45fbe4 --- /dev/null +++ b/day_25/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_25\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_25/part 2 solution.py b/day_25/part 2 solution.py new file mode 100644 index 0000000..a45fbe4 --- /dev/null +++ b/day_25/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_25\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_3/part 1 solution.py b/day_3/part 1 solution.py new file mode 100644 index 0000000..07bb032 --- /dev/null +++ b/day_3/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_3\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_3/part 2 solution.py b/day_3/part 2 solution.py new file mode 100644 index 0000000..07bb032 --- /dev/null +++ b/day_3/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_3\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_4/part 1 solution.py b/day_4/part 1 solution.py new file mode 100644 index 0000000..8812908 --- /dev/null +++ b/day_4/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_4\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_4/part 2 solution.py b/day_4/part 2 solution.py new file mode 100644 index 0000000..8812908 --- /dev/null +++ b/day_4/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_4\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_5/part 1 solution.py b/day_5/part 1 solution.py new file mode 100644 index 0000000..5878674 --- /dev/null +++ b/day_5/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_5\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_5/part 2 solution.py b/day_5/part 2 solution.py new file mode 100644 index 0000000..5878674 --- /dev/null +++ b/day_5/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_5\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_6/part 1 solution.py b/day_6/part 1 solution.py new file mode 100644 index 0000000..7817f1e --- /dev/null +++ b/day_6/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_6\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_6/part 2 solution.py b/day_6/part 2 solution.py new file mode 100644 index 0000000..7817f1e --- /dev/null +++ b/day_6/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_6\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_7/part 1 solution.py b/day_7/part 1 solution.py new file mode 100644 index 0000000..5aeaff1 --- /dev/null +++ b/day_7/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_7\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_7/part 2 solution.py b/day_7/part 2 solution.py new file mode 100644 index 0000000..5aeaff1 --- /dev/null +++ b/day_7/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_7\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_8/part 1 solution.py b/day_8/part 1 solution.py new file mode 100644 index 0000000..3016a8f --- /dev/null +++ b/day_8/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_8\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_8/part 2 solution.py b/day_8/part 2 solution.py new file mode 100644 index 0000000..3016a8f --- /dev/null +++ b/day_8/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_8\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_9/part 1 solution.py b/day_9/part 1 solution.py new file mode 100644 index 0000000..04c03e0 --- /dev/null +++ b/day_9/part 1 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_9\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/day_9/part 2 solution.py b/day_9/part 2 solution.py new file mode 100644 index 0000000..04c03e0 --- /dev/null +++ b/day_9/part 2 solution.py @@ -0,0 +1,6 @@ +import os + +with open('advent_of_code_2023\day_9\input.txt', 'r') as file: + input = file.read() + +print(input) diff --git a/organise_my_repo.py b/organise_my_repo.py index 9f7f3b8..1d5a9bd 100644 --- a/organise_my_repo.py +++ b/organise_my_repo.py @@ -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: