Skip to content

Instantly share code, notes, and snippets.

@dartharva
Created December 4, 2022 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dartharva/b539597275faf7c39bf6d36ed8b3179f to your computer and use it in GitHub Desktop.
Save dartharva/b539597275faf7c39bf6d36ed8b3179f to your computer and use it in GitHub Desktop.
Advent of Code 2022, Day 4 - Simple, verbose python
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
@purran23
Copy link

purran23 commented Dec 5, 2022

Nice and clean solution. How do you assign x to assignments variable for the second loop?

@dartharva
Copy link
Author

dartharva commented Dec 5, 2022

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