DEV Community

DoctorLai
DoctorLai

Posted on

Avent of Code - Day 04 - Camp Cleanup

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Today is about the interval intersection


Steem to the Moon!

Oldest comments (0)