-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
70 lines (56 loc) · 1.72 KB
/
day3.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
# Nelson Dane
# Advent of Code 2022 Day 3
# Read in the input file
with open('input.txt', 'r') as f:
data = f.read().splitlines()
# Part 1
# Return the priority of each letter
def get_priority(input):
# Convert to ASCII then subtract to find priority
if not input.isupper():
return ord(input)-96
else:
return ord(input)-64+26
# Split lines in half
def split_input(input):
return input[:len(input)//2], input[len(input)//2:]
# Declare priority and dupes
prioritySum = 0
dupes = []
# Loop through data
for i in range(len(data)):
temp_dupes = []
first_half, second_half = split_input(data[i])
# Find duplicate letters
for j in range(len(first_half)):
if first_half[j] in second_half and first_half[j] not in temp_dupes:
temp_dupes.append(first_half[j])
dupes += temp_dupes
# Find priority of each letter
for i in range(len(dupes)):
prioritySum += get_priority(dupes[i])
print(f'Part 1 Sum of Item Priorities: {prioritySum}')
# Part 2
# Priority and badges
prioritySum2 = 0
badges = []
# Variables for lists of 3
temp3 = []
count = 0
# Loop through data and split into lists of 3
for i in range(len(data)+1):
if count % 3 == 0 and count != 0:
temp_dupes = []
# Find duplicate letters
for j in range(len(temp3[0])):
if temp3[0][j] in temp3[1] and temp3[0][j] in temp3[2] and temp3[0][j] not in temp_dupes:
temp_dupes.append(temp3[0][j])
badges += temp_dupes
temp3 = []
if i != len(data):
temp3.append(data[i])
count += 1
# Find priority of each letter
for i in range(len(badges)):
prioritySum2 += get_priority(badges[i])
print(f'Part 2 Sum of Badge Priorities: {prioritySum2}')