Skip to content

Commit

Permalink
Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
lab57 committed Dec 4, 2022
1 parent 1f3eb5f commit 4927f27
Show file tree
Hide file tree
Showing 4 changed files with 1,044 additions and 4 deletions.
8 changes: 6 additions & 2 deletions day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
input = file.read().split("\n")


input = [(sack[0:len(sack)//2], sack[len(sack)//2:]) for sack in input]
input = [(sack[0:len(sack)//2], sack[len(sack)//2:]) for sack in input] #split in half


def getPriority(letter):
"""
Calculate the priority of a letter char
"""
return ord(letter)-ord("a")+1 if letter.islower() else ord(letter)-ord("A")+27


Expand All @@ -20,7 +24,7 @@ def getPriority(letter):
for sack in input:
print(sack)
for letter in sack[0]:
if letter in sack[1]:
if letter in sack[1]: #check if letter in other sack
score += getPriority(letter)
break
print(score)
Expand Down
8 changes: 6 additions & 2 deletions day3_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@


def getPriority(letter):
"""
Calculate the priority of a letter char
"""
return ord(letter)-ord("a")+1 if letter.islower() else ord(letter)-ord("A")+27


score = 0
for i in range(0, len(input),3):
for i in range(0, len(input),3): #skip over the input indicies in steps of 3
for letter in input[i]:
if letter in input[i+1] and letter in input[i+2]:
if letter in input[i+1] and letter in input[i+2]: #check if letter in both sacks
score += getPriority(letter)
break
print(score)
32 changes: 32 additions & 0 deletions day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Advent of Code 2022 - Day 4
Notes:
Time: ~6 minutes
map() is useful, worth praciting. Couldve killed lines 14-18 and simplifed the rest
"""
with open("day4.txt", "r") as file:
input = file.read().split("\n")

contained = 0
for line in input:
#split into groups of ints for each elf
split1 = line.split(",")
elf1 = split1[0].split("-")
elf2 = split1[1].split("-")
elf1 = [int(elf1[0]), int(elf1[1])]
elf2 = [int(elf2[0]), int(elf2[1])]

#check if both values for elf2 are contained in elf1, and vice versa
#change the ors to ands for part 1
if elf1[0] <= elf2[0] <= elf1[1] or elf1[0] <= elf2[1] <= elf1[1]:
contained += 1
elif elf2[0] <= elf1[0] <= elf2[1] or elf2[0] <= elf1[1] <= elf2[1]:
contained += 1
print(contained)





Loading

0 comments on commit 4927f27

Please sign in to comment.