Skip to content

Instantly share code, notes, and snippets.

@BinaryPaean
Created December 1, 2023 23:19
Show Gist options
  • Save BinaryPaean/71a35e15b5e17eba42f1ead38f16e114 to your computer and use it in GitHub Desktop.
Save BinaryPaean/71a35e15b5e17eba42f1ead38f16e114 to your computer and use it in GitHub Desktop.
Python Advent of Code 2023 - Day 1
import re
import array
import sys
def handle_elf_line(line):
"Processes a single line from the input file for the 2023 advent of code."
rstring = r'(?=(\d|one|two|three|four|five|six|seven|eight|nine))'
elfnums = re.compile(rstring)
translate = {
'1': '1',
'one': '1',
'2': '2',
'two': '2',
'3': '3',
'three': '3',
'4': '4',
'four': '4',
'5': '5',
'five': '5',
'6': '6',
'six': '6',
'7': '7',
'seven': '7',
'8': '8',
'eight': '8',
'9': '9',
'nine': '9'
}
match = elfnums.findall(line)
if match:
return int(translate[match[0]] + translate[match[-1]])
else:
return 0
with open(sys.argv[1]) as f:
values = array.array('i')
for line in f:
values.append(handle_elf_line(line))
print(sum(values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment