-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.py
More file actions
47 lines (42 loc) · 1.27 KB
/
day3.py
File metadata and controls
47 lines (42 loc) · 1.27 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
file = open("./day3_input", "r")
content = file.read()
strings = content.split("\n")
# find the largest number then the second largest after that
total = 0
for string in strings:
# find first
first_largest = 0
second_largest = 0
for letter in string[:-1]:
if int(letter) > first_largest:
first_largest = int(letter)
# find second
for letter2 in string[string.find(str(first_largest)) + 1:]:
if int(letter2) > second_largest:
second_largest = int(letter2)
number = first_largest * 10 + second_largest
total += number
print(total)
total = 0
def find_next(position: int, string: str):
next = 0
for letter in string[: (position * -1)] if position != 0 else string:
if int(letter) > next:
next = int(letter)
return string.find(str(next))
for string in strings:
origin = string
# find first
first = 0
for letter in string[:-11]:
if int(letter) > first:
first = int(letter)
position = string.find(str(first))
number = string[position]
string = string[position + 1:]
for i in range(10, -1, -1):
next = find_next(i, string)
number = number + string[next]
string = string[next + 1:]
total += int(number)
print(total)