Created
December 4, 2022 18:39
-
-
Save dartharva/b539597275faf7c39bf6d36ed8b3179f to your computer and use it in GitHub Desktop.
Advent of Code 2022, Day 4 - Simple, verbose python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('input.txt', 'r') as inputfile: | |
assignments = [i.rsplit(',') for i in inputfile.read().split('\n')] | |
for x in assignments: | |
for i,j in enumerate(x): | |
k = j.split('-') | |
x[i] = set(range(int(k[0]), int(k[1])+1)) | |
count_1 = 0 | |
count_2 = 0 | |
for i in assignments: | |
if i[0].issubset(i[1]) or i[1].issubset(i[0]): | |
count_1 += 1 | |
if not i[0].isdisjoint(i[1]): | |
count_2 += 1 | |
print(count_1) #Part 1 | |
print(count_2) #Part 2 |
Not sure what you mean. The second loop modifies the assignment list to convert each string element into a list of a pair of sets containing the elf pair's respective assignments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice and clean solution. How do you assign x to assignments variable for the second loop?