2023 - Day 1 part 2
This commit is contained in:
parent
6e3705e763
commit
7d22b9ed0d
3 changed files with 138 additions and 0 deletions
66
2023/1/1_2.py
Normal file
66
2023/1/1_2.py
Normal file
|
@ -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)}")
|
65
2023/1/1_2_bad.py
Normal file
65
2023/1/1_2_bad.py
Normal file
|
@ -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)
|
7
2023/1/input_example2.txt
Normal file
7
2023/1/input_example2.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
Loading…
Reference in a new issue