diff --git a/2023/1/1_2.py b/2023/1/1_2.py new file mode 100644 index 0000000..6a4a9e7 --- /dev/null +++ b/2023/1/1_2.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# 2023 - Advent Of Code 1 - part 2 + +# file = 'input_example2.txt' +file = 'input.txt' + +text2num = [ + ("oneight", "1eight"), + ("threeight", "3eight"), + ("fiveight", "5eight"), + ("nineight", "9eight"), + + ("eightwo", "8two"), + ("eighthree", "8three"), + + ("twone", "2one"), + + ("sevenine", "7nine"), + + ("one", "1"), + ("two", "2"), + ("three", "3"), + ("four", "4"), + ("five", "5"), + ("six", "6"), + ("seven", "7"), + ("eight", "8"), + ("nine", "9") +] + + +def replace_text(lines_list): + print(lines_list) + replaced_lines = [] + for line in lines_list: + tmp_line = line + for token in text2num: + tmp_line2 = tmp_line.replace(token[0], token[1]) + tmp_line = tmp_line2 + replaced_lines.append(tmp_line) + + return replaced_lines + + +def num_and_sum(lines): + accum = 0 + for line in lines: + first_num = last_num = -1 + for c in line: + if '0' <= c <= '9': + if first_num == -1: + first_num = c + last_num = c + line_num = first_num + last_num + # print(f"first: {first_num}, last: {last_num}") + # print(f"line_num: {line_num}") + accum += int(line_num) + + return accum + + +# pylint: disable=consider-using-with +input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] +new_lines = replace_text(input_lines) +# print(new_lines) +print(f"sum: {num_and_sum(new_lines)}") diff --git a/2023/1/1_2_bad.py b/2023/1/1_2_bad.py new file mode 100644 index 0000000..500fa58 --- /dev/null +++ b/2023/1/1_2_bad.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# 2023 - Advent Of Code 1 - part 2 +import re + +# file = 'input_example2.txt' +file = 'input.txt' + +text2num = [ + ("one", "1"), + ("two", "2"), + ("three", "3"), + ("four", "4"), + ("five", "5"), + ("six", "6"), + ("seven", "7"), + ("eight", "8"), + ("nine", "9") +] + + +def col_print(c1, c2): + # pylint: disable=unnecessary-list-index-lookup + for x, _ in enumerate(c1): + print(c1[x], c2[x]) + + +def replace_text(lines_list): + print(lines_list) + + replaced_lines = [] + for line in lines_list: + tmp_line = line + for c, _ in enumerate(line): + for token in text2num: + tmp_line2 = tmp_line[:c] + re.sub(rf'^{token[0]}', token[1], tmp_line[c:]) + tmp_line = tmp_line2 + + replaced_lines.append(tmp_line) + + return replaced_lines + + +def num_and_sum(lines): + accum = 0 + for line in lines: + first_num = last_num = 0 + for c in line: + if '0' <= c <= '9': + if first_num == 0: + first_num = c + last_num = c + line_num = first_num + last_num + # print(f"first: {first_num}, last: {last_num}") + # print(f"line_num: {line_num}") + accum += int(line_num) + + return accum + +# pylint: disable=consider-using-with +input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] +new_lines = replace_text(input_lines) +print(new_lines) +print(f"sum: {num_and_sum(new_lines)}") + +col_print(input_lines, new_lines) diff --git a/2023/1/input_example2.txt b/2023/1/input_example2.txt new file mode 100644 index 0000000..41aa89c --- /dev/null +++ b/2023/1/input_example2.txt @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen