Skip to content

Commit

Permalink
Day 1-10
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshallam93 committed Dec 10, 2023
0 parents commit 7e7cb03
Show file tree
Hide file tree
Showing 41 changed files with 5,080 additions and 0 deletions.
1 change: 1 addition & 0 deletions 1/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.5
25 changes: 25 additions & 0 deletions 1/1-1.py
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
78 changes: 78 additions & 0 deletions 1/1-2.py
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
7 changes: 7 additions & 0 deletions 1/1-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
Loading

0 comments on commit 7e7cb03

Please sign in to comment.