-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7e7cb03
Showing
41 changed files
with
5,080 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.9.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import re | ||
|
||
|
||
def get_alpha_regex(): | ||
return re.compile("[a-zA-Z]") | ||
|
||
|
||
def get_input(): | ||
regex = get_alpha_regex() | ||
with open("one.txt", "r") as f: | ||
return [regex.sub("", l) for l in f.read().splitlines()] | ||
|
||
|
||
def get_first_and_last(string): | ||
return int(string[0] + string[-1]) | ||
|
||
|
||
def main(): | ||
input = get_input() | ||
print(sum(get_first_and_last(i) for i in input)) | ||
|
||
|
||
main() | ||
|
||
# 57346 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import re | ||
|
||
NUMS = { | ||
"one": 1, | ||
"two": 2, | ||
"three": 3, | ||
"four": 4, | ||
"five": 5, | ||
"six": 6, | ||
"seven": 7, | ||
"eight": 8, | ||
"nine": 9, | ||
} | ||
|
||
|
||
def get_alpha_regex(): | ||
return re.compile("[a-zA-Z]") | ||
|
||
|
||
def get_input(): | ||
with open("one.txt", "r") as f: | ||
return f.read().splitlines() | ||
|
||
|
||
def sub_nums(input_list): | ||
inp = [] | ||
regex = get_alpha_regex() | ||
for i in input_list: | ||
i = replace_first_and_last(i) | ||
i = regex.sub("", i) | ||
inp.append(i) | ||
return inp | ||
|
||
|
||
def get_first_or_last(positions, pos): | ||
func = max if pos == "last" else min | ||
pos = func(positions.keys()) | ||
return pos, positions[pos] | ||
|
||
|
||
def replace_(string, pos, num): | ||
string = list(string) | ||
string[pos] = str(num) | ||
string = "".join(string) | ||
return string | ||
|
||
|
||
def replace_first_and_last(string): | ||
num_string_positions = { | ||
string.find(i): i for i in NUMS.keys() if string.find(i) != -1 | ||
} | ||
r_num_string_positions = { | ||
string.rfind(i): i for i in NUMS.keys() if string.rfind(i) != -1 | ||
} | ||
if not num_string_positions or not r_num_string_positions: | ||
return string | ||
|
||
first_pos, first_num = get_first_or_last(num_string_positions, "first") | ||
last_pos, last_num = get_first_or_last(r_num_string_positions, "last") | ||
string = replace_(string, first_pos, NUMS[first_num]) | ||
string = replace_(string, last_pos, NUMS[last_num]) | ||
|
||
return string | ||
|
||
|
||
def get_first_and_last(string): | ||
return int(string[0] + string[-1]) | ||
|
||
|
||
def main(): | ||
input = get_input() | ||
input_list = sub_nums(input) | ||
print(sum(get_first_and_last(i) for i in input_list)) | ||
|
||
|
||
main() | ||
|
||
# 57345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
two1nine | ||
eightwothree | ||
abcone2threexyz | ||
xtwone3four | ||
4nineeightseven2 | ||
zoneight234 | ||
7pqrstsixteen |
Oops, something went wrong.