-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_1.py
More file actions
146 lines (112 loc) · 3.67 KB
/
day_1.py
File metadata and controls
146 lines (112 loc) · 3.67 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
f=open("day_1_input")
def part1_v1():
# place holder for sum
num=0
for line in f.readlines():
# how to check if a string character can be int-y-fied ?
num_string=''
for char in line:
try:
int(char)
except ValueError:
pass # maybe line.remove(char) ?
else:
num_string+=char
tens=int(num_string[0])
units=int(num_string[-1])
num+=10*tens+units
return num
def find_all(sub,str):
list_return=[]
# look for instance of string
# if not found, return currently built set
# if found, attach index to list, jump to next position and repeat
i_sub=str.find(sub)
while i_sub!=-1:
list_return.append((i_sub))
i_sub=str.find(sub,i_sub+1)
return list_return
def decode(num):
dict_alphabetic_digits={
'one':1,
'two':2,
'three':3,
'four':4,
'five':5,
'six':6,
'seven':7,
'eight':8,
'nine':9
}
if num in dict_alphabetic_digits:
return dict_alphabetic_digits[num]
else:
return int(num)
# not working atm
def part2_v1():
dict_alphabetic_digits={
'one':1,
'two':2,
'three':3,
'four':4,
'five':5,
'six':6,
'seven':7,
'eight':8,
'nine':9
}
# place holder for sum
num=0
lines=f.readlines()
for i_line,line in enumerate(lines):
list_digits_total=[]
list_digit_indices_total=[]
for digit in dict_alphabetic_digits:
list_digit_indices=find_all(digit,line)
list_digits=[digit for _ in list_digit_indices]
list_digit_indices_total+=list_digit_indices
list_digits_total+=list_digits
for digit in range(1,10):
list_digit_indices=find_all(str(digit),line)
list_digits=[digit for _ in list_digit_indices]
list_digit_indices_total+=list_digit_indices
list_digits_total+=list_digits
final_list=list(zip(list_digit_indices_total,list_digits_total))
final_list.sort(key=lambda item: item[0])
tens=decode(final_list[0][1])
units=decode(final_list[-1][1])
if len(final_list)<2:
print("WARNING: have none- or single-value list "+str(final_list)+" from line "+line.strip())
aux=units
else:
aux=10*tens+units
num+=aux
return num
# trying again after learning about the existence of rfind
def part2_v2():
first_digit=''
last_digit=''
list_digits=['one','two','three','four','five','six','seven','eight','nine']
list_digits+=map(str,range(1,10))
sum_calibration_values=0
for line in f.readlines():
i_start_first_digit=len(line)
i_start_last_digit=0
for digit in list_digits:
i_start_digit=line.find(digit)
i_last_digit=line.rfind(digit)
if i_start_digit != -1:
if i_start_digit < i_start_first_digit:
i_start_first_digit = i_start_digit
first_digit=digit
if i_last_digit != -1:
if i_last_digit > i_start_last_digit:
i_start_last_digit = i_last_digit
last_digit=digit
if i_start_first_digit==i_start_last_digit:
sum_calibration_values+=decode(first_digit)
else:
sum_calibration_values+=(10*decode(first_digit)+decode(last_digit))
return sum_calibration_values
if __name__=='__main__':
print(part2_v2())