-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.py
33 lines (29 loc) · 1.21 KB
/
day1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
import numpy as np
import re
f = open("ressources/day1.txt", "r") #Open File
lines = f.readlines() #Separate in lines
#Q1
calibration = 0
for line in lines:
numbers = re.findall(r'\d', line) #Regex Directly finds all digits (\d)
number1 = numbers[0]
number2 = numbers[-1]
twodigitnumber = int(number1+number2)
calibration += twodigitnumber
print("Calibration value is: {}".format(calibration))
#Q2
calibration = 0
numbers = r"""(?=(\d|one|two|three|four|five|six|seven|eight|nine))"""
#Regex finds all digits (\d), as well as all keywords. ?= signify that there might be overlap, like twone
digitDict = {
'one': '1','two': '2','three': '3','four': '4','five': '5','six': '6','seven': '7','eight': '8','nine': '9','zero': '0',
'1': '1','2': '2','3': '3','4': '4','5': '5','6': '6','7': '7','8': '8','9': '9','0': '0'
} #Convert both digits and words to digits, so we don't have to check if it is int or word
for line in lines:
numbersDigit = re.findall(numbers, line)
number1 = digitDict[numbersDigit[0]]
number2 = digitDict[numbersDigit[-1]]
twodigitnumber = int(number1+number2)
calibration += twodigitnumber
print("New Calibration is: {}".format(calibration))