Avent of Code - Day 04 - Camp CleanupsteemCreated with Sketch.

in #adventofcodelast year (edited)

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!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.032
BTC 64799.61
ETH 3102.94
USDT 1.00
SBD 3.83