#AoC 2022 | Day 4 | Solutions & Spoilers

1508 messages · Page 2 of 2 (latest)

pastel pilot
#
with open("04_input.txt") as file:
    data = file.read().splitlines()

total_lines_contained = 0
total_lines_overlapping = 0

for line in data:
    assignment_1, assignment_2 = (tuple(map(int, interval.split("-"))) for interval in line.split(","))
    interval_1, interval_2 = set(range(assignment_1[0], assignment_1[1] + 1)), set(range(assignment_2[0], assignment_2[1] + 1))
    if interval_1 & interval_2 in (interval_1, interval_2):
        total_lines_contained += 1
    if interval_1 & interval_2:
        total_lines_overlapping += 1

print(total_lines_contained)
print(total_lines_overlapping)
somber notch
#

Super difficult pt. 1 and then... surprise even more difficult pt 2 no Christmas rest for you

jovial marten
#

oh right, 2019 25 was an intcode text adventure

vagrant token
#
  1. that's not a CLI that's a script interface
  2. how is 'a' and 'b' more clear than '1' and '2'? Literally 'the first part' and 'the second part'
somber notch
#

(you can do either one in the script interface, I checked)

kind niche
#

Nothing that can save characters unfortunately

wide ore
#
#d4p1
def d4p1(text):
    lines = text.splitlines()
    func = lambda x:any([all([c in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1)) for c in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1))]),all([z in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1)) for z in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1))])])
    #func = lambda x:[x.replace('-',' ').replace(',',' ').split()]
    for i in lines:
        yield func(i)

#d4p2
def d4p2(text):
    lines = text.splitlines()
    func = lambda x:any([any([c in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1)) for c in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1))]),any([z in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1)) for z in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1))])])
    for i in lines:
        yield func(i)
```d4 solve almost one line solution
#

actually i can make it less

somber notch
#

return map(lambda x ...., text.splitlines())

wide ore
#

oh yea lol

#

im so bad at map

pastel pilot
#

sum(anyfunc for i in text.splitlines()) you don't need the lambda

wide ore
#
def d4p1(text):
    return sum([any([all([c in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1)) for c in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1))]),all([z in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1)) for z in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1))])]) for x in text.splitlines()])
def d4p2(text):
    return sum([any([any([c in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1)) for c in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1))]),any([z in list(range(int(x.replace('-',' ').replace(',',' ').split()[2]),int(x.replace('-',' ').replace(',',' ').split()[3])+1)) for z in list(range(int(x.replace('-',' ').replace(',',' ').split()[0]),int(x.replace('-',' ').replace(',',' ').split()[1])+1))])]) for x in text.splitlines()])
#

here we go

gloomy comet
#

Had to write it on mobile, it was painful

import re
from operator import methodcaller
from pathlib import Path

data = Path("input.txt").read_text().split("\n")

pattern = re.compile(r"(\d+)-(\d+),(\d+)-(\d+)") 

print(sum((x1<=y1<=x2 and x1<=y2<=x2) or (y1<=x1<=y2 and y1<=x2<=y2) for x1, x2, y1, y2 in map(lambda g: map(int, g),  map(methodcaller("groups"), (map(pattern.match, data))))))

print(sum((x1<=y1<=x2 or x1<=y2<=x2 or y1<=x1<=y2) for x1, x2, y1, y2 in map(lambda g: map(int, g),  map(methodcaller("groups"), (map(pattern.match, data))))))

Is there a more interesting way to check overlap? Besides creating the full range

potent summit
#

I tend to think of this as if they don't overlap, that means either the first one takes place entirely before the second (first ending before second start) or the second one takes place entirely before the first (second ending before first start)

jovial marten
#

the negated version is more obvious to reason about imo

neat mountain
#

Wrote mine in the comfort of my desk.

def parse_sections(s: str):
    start, end = map(int, s.split("-"))
    return set(range(start, end + 1))


def main(data: str):
    redudnant_count = 0
    overlapping_count = 0
    for line in data.splitlines():
        # e - elf
        e1, e2 = map(parse_sections, line.split(","))
        if e1.issubset(e2) or e1.issuperset(e2):
            redudnant_count += 1
        if e1.intersection(e2):
            overlapping_count += 1
    print("Part 1:", redudnant_count)
    print("Part 2:", overlapping_count)
jovial marten
#
not (r1 < l2 or r2 < l1)
potent summit
#

Which is actually a mistake I made during my solve too, where I had one flipped

jovial marten
#

it is endings < beginnings

#

range [l, r]

potent summit
#

I see, r1 is an end. Zig was using x1,x2,y1,y2 so the 2's were the endings

jovial marten
#

ah, right

potent summit
jovial marten
#

like, this works for any intervals where the endpoints can be compared with <

plush ibex
#

106

print(sum(1-(a>c<=d<b or d>b>=a<c)+1j*(a<=d>=c<=b)for a,b,c,d in[eval(l.replace(*'-,'))for l in open(0)]))```
78 with encoding
```py
exec(bytes('牰湩⡴畳⡭ⴱ愨挾㴼㱤⁢牯搠戾㴾㱡⥣ㄫ⩪愨㴼㹤挽㴼⥢潦⁲ⱡⱢⱣ⁤湩敛慶⡬⹬敲汰捡⡥✪Ⱝ⤧昩牯氠椠灯湥〨崩⤩','u16')[2:])```
#

new best

#

it's a 106

median seal
#
import re

with open('input.txt') as f:
    data = f.read().splitlines()

part_1_matches = 0
part_2_matches = 0
for line in data:
    a1, a2, b1, b2 = map(int, re.findall(r'\d+', line))
    set_a, set_b = set(range(a1, a2+1)), set(range(b1, b2+1))
    if len(set_a | set_b) == len(set_a) or len(set_a | set_b) == len(set_b):
        part_1_matches += 1
    if set_a & set_b:
        part_2_matches += 1


print(part_1_matches) #Part 1
print(part_2_matches) #Part 2
neat mountain
#

Why using regex?

plush ibex
neat mountain
#

!e ```py
print(bytes('牰湩⡴畳⡭ⴱ愨挾㴼㱤⁢牯搠戾㴾㱡⥣ㄫ⩪愨㴼㹤挽㴼⥢潦⁲ⱡⱢⱣ⁤湩敛慶⡬⹬敲汰捡⡥✪Ⱝ⤧昩牯氠椠灯湥〨崩⤩','u16')[2:])

tulip ravenBOT
#

@neat mountain :white_check_mark: Your 3.11 eval job has completed with return code 0.

b"print(sum(1-(a>c<=d<b or d>b>=a<c)+1j*(a<=d>=c<=b)for a,b,c,d in[eval(l.replace(*'-,'))for l in open(0)]))"
plush ibex
#

yeah

jovial marten
#

185 bytes

plush ibex
#

but i still have the shortest when counting bytes

#

106

jovial marten
#

you don't have 76 bytes though 😛

plush ibex
#

ok but nobody here does

#

not in python, at least

kind niche
#

Good ol demorgans strikes again

plush ibex
#

yep

vagrant token
#

I read that as demagorgons and was confused lol

still matrix
#

I am оωo "sets are cool" #4962 py def day4(inp: str) -> tuple[int, int]: cnt = 0 tcnt = 0 for i in inp.strip().split("\n"): (af, at), (bf, bt) = map(lambda i: map(int, i.split("-")), i.split(",")) if set(range(af, at + 1)) & set(range(bf, bt + 1)) in [set(range(af, at + 1)), set(range(bf, bt + 1))]: cnt += 1 if set(range(af, at + 1)) & set(range(bf, bt + 1)): tcnt += 1 return (cnt, tcnt)

feral cliff
#

Sets probably easier to read but checking boundaries faster to compute

still matrix
#

but I am lazy

#

and I have a good PC

jovial marten
feral cliff
#

This puzzle is easy for most compute. Thinking scaling:)

still matrix
#

nononono

jovial marten
#

here this

still matrix
#

still, only 1k lines lol

jovial marten
#

number of lines isn't the issue

turbid nebula
#

I like doing this for fun 🤷‍♂️

jovial marten
#

the size of the interval is

turbid nebula
#

I'm not bothered about a hypothetical production implementation of a small fun little puzzle

feral cliff
#

For sure. Just talking:)

jovial marten
#

what I'm sad about is having a puzzle with a nice lesson (doing fast operations on ranges) that is trivially bruteforce-able

feral cliff
#

If that was interview question, that would be next q probably:)

jovial marten
#

as I stated before, we need more lanternfish

still matrix
#

I originally solved #1 without sets lol

feral cliff
#

When difficult puzzles start,?

jovial marten
#

(when does the first puzzle that makes me take >1ms in rust come)

idle locust
#

Wow, now I can doing 1d line collision detection

thin pagoda
#

🎉

still lintel
#

pandas rocks again

# Part 1
print(pd.read_csv("day_4.input", header=None).apply(lambda c: c.str.split("-")).explode([0, 1], ignore_index=1).astype(int).mask(lambda fr: pd.DataFrame([fr.index % 2] * 2).T.astype(bool), lambda fr: fr.add(1)).groupby(lambda idx: idx // 2).agg(list).applymap(lambda v: {*range(*v)}).agg(lambda r: r[0] | r[1] in (r[0], r[1]), 1).sum())

# Part 2
print(pd.read_csv("day_4.input", header=None).apply(lambda c: c.str.split("-")).applymap(lambda v: pd.Interval(*map(int, v), "both")).agg(lambda r: r[0].overlaps(r[1]), 1).sum())
hollow tulip
#
with open("4.txt") as f:
    pairs = [i.replace("\n", "") for i in f]
    pairs = [i.split(",") for i in pairs]

overlaps = 0
with open("4.txt") as f:
    pairs = [i.strip() for i in f]
    pairs = [i.split(",") for i in pairs]     # divides each pair into sub-lists e.g.([[1-2, 2-3], [2-3, 4-5]]

overlaps = 0
for i in pairs:
    l1 = [i for i in range(int(i[0].split("-")[0]), int(i[0].split("-")[1]) + 1)]  # makes 1 list for the first pair range

    l2 = [i for i in range(int(i[1].split("-")[0]), int(i[1].split("-")[1]) + 1)]  # makes other list for the second pair
    l3 = set(l1).intersection(l2)  # makes a set with only the intersecting items
    if set(l1) == l3 or set(l2) == l3:  # checks if the set == any of the two pairs...
        overlaps += 1  # ...if so, they are intersecting

print(overlaps)

What do you guys think of my code for part 1?

thin pagoda
#

l1 = [i for i in range(int(i[0].split("-")[0]), int(i[0].split("-")[1]) + 1)]
this can just be
l1 = list(range(int(i[0].split("-")[0]), int(i[0].split("-")[1]) + 1))

#

but also, I think you should just turn them into sets right away

l1 = set(range(int(i[0].split("-")[0]), int(i[0].split("-")[1]) + 1))
l2 = set(range(int(i[1].split("-")[0]), int(i[1].split("-")[1]) + 1))```
#

since you're doing that multiple times later on anway

hollow tulip
#

oh yeah, I was gonna ask how to improve those first lines

#

thank you

thin pagoda
#

I did a similar thing, so here's how I did mine

count = 0
count2 = 0
for x in re.findall(r'(\d+)-(\d+),(\d+)-(\d+)', raw):
    a, b, c, d = map(int, x)
    s1 = set(range(a, b + 1))
    s2 = set(range(c, d + 1))
    if len(s1.union(s2)) == max(len(s1), len(s2)):
        count += 1
    if len(s1 & s2) > 0:
        count2 += 1
hollow tulip
#
with open("4.txt") as f:
    pairs = [i.replace("\n", "") for i in f]
    pairs = [i.split(",") for i in pairs]

can I make this part better? I'm really confused since I learned about list comprehension just yesterday

thin pagoda
#

i.replace("\n", "") instead of this you can just do i.strip() to remove the whitespace at the beginning and end of the line

hollow tulip
thin pagoda
#
with open("4.txt") as f:
    pairs = [i.split(",") for i in f.readlines()]```
#

I think readlines() gives you each line without the newline character

hollow tulip
#

nice

#

I'll look into that

#

thank you

thin pagoda
#

np

fair lagoon
#

readlines keeps the line endings

thin pagoda
#

ah, well then you'll need to i.strip().split(',')

obsidian snow
#

lets go custom data types and loops + 1 for the win

severe heath
#

i usually go with f.read().splitlines()

agile bronze
#

I'm wondering if there's a fleshed-out Interval Class for Python

#

I don't think there is one for the standard library

#

But someone should have done one for pypi by now

pine field
#

I like to think my code is still readable. 🙂 pure golfing is more like write once, read never.

tall torrent
#

i'm surprised by the people that come up with a golfing solution in the first 8 minutes of the puzzle being released

plush ibex
#

why?

tall torrent
#

like, I assumed most people would just write a short solution and then make it shorter from there

severe heath
#

that's what they do

plush ibex
#

yea

tall torrent
#

the fact that their brains are wired to think in golf kinda scares me

#

yea but like

severe heath
#

but there are some very common tricks to make it shorter

tall torrent
#

the first 8 minutes

plush ibex
#

i could probably do these problems in 2 minutes with sloppy code

#

then use the remaining 6 to golf

#

8 mins is fast but not surprising

#

at least not for the first few days

median stirrup
#

can someoone explain me why i don't get the wright answer ? I can figure it out

leaden stratus
#

"2" > "13"

median stirrup
#

oooohh ok, mb

#

ty

pine field
median stirrup
#

oops yeah, next time

plush ibex
#

i will compile the code in my head

muted ingot
#

not too sure what's wrong here, this should be right unless I didn't fully understand the overlapping (FOR PART 2)

input = open("input.txt").read().split("\n")
newList = []
for x in input:
    z = x.split(",")
    newList.append([z[0].split("-"), z[1].split("-")])

pairsList = []
count = 0
for x in newList:
    fp1 = int(x[0][0])
    fp2 = int(x[0][1])
    sp1 = int(x[1][0])
    sp2 = int(x[1][1])

    if fp1 >= sp1 or fp2 >= sp1:
        count += 1

print(count)
plush ibex
#

3-4,1-2

hollow tulip
#

the overlapping is for any range that overlaps in any way (like 2-4, 4-5, wich overlaps in 4)

muted ingot
#

thanks for example! I was searching for a wrong answer in the correct output lists couldn't stumble on 1 thats incorrect but didn't think about that ^^

obsidian snow
#

7.3ms lets go

#

no output

topaz fiber
#

Why does this not work.... ```py
def convert_range_to_list(x: str) -> str:
x = x.split('-')
x = list(map(int, x))
x = list(range(x[0], x[1]+1))
x = ','.join(map(str, x))
return x

with open(os.path.dirname(sys.argv[0]) + "/input.txt", 'r') as f:
data = f.read()
data = data.split('\n')
data = [d.split(',') for d in data]

counter = 0
for n, d in enumerate(data):
elf1 = convert_range_to_list(d[0])
elf2 = convert_range_to_list(d[1])
if elf1 in elf2 or elf2 in elf1:
counter += 1

print(counter)

#

It gives me an answer that is too high

obsidian snow
#

Use Rust

topaz fiber
#

This is pt 1, I am creating a string for each of the tasks and then comparing with in. It works on the sample input, but it doesnt on my problem input

topaz fiber
plush ibex
#

use rust regardless

obsidian snow
#

where is the main function?

topaz fiber
#

there isnt a main function

obsidian snow
#

that's your problem

topaz fiber
#

No its not - you dont need a main function

plush ibex
#

elf1 in elf2 but 1-1 is not a subset of 11-11

obsidian snow
#

the counter should be a += 0.5

topaz fiber
#

Thats a good catch, thanks

plush ibex
#

np

topaz fiber
# plush ibex np

This is a horrible solution but it worked. Such a niche edge case, I would have spent forever looking for that.

    y = lambda x: '.' + str(x) + '.'
    x = ','.join(map(y, x))
vocal osprey
#
def parse_raw():
    with open('input.txt') as f:
        data = f.read().strip().split('\n')
        data = [pair.split(',') for pair in data]
        data = [[pair[0].split('-'), pair[1].split('-')] for pair in data]

        for idx, pair in enumerate(data):
            elf_1_start = int(pair[0][0])
            elf_1_stop = int(pair[0][1])
            elf_2_start = int(pair[1][0])
            elf_2_stop = int(pair[1][1])

            elf_1_range = range(elf_1_start, elf_1_stop + 1)
            elf_2_range = range(elf_2_start, elf_2_stop + 1)

            data[idx] = [set(elf_1_range), set(elf_2_range)]

    return data


data = parse_raw()


def part_one():
    count = 0
    for pair in data:
        if (pair[0].issubset(pair[1]) or pair[1].issubset(pair[0])):
            count += 1
    print(count)


def part_two():
    count = 0
    for pair in data:
        if (len(pair[0] & pair[1]) > 0):
            count += 1
    print(count)


part_one()
part_two()
``` I like how clean / simple the actual solutions are, but I wish I knew a less obtuse way to format the input that would still result in the same data structure
plush ibex
#

frfr look at how clean and simple my solution is

print(sum(1-(a>c<=d<b or d>b>=a<c)+1j*(a<=d>=c<=b)for a,b,c,d in[eval(l.replace(*'-,'))for l in open(0)]))```
feral cliff
#

Sets feel a bit overkill for these imo

winter gulch
#

It’s the easiest way to deal with it, no faffing around with the range boundaries, just do set operations. Obv won’t work for large inputs though

vocal osprey
feral cliff
leaden stratus
#

bruh

vocal osprey
#

coming up with the solution myself is part of the fun for me, but to each their own

#

as long as youre not trying to place on leaderboards with it i dont see the issue

plush ibex
#

for the first few days, it's not too fun to come up with solutions, since they're trivial

#

golfing is where it's at

vocal osprey
#

everyone is at different levels of experience tho 🙂 i dont consider myself that advanced, so the early days are still good practice for me

feral cliff
#

Yeah using AI for it was fun way to learn that it can solve it with 1:1 copy of puzzle instructions. Crazy

severe heath
vocal osprey
#

i know 😦

plush ibex
#

i might try ai for d5

vocal osprey
#

pretty disappointing in my opinion

vocal osprey
#

im nowhere close to being competitive enough to be on the leaderboards. but i feel bad for the people who are, yet lose out on spots due to others using AI

feral cliff
vocal osprey
#

yup ive seen all the drama on twitter / AoC subreddit

#

like yeah its cool that it can solve it in 16 seconds, its very impressive. but its lame to take away spots from humans actually trying to compete

feral cliff
plush ibex
#

lame but there's almost nothing that can be done to enforce it

vocal osprey
# feral cliff Huge progress on one end.

agreed. it is cool and very impressive that these language models are so advanced they can solve it in 16 seconds. i just feel bad for the people trying to compete to get on the leaderboards

plush ibex
#

they'll get their chance in later days

feral cliff
vocal osprey
#

just my 2 cents, everyone is entitled to their own opinion 🙂

feral cliff
#

Solving is instant i would guess

plush ibex
#

no

feral cliff
#

Yeah probably

#

Maybe rules should prevent it. Sure making sure it doesn't happen is different story

jovial marten
obsidian snow
#

assembly

jovial marten
#

then 7.3ms feels kinda slow pithink

plush ibex
#

probably inefficient code

feral cliff
#

Depends on the machine:)

plush ibex
#

true

leaden stratus
#

hard to imagine a >1000x increase based on hardware alone though

jovial marten
#

I have <20μs including file read and parsing

burnt wadi
#

gameboy color

#

lets go

feral cliff
#

Unless it's Atarii 1200 😆

plush ibex
burnt wadi
#

do they have an actual clock?

tulip ravenBOT
#

Hey @obsidian snow!

You either uploaded a .txt file or entered a message that was too long. Please use our paste bin instead.

plush ibex
#

idk if this counts

obsidian snow
sleek python
#

can you not use the pastebin?

obsidian snow
obsidian snow
jovial marten
#

I guess you miss out on compiler optimizations

plush ibex
#

ye

#

like i said, inefficient code

sleek python
#

I feel like at this point a compiled Nim or C version would be faster

#

because of the optimizations

plush ibex
#

man's getting skill diffed by compilers 😔

sleek python
#

Maybe you could use the -S flag and look at the output assembly by the compiler

#

and find some good tips

#

*it might be another flag I forget which

jovial marten
#

though maybe the approach is also different

sleek python
#

If you really want to be extreme you could embed the file input in the data of your assembly

#

Or use extreme parallelization on the GPU

#

this seems like something that could be parallelized

jovial marten
#

can you even send data to the gpu in <20μs?

sleek python
leaden stratus
#

m1 mac? 👀

plush ibex
sleek python
#

It might be possibly to use some sort of async file access

plush ibex
#

😎

sleek python
#

where you are reading and operating at the same time

#

I know it's possible in C (I think it's in the Boost library) and you could get ~2700 more operations while reading the file

#

boost::asio::async_read(s, boost::asio::buffer(data, size), handler);

jovial marten
#

or just accept that GPU isn't going to be helpful for this tiny size

#

simd is a better bet for speedups

sleek python
#

The GPU + Parallelization will be slower for tiny input but will speed up significantly for a ridiculously large input I would guess

jovial marten
#

this is AoC

sleek python
#

but because we have a small input it's probably better not to use GPU

jovial marten
#

no input is that huge

topaz fiber
#

Is there a neater way of doing this kind of assignment in python:

    if len_range(a) < len_range(b):
        sub_list = a
        mainlist = b
    else:
        sub_list = b
        mainlist = a
sleek python
#
sub_list, mainlist = a, b
if len_range(a) < len_range(b):
  mainlist, sub_list = sub_list, main_list
#

is one way that is shorter

#

not sure about neater

#

I would at least use the comma assignment though

#

because it is neater

#
        sub_list = a
        mainlist = b

becomes

sub_list, mainlist = a, b
leaden stratus
#

imo that's much more annoying to read

topaz fiber
sleek python
#

we don't need to golf his solution lol

topaz fiber
#

wtf is golf? 😂

sleek python
#

mainlist,sub_list=((b,a),(a,b))[len_range(a)<len_range(b)]

sleek python
jovial marten
#

if you don't mind keeping the old names, then

if ...:
  a, b = b, a
gloomy comet
#

How about a weirder way 🥴

main_list = max(a, b, key=len_range)
sub_list = min(a, b, key=len_range)
jovial marten
#

if not in expected order, swap

topaz fiber
#

ahh I like the min max 😝

gloomy comet
#

true

sleek python
# topaz fiber wtf is golf? 😂

like yesterday we golfed the solution to:

# LF (144)
*z,=open(0,'rb');u=sum;print(u((u({*a[(l:=len(a)//2):]}&{*a[:l]})-96)%58for a in z),u((u({*b}&{*c}&{*d})-106)%58for b,c,d in zip(*3*[iter(z)])))
# CR (144)
*z,=open(0,'rb');u=sum;print(u((u({*a[(l:=len(a)//2):]}&{*a[:l]})-96)%58for a in z),u((u({*b}&{*c}&{*d})-109)%58for b,c,d in zip(*3*[iter(z)])))
# CRLF (146)
*z,=open(0,'rb');u=sum;print(u((u({*a[(l:=len(a)//2-1):]}&{*a[:l]})-96)%58for a in z),u((u({*b}&{*c}&{*d})-119)%58for b,c,d in zip(*3*[iter(z)])))
# xplatform (156)
z=open(0,'rb').read().split();u=sum;print(u((u({*a[(l:=len(a)//2):]}&{*a[:l]})-96)%58for a in z),u((u({*b}&{*c}&{*d})-96)%58for b,c,d in zip(*3*[iter(z)])))
# xplatform one loop (161)
u=sum;print(u(u((u({*a[(l:=len(a)//2):]}&{*a[:l]})-96)%58for a in(b,c,d))+(u({*b}&{*c}&{*d})-96)%58*1j for b,c,d in zip(*3*[iter(open(0,'rb').read().split())])))
# encoded
# LF (97)
exec(bytes("稪㴬灯湥〨✬扲⤧画猽浵瀻楲瑮用⠨⡵⩻孡氨㴺敬⡮⥡⼯⤲崺♽⩻孡氺絝⴩㘹┩㠵潦⁲⁡湩稠Ⱙ⡵用笨截♽⩻絣符搪⥽ㄭ㘰┩㠵潦⁲ⱢⱣ⁤湩稠灩⨨⨳楛整⡲⥺⥝⤩",'u16')[2:])
# CR (97)
exec(bytes("稪㴬灯湥〨✬扲⤧画猽浵瀻楲瑮用⠨⡵⩻孡氨㴺敬⡮⥡⼯⤲崺♽⩻孡氺絝⴩㘹┩㠵潦⁲⁡湩稠Ⱙ⡵用笨截♽⩻絣符搪⥽ㄭ㤰┩㠵潦⁲ⱢⱣ⁤湩稠灩⨨⨳楛整⡲⥺⥝⤩",'u16')[2:])
# CRLF (98)
exec(bytes('稪㴬灯湥〨✬扲⤧画猽浵瀻楲瑮用⠨⡵⩻孡氨㴺敬⡮⥡⼯ⴲ⤱崺♽⩻孡氺絝⴩㘹┩㠵潦⁲⁡湩稠Ⱙ⡵用笨截♽⩻絣符搪⥽ㄭ㤱┩㠵潦⁲ⱢⱣ⁤湩稠灩⨨⨳楛整⡲⥺⥝⤩','u16')[2:])
# xplatform (103)
exec(bytes("㵺灯湥〨✬扲⤧爮慥⡤⸩灳楬⡴㬩㵵畳㭭牰湩⡴⡵用笨愪⡛㩬氽湥愨⼩㈯㨩絝符愪㩛嵬⥽㤭⤶㔥昸牯愠椠⥺甬⠨⡵⩻絢符挪♽⩻絤⴩㘹┩㠵潦⁲ⱢⱣ⁤湩稠灩⨨⨳楛整⡲⥺⥝⤩",'u16')[2:])
# xplatform one loop (106)
exec(bytes("㵵畳㭭牰湩⡴⡵⡵用笨愪⡛㩬氽湥愨⼩㈯㨩絝符愪㩛嵬⥽㤭⤶㔥昸牯愠椠⡮ⱢⱣ⥤⬩用笨截♽⩻絣符搪⥽㤭⤶㔥⨸樱映牯戠挬搬椠楺⡰㌪嬪瑩牥漨数⡮ⰰ爧❢⸩敲摡⤨献汰瑩⤨崩⤩
",'u16')[2:])
gloomy comet
#

peak python

#

just like Guido intended

sleek python
#

exactly

feral cliff
topaz fiber
#

Oh dear god

jovial marten
#

this is why we should measure bytes, not characters

topaz fiber
sleek python
feral cliff
#

Still need to assign main sub not?

jovial marten
topaz fiber
#

Ah thats true

gloomy comet
#

then do min(b, a) just in case

rotund forge
severe heath
rotund forge
#

Obviously, the code should include everything it needs to run, don't open another file or fetch from the internet and exec that

sleek python
#

Something like:

# xplatform one loop (106)
exec(bytes("㵵畳㭭牰湩⡴⡵⡵用笨愪⡛㩬氽湥愨⼩㈯㨩絝符愪㩛嵬⥽㤭⤶㔥昸牯愠椠⡮ⱢⱣ⥤⬩用笨截♽⩻絣符搪⥽㤭⤶㔥⨸樱映牯戠挬搬椠楺⡰㌪嬪瑩牥漨数⡮ⰰ爧❢⸩敲摡⤨献汰瑩⤨崩⤩
",'u16')[2:])

Kind of ruins the point tbh

rotund forge
#

You can replace exec with eval in that if the original was a single print call or whatever and it will work. Doesn't make sense to differentiate between the two when it comes to code golf really

sleek python
#

good point

#

but really you could just do

sleek python
#

technically

jovial marten
#

that's why we shouldn't measure characters

sleek python
#

"Optimal" solution for every problem ever:

print(eval(input()))
# and
exec(input())
rotund forge
#

When "input can be given in any convenient format"

sleek python
#

I mean you can put any valid code and it will work fine

#

the only things that are shorter than that

#

are like

#

print(5)

#

or something small

#
exec(input())
print(123456)
#

you can print a maximum of a 5 character expression

#

before beating that

normal obsidian
#

Love to see situations where part 2 require 3 more lines of code

def puzzle():
    crossovers = 0
    anyCrossOver = 0
    for pair in pairs:
        order1 = set(range(pair[0][0], pair[0][1] + 1))
        order2 = set(range(pair[1][0], pair[1][1] + 1))
        if order1.intersection(order2) == order1 or order1.intersection(order2) == order2:
            crossovers += 1
        elif not order1.isdisjoint(order2):
            anyCrossOver += 1
    return crossovers + anyCrossOver


with open('input.txt', 'r') as file:
    data = file.read().split("\n")

pairs = [[[int(i) for i in hyphen.split('-')] for hyphen in row.split(',')] for row in data]

puzzle()
supple creek
#

finished!

#

I got to use sets for this too

#
def part_1(ranges: list[Iterator[int]]) -> int:
    r = [
        i
        for i in ranges
        if (a := set(range(next(i), next(i) + 1))).issubset(
            b := set(range(next(i), next(i) + 1))
        )
        or b.issubset(a)
    ]

    return len(r)


def part_2(ranges: list[Iterator[int]]) -> int:
    r = [
        i
        for i in ranges
        if set(range(next(i), next(i) + 1)) & set(range(next(i), next(i) + 1))
    ]

    return len(r)
``` was my solution
#

hmm, the second thing for an intersection doesn't have to be a set, does it

#

ah, yeah, if you use .intersection you can pass any iterable

#

eh whatever, & is cleaner

#

where ranges was ranges = [map(abs, extract_ints(l)) for l in raw_input]

jovial marten
#

O(😔)

severe heath
#

i'm surprised y'all actually constructed ranges

supple creek
#

What the fuck

severe heath
#

i did at first when i thought you could do range in range

supple creek
#

my pc crashed when I tried to create two huge sets for an intersection with timeit

severe heath
#

but bounds checking is so much easier

supple creek
jovial marten
supple creek
#

but muh sets

leaden stratus
#

new feature for 3.12, range.intersect

supple creek
#

PRing immediately

sleek python
#

but they would probably just say convert to set

severe heath
#

lmfao i was expecting someone to pr Range::overlaps and Range::covers to rust

#

surprised it hasn't happened yet

leaden stratus
#

turning a O(1) time and memory to O(n) time and meemory

sleek python
#

I suppose so

supple creek
#

Is intersection O(1)?

severe heath
#

ranges can do trivial bounds checking, they don't calculate everything beforehand

sleek python
#

Yes

supple creek
#

I see

sleek python
#

it is just a bunch of comparisons

#

at the lowest level

jovial marten
#

intersect for general ranges is...non-trivial

#

because step sizes

leaden stratus
#

as i found out quite recently in #algos-and-data-structs !

idle locust
#

I'm a JS user :)

severe heath
#

oh yes, steps exist

sleek python
severe heath
#

completely forgot about those

sleek python
#

same step sizes wouldn't change anything

jovial marten
#

at least intersect for general ranges would still be a range

#

(afaik)

#

err...I guess negative steps makes the direction weird

supple creek
#

just error on steps 😤

jovial marten
#

what's the intersection between range(10) and range(9, -1, -1)?

leaden stratus
#

you could just default to a >0 step

sleek python
#

!e ```py
a = set(range(10))
b = set(range(9, -1, -1))
print(a)
print(b)
print(a&b)

leaden stratus
#

:O sets are ordered??? /j

tulip ravenBOT
#

@sleek python :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
002 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
003 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
burnt wadi
#

no lol

sleek python
#

Replace the two ranges to test what the intersection should be

leaden stratus
#

now do range(10000000000000000)

sleek python
#

!e ```py
a = set(range(100000000))
b = set(range(9, -1, -1))
print(a)
print(b)
print(a&b)

tulip ravenBOT
#

@sleek python :warning: Your 3.11 eval job timed out or ran out of memory.

[No output]
supple creek
#

Skill issue

sleek python
#

yeah that's unsuprising

jovial marten
sleek python
jovial marten
#

which is possible (but possibly ill defined)

#

a set isn't a range

sleek python
#

it would be equivalent to the set() of the range

#

I think

#

but I haven't tested like anything so I'm probably wrong

jovial marten
#

range(1, 8) & range(2, 100, 2) could be range(2, 8, 2)

#

and it would make sense

sleek python
#

what would the logic be though for every case

idle locust
#

random shit

jovial marten
#

you can solve it

burnt wadi
#

yes, the step might be least common multiple

jovial marten
#

the answer is a range

sleek python
jovial marten
#

gcd is what? log(n)?

sleek python
#

alright

#

Yeah I'm pretty sure its log(n)

#

Worst case is

#
log10(min(x,y))
#

where x, y are the inputs

#

for the most optimized algorithm

leaden stratus
#

!d math.gcd

jovial marten
#

another example

a = range(1, 100, 3)
b = range(0, 100, 5)
a&b could be range(10, 15, 100)
burnt wadi
#

hm. what is gcd good for in this case? PES_Think

sleek python
#

solving a "linear diophantine equation"

jovial marten
#

In mathematics, a Diophantine equation is an equation, typically a polynomial equation in two or more unknowns with integer coefficients, such that the only solutions of interest are the integer ones. A linear Diophantine equation equates to a constant the sum of two or more monomials, each of degree one. An exponential Diophantine equation is ...

sleek python
#

I'm unfamilar with the term "diophantine" at all but I'm gonna trust fiery on this one

jovial marten
#

a diophantine equation is just an equation with only integers allowed

#

this is a particularly easy one

sleek python
#

oh this seems way easier then

#

we would have to write a solution in C and add it to the range class

jovial marten
#

but as mentioned, it's a bit ill-defined

sleek python
#

I'll see if I can find the source for range

jovial marten
#

e.g. for a mix of negative and positive step sizes

sleek python
#

yeah

#

we could raise ValueError

ember junco
#

Thinking of raise...

#

!e raise raise raise

tulip ravenBOT
#

@ember junco :x: Your 3.11 eval job has completed with return code 1.

001 |   File "<string>", line 1
002 |     raise raise raise
003 |           ^^^^^
004 | SyntaxError: invalid syntax
ember junco
#

Understandable

#

Sorry sir

sleek python
#

found the definition

#

of PyRange_Type

odd frost
#

A little late to the party because I wasn't there this wk:

def all_overlapping(parts):
    first = list(map(int, parts[0].split("-")))
    second = list(map(int, parts[1].split("-")))
    return (first[0] <= second[0] and first[-1] >= second[-1]) \
            or (second[0] <= first[0] and second[-1] >= first[-1])

def some_overlapping(parts):
    first = list(map(int, parts[0].split("-")))
    second = list(map(int, parts[1].split("-")))
    return (first[0] <= second[0] and first[1] >= second[0]) or \
            (second[0] <= first[0] and second[1] >= first[0]) 

def part1(content):
    return sum([all_overlapping(line.split(",")) for line in content.split("\n")])

def part2(content):
    return sum([some_overlapping(line.split(",")) for line in content.split("\n")])

content = read_file("input.txt").strip()
print(part2())
inland leaf
#

with open('day4\input.txt', 'r') as f:
    inputlist = f.read().split('\n')

total = 0

def formula(x):
    return {num for num in range(int(x[0]), int(x[1]) + 1)}

for line in inputlist:
    a, b = [half.split('-') for half in line.split(',')]
    seta, setb = formula(a), formula(b)
    if seta.intersection(setb) != set():
        total += 1
    
print(total)```
Solution for part 2, lmk if you spot any obvious optimizations that can be made!
gloomy comet
#

also technically if seta.intersection(setb): is enough, but maybe you were trying to be verbose

inland leaf
#

Ah, I was not :P because python treats empty stuff as false?

gloomy comet
#

if you're looking for optimization, then comparing the boundaries will be more efficient than generating the entire set for each pair of integers

inland leaf
inland leaf
gloomy comet
#

range is just a function (technically a class), it can be used anywhere

inland leaf
#

Also question, why can't I replace the line

if seta.intersection(setb) != set():``` 
with
```py
if seta.isdisjoint(setb):```
It seems like they should be the same thing
gloomy comet
#

you want to know if they're not disjoint

inland leaf
#

ah, yeah I thought it might be something like that, how would I write that?

gloomy comet
#

if not seta...

inland leaf
#

tysm

plush ibex
#

ig i don't get a pin 😦

pine field
#
seta.intersection(setb)
pine field
pine field
gloomy comet
pine field
surreal wigeon
#

and for those people scale doesn't pose that much of an issue

pine field
#

I'll be honest, I was also surprised to see so many people using sets instead of just comparing the end points.

deft flame
#

And here I thought I was cool because I thought outside the box and came up with this cool idea for sets...

pine field
#

sets are cool. I love sets. didn't use them this time. =/

severe heath
#

for some reason comparisons always mess me up

ripe kraken
#

Laughably unoptimised this time, but I was lazy lol

def day_4():
    def part_one():
        return len([1 for pair in DATA.splitlines() if all([n in range(int(pair.split(",")[1].split("-")[0]), int(pair.split(",")[1].split("-")[1]) + 1) for n in range(int(pair.split(",")[0].split("-")[0]), int(pair.split(",")[0].split("-")[1]) + 1)]) or all([n in range(int(pair.split(",")[0].split("-")[0]), int(pair.split(",")[0].split("-")[1]) + 1) for n in range(int(pair.split(",")[1].split("-")[0]), int(pair.split(",")[1].split("-")[1]) + 1)])])
    def part_two():
        return len([1 for pair in DATA.splitlines() if any([n in range(int(pair.split(",")[1].split("-")[0]), int(pair.split(",")[1].split("-")[1]) + 1) for n in range(int(pair.split(",")[0].split("-")[0]), int(pair.split(",")[0].split("-")[1]) + 1)]) or any([n in range(int(pair.split(",")[0].split("-")[0]), int(pair.split(",")[0].split("-")[1]) + 1) for n in range(int(pair.split(",")[1].split("-")[0]), int(pair.split(",")[1].split("-")[1]) + 1)])])
    return part_one()
severe heath
#

i got the logic right yesterday in my submission, but it took me about 15 minutes to mentally work through it when helping someone this morning

pine swan
ripe kraken
pine swan
surreal wigeon
#

uh

#

:= is your friend

wet wagon
#

i used all and any for the first time

frank sleet
#

You haven't until this point? @wet wagon

#

For some reason, that surprises me

wet wagon
#

I've never had to use it @frank sleet

frank sleet
#

no kidding

supple creek
#

I used sets 🎉

#

who needs to scale anyways

plush ibex
#

me

#

😔

normal obsidian
#

Agreed

quiet crypt
#

Here's mine for part one

# Checks if any range fully contains the other
def fully_contains(range_one, range_two):
    one_fully_contains_two = range_one[0] <= range_two[0] and range_one[1] >= range_two[1]
    two_fully_contains_one = range_two[0] <= range_one[0] and range_two[1] >= range_one[1]

    return one_fully_contains_two or two_fully_contains_one

def main():
    # Opens input file in read mode
    with open("problem_4_input.txt", "r") as input_file:
        # Number of ranges that fully contains the other
        num_fully_contains = 0

        for line in input_file.readlines():
            # Split line into two ranges
            line = line.rstrip("\n")
            ranges = line.split(",")
            # Split both ranges into two integer strings
            range_one = ranges[0].split("-")
            range_two = ranges[1].split("-")
            # List versions of both ranges
            range_one_list = [int(range_one[0]), int(range_one[1])]
            range_two_list = [int(range_two[0]), int(range_two[1])]
            # If either range fully contains the other, increase number of ranges that fully contains the other
            if fully_contains(range_one_list, range_two_list):
                num_fully_contains += 1

        # Print result
        print(num_fully_contains)

if __name__ == "__main__":
    main()
#

Tips and suggestions are welcome

ocean elm
#

yahoo

#

wait wrong day

wet wagon
#
with open("the.txt","r") as f:
    ko = f.read().split("\n")
    count = 0
    for i in ko:
        k, l = i.split(",")
        do,xo = k.split("-")
        fo,go = l.split("-")
        do = int(do)
        xo = int(xo)
        fo = int(fo)
        go = int(go)
        if do >= fo and xo >= go:
            count += 1
        elif fo >= do and go >= xo:
            count += 1
    print(count)```
#

What's wrong exactly?

tall torrent
wet wagon
#

I don't think I really understand part 2

tall torrent
tall torrent
wet wagon
#

Ah

snow olive
#

cleaned up my solution a bit

def part1(section_ids):
    set_a = set(range(section_ids[0], section_ids[1] + 1))
    set_b = set(range(section_ids[2], section_ids[3] + 1))
    smallest_set = set_a if len(set_a) < len(set_b) else set_b
    return set_a & set_b == smallest_set


def part2(section_ids):
    set_a = set(range(section_ids[0], section_ids[1] + 1))
    set_b = set(range(section_ids[2], section_ids[3] + 1))
    return not len(set_a & set_b) == 0


with open("../inputs/day4.txt") as file:
    sections = list(map(lambda s: list(map(int, s)),
                        [ids.replace('-', ',').split(',') for ids in file.read().splitlines()]))

print("Part 1:", len(list(filter(part1, sections))))
print("Part 2:", len(list(filter(part2, sections))))
wet wagon
#

can someone explain what i am doing wrong?

def range_to_string(lower, upper):
    return "".join([str(x) for x in range(int(lower), int(upper) + 1)])


def main() -> int:
    contains_count = 0

    with open("input.txt", "r", encoding="UTF-8") as f:
        for line in f:
            line = line.strip("\n")

            e1_range, e2_range = line.split(",")

            e1_lower, e1_upper = e1_range.split("-")
            e2_lower, e2_upper = e2_range.split("-")

            elf_1 = range_to_string(e1_lower, e1_upper)
            elf_2 = range_to_string(e2_lower, e2_upper)

            if (elf_1 in elf_2) or (elf_2 in elf_1):
                contains_count += 1

    print(contains_count)

    return 0
#

this outputs 2 for the example they gave but when i try it on my own puzzle input it is wrong apparently

fair yacht
#
d4a = sum(a<=c<=d<=b or c<=a<=b<=d for a,b,c,d in (map(int, line.replace(",","-").split("-")) for line in in4a.splitlines()))

My single-liner :3

fair yacht
wet wagon
fair yacht
#

Okay, second part finished:

d4a = sum(a<=c<=d<=b or c<=a<=b<=d for a,b,c,d in (map(int, line.replace(",","-").split("-")) for line in in4a.splitlines()))

d4b = sum((min(b,d)+1-max(a,c))>0 for a,b,c,d in (map(int, line.replace(",","-").split("-")) for line in in4a.splitlines()))
empty river
#
with open('advent4.txt', 'r') as txtfile:
    from re import split
    txtlst = [list(map(int, split(r',|-', line.strip()))) for line in txtfile]
    #print(txtlst)

This is how I'm parsing. So I didn't use the read() or readline() but it still works? Is there a reason to use read() over looping. Please don't comment about the parsing method yet I'm going to try to do it my way first

winter gulch
empty river
#

I guess read() is what you mean

#

and readline() same as looping?

#

what I did

#

*iterating

winter gulch
winter gulch
empty river
#

Gotcha gotcha ty

empty river
#
def parttwo(loloi):
    counter = 0
    for item in loloi:
        rge1, rge2 = set(range(item[0], item[1] + 1)), set(range(item[2], item[3] + 1))
        #if not rge1 - rge2 or not rge2 - rge1:
        if rge2 & rge1:
            counter += 1
    return counter

with open('advent4.txt', 'r') as txtfile:
    from re import split
    txtlst = [list(map(int, split(r',|-', line.strip()))) for line in txtfile]```
#

This is my incorrect solution for parttwo. I would like to be given a hint.

#

It seems to me a simple intersection should do the trick.

EDIT: input was wrong. Solution works

winter gulch
empty river
#

I got the same answer of 4 as here

#

I'm thinking there must be an edge case I'm missing.

minor grove
empty river
#

Let me double-check my input file.

minor grove
#

yeah, maybe just redownload it

empty river
minor grove
#

💀

empty river
#

Thank you for testing in your input

solid quarry
#

solution (not python)```ruby

aoc problem 4

class Range
def overlap?(other)
self.end >= other.begin or self.begin > other.begin and self.begin <= other.end
end
end

def input
File.read('4.txt').lines.map do |l|
l.split(/,/).map do |r|
start, stop = r.split(/-/)
start.to_i..stop.to_i
end
end
end

return unless FILE == $0

part 1 solution

puts input.count {|r0, r1| r0.cover? r1 or r1.cover? r0}

part 2 solution

puts input.count {|r0, r1| r0.overlap? r1}

solid quarry
#

ok now yes python ```py

aoc problem 4

def getinput():
acc = []
with open('4.txt') as f:
for l in f.read().splitlines():
(r0start, r0stop), (r1start, r1stop) = [r.split('-') for r in l.split(',')]
acc.append((set(range(int(r0start), int(r0stop) + 1)),
set(range(int(r1start), int(r1stop) + 1))))

return acc

def full_contains(rangepairs):
acc = 0
for r0, r1 in rangepairs:
if (r0 | r1) == r0 or (r0 | r1) == r1:
acc += 1
return acc

def overlaps(rangepairs):
acc = 0
for r0, r1 in rangepairs:
if (r0 & r1) != set():
acc += 1
return acc

def main():
inpt = getinput()
print(full_contains(inpt)) # part 1 solution
print(overlaps(inpt)) # part 2 solution

if name == 'main':
main()

#

sets are cool

plush ibex
#

yeah that's rb

solid quarry
candid fable
#
input_file = "4.txt"

with open(input_file) as f:
    data = f.read().split("\n")

score = 0
for line in data:
    one, two = line.split(",")
    one_range = one.split("-")
    two_range = two.split("-")
    one_range = list(range(int(one_range[0]), int(one_range[1])+1))
    two_range = list(range(int(two_range[0]), int(two_range[1])+1))
 
    if any(x in two_range for x in one_range) or any(x in one_range for x in two_range):
        score += 1
print(score)
# for part 1, just change the `any` functions to `all`
wet wagon
#

I keep getting a wrong answer for the part 1 (my input is fine). It works for the test values. Could someone help me out? Thanks

f = open("inputday4.txt",'r')
filecontents=f.read().replace('\n',',').replace('-',',').split(',')
filecontents = [filecontents[x:x+4] for x in range(0,len(filecontents),4)]


assignments1 = []
assignments2 = []

for x in filecontents:
    assignments1.append([z for z in range (int(x[0]),int(x[1])+1)])
for x in filecontents:
    assignments2.append([z for z in range (int(x[2]),int(x[3])+1)])

assignments1 = [','.join([str(z) for z in x])for x in assignments1]
assignments2 = [','.join([str(z) for z in x])for x in assignments2]

assignments_containing_the_other = 0

for x in range(len(assignments1)):
    if assignments1[x] in assignments2[x] or assignments2[x] in assignments1[x]:
        assignments_containing_the_other+=1

print(assignments_containing_the_other)

f.close()
kind niche
dreamy kestrel
#

hello

#

i am facing some awkward issue with this problem

#
8
2 6 4 8
True False False True
2 4 3 5
True False False True
5 7 7 9
True False False True
2 3 8 7
True True False False
['2', '8'] ['3', '7']
6 4 6 6
False True True True
['6', '6'] ['4', '6']
2 4 6 8
True False False True
5 5 5 5
True True True True
['5', '5'] ['5', '5']
13 8 98 13
True True False False
['13', '98'] ['8', '13']
4
#

here, my condition was

#

so you see for the last experimental input which is 13-98,8-13
the program shouldn't count this pair as it doesn't fulfill the condition

#

but the program somehow counts it

#

here in this part s_point_1st_elf <= s_point_2nd_elf s_point_1st_elf is 13 and s_point_2nd_elf is 8, so 13 <=8 should have been False but when printed the result for this input it is True

#
13 8 98 13
True True False False
['13', '98'] ['8', '13']
#

x1 = 13 and x2 = 8
y1 = 98 and y2 = 13
so for the first condition
13 <= 8 is False and 98 >= 13 is True which results False so it shouldn't count
as for the second condition
8 <= 13 is True and 13 >= 98 is False which also results False and won't count

so this pair shouldn't count at all
but how come my program is counting it?
the worst thing is i am not seeing any wrong with my program but if we read the program it shouldn't count this pair but it does anyway

#
#Advent of code
#Problem 4
#--- Day 4: Camp Cleanup ---

with open("day4exp.txt") as f:
    inp = f.read()

li1 = [list(map(str, i.split("\n"))) for i in inp.strip().split("\n\n")]

li2 = []
for i in li1[0]:
    str1 = i.split(",")
    li2.append(str1)

cnt = 0

for j in li2:
    temp_li1 = j[0].split("-")
    temp_li2 = j[1].split("-")
    x1 = temp_li1[0]
    y1 = temp_li1[1]
    x2 = temp_li2[0]
    y2 = temp_li2[1] 

    if (x1 <= x2) and (y1 >= y2):
        cnt += 1
    elif (x2 <= x1) and (y2 >= y1):
        cnt +=1

print(cnt)
#

here's my program and idk why it doesn't work

#

it works on the example input

#

but like i said this program count some pair which it shouldn't count

#

that's why i get wrong answer in the part 1

kind niche
#

You're probably doing string comparison here

#

Which isn't right

dreamy kestrel
#

ahh damn! okay let me try after converting them to integer

dreamy kestrel
kind niche
#

Happens to the best of us 😅

#

It's easier to find certain bugs when you have a fresh pair of eyes

dreamy kestrel
#

my brain is not working today at all lol

kind niche
#

You've probably just been looking at it too long, and think it does what you want, not what it says

#

Been there done that

dreamy kestrel
#

yeah sorry! learned my lesson today lemon_sweat