-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.py
73 lines (46 loc) · 1.56 KB
/
day01.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from common import *
def part1():
lines = getDayInput(1, 1)
digits = []
for line in lines:
lineDigits = []
for char in line:
if char.isdigit():
lineDigits.append(int(char))
# "calibration value" of this line is the first and last digits, concatenated
calibValue = int(f"{lineDigits[0]}{lineDigits[-1]}")
digits.append(calibValue)
return sum(digits)
def part2():
NUMBERS = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
lines = getDayInput(1, 2, False)
digits = []
for line in lines:
chars = list(line)
# check for any spelled out numbers, once for each char in the list)
for _ in range(0, len(chars)):
for num in NUMBERS: # check every spelled-out number
charString = "".join(chars)
if num in charString:
# we've found a number, where was it in the line?
index = charString.index(num)
# what digit is it?
digit = str(NUMBERS.index(num) + 1)
# replace *second letter* of number with digit
# cannot do the first letter because of overlapping letters;
# eg: nineight, twone, etc (fuck this)
chars[index + 1] = digit
# print(chars)
# remove non-digits from the list
lineDigits = []
for char in chars:
if char.isdigit():
lineDigits.append(int(char))
# print(lineDigits)
# "calibration value" of this line is the first and last digits, concatenated
calibValue = int(f"{lineDigits[0]}{lineDigits[-1]}")
# print(calibValue)
digits.append(calibValue)
return sum(digits)
print(f"Part 1: {part1()}")
print(f"Part 2: {part2()}")