Added day1 part1 and part2

This commit is contained in:
FrederikBaerentsen 2023-12-01 10:47:08 +01:00
parent ae87d008b1
commit b5bcd40fe2
6 changed files with 2066 additions and 0 deletions

1000
2023/day1/part1/input Normal file

File diff suppressed because it is too large Load Diff

14
2023/day1/part1/part1.py Normal file
View File

@ -0,0 +1,14 @@
#!/bin/python3
import re
import sys
input_f = sys.argv[1]
count = 0
with open(input_f) as file:
for line in file:
x=re.findall('\d{1}',line.rstrip())
count = count + int(x[0] + x[len(x)-1])
print(count)

4
2023/day1/part1/test Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

1000
2023/day1/part2/input Normal file

File diff suppressed because it is too large Load Diff

41
2023/day1/part2/part2.py Normal file
View File

@ -0,0 +1,41 @@
#!/bin/python3
import re
import sys
input_f = sys.argv[1]
count = 0
#list of values that can be found
lst = ['one','two','three','four','five','six','seven','eight','nine','1','2','3','4','5','6','7','8','9','0']
#list to match numbers with
num = ['zero','one','two','three','four','five','six','seven','eight','nine']
#open file
with open(input_f) as file:
for line in file:
tmp = line.rstrip() #read the line
z = []
#find all matches from lst in the line
for i in lst:
for m in re.finditer(i,tmp):
#make a 2d array
z.append([m.start(),m.group(0)])
#sort the 2d array with first column as key
y=sorted(z,key=lambda z: z[0])
d1 = y[0][1]
try:
d1 = num.index(d1)
except:
pass
d2 = y[len(y)-1][1]
try:
d2 = num.index(d2)
except:
pass
count = count + int(str(d1) + str(d2))
print(count)

7
2023/day1/part2/test Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen