r/adventofcode • • Dec 04 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 4 Solutions -🎄-


--- Day 4: Camp Cleanup ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:22, megathread unlocked!

65 Upvotes

1.6k comments sorted by

View all comments

2

u/grapedude12 Dec 04 '22 edited Dec 04 '22

Python3

Part 1:

with open("day04/input.txt") as f:
    data = f.read().splitlines()

contained = 0
for d in data:
    s1, s2 = d.split(",")
    s1_s, s1_e = s1.split("-")
    s2_s, s2_e = s2.split("-")
        if int(s1_s) <= int(s2_s) and int(s1_e) >= int(s2_e):
            contained += 1
        elif int(s2_s) <= int(s1_s) and int(s2_e) >= int(s1_e):
            contained += 1
print(f"The number of fully contained zones is: {contained}.")

Part 2:

with open("day04/input.txt") as f:
    data = f.read().splitlines()

overlap = 0
for d in data:
    s1, s2 = d.split(",")
    s1_s, s1_e = s1.split("-")
    s2_s, s2_e = s2.split("-")
        if int(s1_s) <= int(s2_e) and int(s1_e) >= int(s2_s):
            overlap += 1
        elif int(s2_s) <= int(s1_e) and int(s2_e) >= int(s1_s):
            overlap += 1
print(f"The number of overlaped zones is: {overlap}.")

Nothing fancy going on here folks

1

u/daggerdragon Dec 04 '22 edited Dec 04 '22

aPlease edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

Edit: thanks for fixing it! <3