Avent of Code - Day 04 - Camp Cleanup

in adventofcode •  2 years ago 

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

Day 4 - Camp Cleanup

https://adventofcode.com/2022/day/4

97e461c00b601f75129638b2d40debb.jpg

Q1

import sys

file1 = open(sys.argv[1], "r")
ans = 0

while True:
        line = file1.readline()
        if not line:
                break
        line = line.strip()
        if not line:
                break
        arr = line.split(',')
        a, b = map(int, arr[0].split('-'))
        c, d = map(int, arr[1].split('-'))
        if (a >= c and b <= d) or (c >= a and d <= b):
                ans += 1

print(ans)

Q2

import sys

file1 = open(sys.argv[1], "r")
ans = 0

while True:
        line = file1.readline()
        if not line:
                break
        line = line.strip()
        if not line:
                break
        arr = line.split(',')
        a, b = map(int, arr[0].split('-'))
        c, d = map(int, arr[1].split('-'))
        if not (b < c or a > d):
                print(a, b, c, d)
                ans += 1

print(ans)

Today is about the interval intersection


Steem to the Moon!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!