Skip to content

Latest commit

 

History

History
66 lines (50 loc) · 1.49 KB

4.md

File metadata and controls

66 lines (50 loc) · 1.49 KB

Day 4

I had Part 1 done in 2 minutes, but I neglected to realize that I had not converted the strings to ints...

Part 1 Part 2 Total
Time 8:49 3:11 12:00

Part 1

import aocd
with open("sess") as f:
    sess = f.readline()
din = aocd.get_data(session=sess, year=2022, day=4).split("\n")

tot = 0
for line in din:
    aa = int(line.split(',')[0].split('-')[0])
    ab = int(line.split(',')[0].split('-')[1])
    ba = int(line.split(',')[1].split('-')[0])
    bb = int(line.split(',')[1].split('-')[1])

    print(aa, ab, ba, bb)
    
    if ((ba >= aa) and (bb <= ab)) or ((aa >= ba) and (ab <= bb)):
        print("^")
        tot += 1

print(tot)

Here's a version of Part 1 with sets that I thought of after doing Part 2. It seems this year's theme is set theory? Two days in a row have easy solutions with set theory.

a = range(aa, ab+1)
b = range(ba, bb+1)
if set(a).issubset(set(b)) or set(b).issubset(set(a)):
    tot += 1

Part 2

import aocd
with open("sess") as f:
    sess = f.readline()
din = aocd.get_data(session=sess, year=2022, day=4).split("\n")

tot = 0
for line in din:
    aa = int(line.split(',')[0].split('-')[0])
    ab = int(line.split(',')[0].split('-')[1])
    ba = int(line.split(',')[1].split('-')[0])
    bb = int(line.split(',')[1].split('-')[1])

    print(aa, ab, ba, bb)

    a = range(aa, ab+1)
    b = range(ba, bb+1)

    if len(list(set(a).intersection(set(b)))):
        print("^")
        tot += 1

print(tot)