Skip to content

Commit

Permalink
feature: add day 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
anatxiki committed Dec 2, 2023
1 parent 7c217b3 commit 8ad0e12
Show file tree
Hide file tree
Showing 4 changed files with 1,191 additions and 0 deletions.
38 changes: 38 additions & 0 deletions anatxiki/2023/day1/day1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import regex as re

myregex = 'one|two|three|four|five|six|seven|eight|nine|[1-9]'
textToDigits = {
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9'
}

def part1():
part1_solution = 0
with open('input.txt', 'r') as file:
for line in file:
digits = [x for x in line if x.isnumeric()]
checksum = digits[0] + digits[-1]
part1_solution += int(checksum)

print(part1_solution)

def part2():
part2_solution = 0
with open('input.txt', 'r') as file:
for line in file:
matches = re.findall(myregex, line, overlapped=True)
digits = [textToDigits.get(x,x) for x in matches]

checksum = digits[0] + digits[-1]
part2_solution += int(checksum)
print(part2_solution)

part1()
part2()
Loading

0 comments on commit 8ad0e12

Please sign in to comment.