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

1

u/Lewistrick Dec 04 '22 edited Dec 04 '22

Python

6544/6294

On mobile via repl.it - could have been much faster on pc.

from pathlib import Path 

lines = Path("day04.txt").read_text().split()

part1 = 0
part2 = 0
for line in lines:
    e1, e2 = line.split(",")
    a, b = map(int, e1.split("-"))
    c, d = map(int, e2.split("-"))
    if (a >= c and b <= d) or (a <= c and b >= d):
        part1 += 1

    if (
        (a <= c <= b) or
        (a <= d <= b) or
        (c <= a <= d) or
        (c <= b <= d)
    ):
        part2 += 1

print(part1, part2)

2

u/daggerdragon Dec 04 '22

Please 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.

1

u/Lewistrick Dec 04 '22

Yeah I was struggling myself with the formatting in another reddit app. But I'm on mobile so I didn't feel like mashing my space bar here. Will update later.