| Bytes | Lang | Time | Link |
|---|---|---|---|
| 070 | Ruby n | 250525T053454Z | Value In |
| 098 | Scala 3 | 250525T024240Z | Ahamad |
| 012 | Uiua | 241229T164308Z | lolad |
| 016 | Japt | 241213T123926Z | Shaggy |
| 080 | Python 3 + SymPy | 241213T074721Z | tsh |
| 050 | R | 241214T040527Z | Eonema |
| 078 | Python3 | 241214T010145Z | Ajax1234 |
| 017 | 05AB1E | 241213T080739Z | Kevin Cr |
| 024 | Charcoal | 241213T091100Z | Neil |
| 050 | Retina 0.8.2 | 241213T063214Z | Neil |
Ruby -n, 72 70 bytes
Input is taken from stdin.
a,b,x,y=$_.scan(/\d+/).map &:to_i
b+=x-a
p b<y||b==y&&!/\[.*\(|\].*\)/
Scala 3, 98 bytes
Golfed version. Attempt This Online!
(s,e,l,r,t,f,m,q)=>{1>t-s-f+e&&(if(l)1 else 0)<=(if(m)1 else 0)&&(if(r)1 else 0)<=(if(q)1 else 0)}
Ungolfed version. Attempt This Online!
object Main {
// Equivalent of the lambda function f
def f(s: Int, e: Int, l: Boolean, r: Boolean, s2: Int, e2: Int, l2: Boolean, r2: Boolean): Boolean = {
1 > s2 - s - e2 + e && (if (l) 1 else 0) <= (if (l2) 1 else 0) && (if (r) 1 else 0) <= (if (r2) 1 else 0)
}
// Parse interval string like "[1,2]" or "(1,3)" into (start, end, leftInclusive, rightInclusive)
def parse(s: String): (Int, Int, Boolean, Boolean) = {
val content = s.substring(1, s.length - 1)
val parts = content.split(",").map(_.trim.toInt)
val leftInclusive = s.charAt(0) == '['
val rightInclusive = s.charAt(s.length - 1) == ']'
(parts(0), parts(1), leftInclusive, rightInclusive)
}
def main(args: Array[String]): Unit = {
val testcases = """[1,2], [1,3] => true
[1,2], (1,3) => true (d can't be integer but it's fine)
[1,2], [8,9] => true
[8,9), [1,2) => true
(1,9), (1,9) => true
[1,4], [3,5] => false
(8,9], [1,2) => false (rotating is not allowed)
(7,9), [1,2] => false
[1,3], (1,3) => false
[1,2], (1,2) => false
[1,2], [1,2) => false
[1,2], (1,2] => false
[1,2], [1,2] => true
[1,2), [1,2) => true
[1,2), (1,2] => false
[1,2), (1,2) => false
(1,2], (1,2] => true
(1,2], [1,2) => false
(1,2], [1,2] => true
(1,2], (1,2) => false
(1,2), (1,2) => true
(1,2), [1,2) => true
(1,2), (1,2] => true
(1,2), [1,2] => true""".split("\n")
for (testcase <- testcases) {
val parts = testcase.split(", ")
val a = parts(0)
val rest = parts(1).split(" ")
val b = rest(0)
val expected = rest(2) == "true"
val (s, e, l, r) = parse(a)
val (s2, e2, l2, r2) = parse(b)
val result = f(s, e, l, r, s2, e2, l2, r2)
println(s"$result $expected")
}
}
}
Uiua, 14 12 bytes
+×⊓×⊃≍>⊓∩=∩-
Uses input format al bl ar br a1 b1 a2 b2, where a1 and a2 are the endpoints of a and al and ar are 0 for a closed intervals, and 1 for an open intervals e.g. [2,3) [4,5) gives 0 0 1 1 2 3 4 5 (if this doesn't fall under "flexible", let me know).
Explanation
\$a\$ fits into \$b\$ iff \$|a|<|b|\$ or \$|a|=|b|\$ and the endpoints are the same openness, which is implemented as so:
+×⊓×⊃≍>⊓∩=∩- # calculate interval fit
⊓ ∩- # subtract each interval
⊓∩= # for each side (left and right), compare openness
+ # intervals fit if either...
× # both:
⊓× # both sides have same openness
⊓ ⊃≍ # and intervals are the same size
⊓ ⊃ > # ...or |b| > |a|
Japt, 16 bytes
+2 bytes because I think I misinterpreted the flexibility of the input.
cVon)r- §WínX r|
Takes input as 4 arrays, with B being the first (U), A the second (V), and the 3rd (W) & 4th (X) representing whether each end of B & A, respectively, is inclusive (1) or exclusive (0).
cVon)r- §WínX r| :Implicit input of arrays U, V, W & X
c :Concatenate to U
Vo : Modify last element of V
n : Negate
) :End concat
r- :Reduce by subtraction
§ :Less than or equal to
Wí X : Interleave W & X
n : Reducing each pair by inverse subtraction
r| : Reduce by bitwise OR
Python 3 + SymPy, 80 bytes
lambda a,b:a.measure<b.measure+a.xreplace({a.inf:b.inf,a.sup:b.sup}).issubset(b)
Input two sympy.set.set.Interval, output bool.
Python 3, 41 bytes
lambda s,e,l,r,S,E,L,R:1>S-s-E+e<=l-L|r-R
Input 8 parameters as (start_a: int, end_a: int, left_open_a: bool, right_open_a: bool, start_b: int, end_b: int, left_open_b: bool, right_open_b: bool)...
-3 bytes by Albert.Lang
R, 52 51 50 bytes
-2 by @pajonk:
Simplified logic: (¬A)&B = ¬(A|B)
Renamed
diffto?
\(x,i,`?`=diff,d=?x)d[2]>d[1]|!any(d[2]-d[1],0>?i)
Attempt This Online! Input:
xis a 2x2 matrix with one column for each intervaliis a 2x2 logical matrix for whether the corresponding bound inxis closed
R's operator precedence just happens to be such that I didn't need any extra parentheses (with weird things like - is before !). Also handy is that diff(t(x))[2]-diff(t(x))[1] == diff(x)[2]-diff(x)[1] for a 2x2 matrix.
Python3, 78 bytes
lambda a,A,b,B,c,C,d,D:(A==B and C==D)+(A==C or B==D)and(b-a,-A-B)<=(d-c,-C-D)
05AB1E, 17 bytes
ÆD`‹sËIËI`ßs_~~*~
Two separated inputs: the first is a pair of reversed pairs of integers. And the second is a pair of pairs of bits (in either order), where 1 indicates inclusive and 0 indicates exclusive (e.g. (8,9], [1,2) would have inputs [[9,8],[2,1]] and ["01","10"]/["10","01"]).
Try it online or verify all test cases.
Inspired by @Neil's Retina answer, so make sure to upvote that answer as well!
Explanation:
In pseudo-code:
- Get the amount of items in both ranges if we would parse it as a regular
[a,b)-range (inclusive start, exclusive end). - The result is now truthy if any of these four options is truthy:
- The amount of items of the second range is larger than the first
- OR the amount of items are the same, and the inclusiveness/exclusiveness of both ranges are matching (e.g.
(a,b),(c,d)/(a,b],(c,d]/[a,b),[c,d)/[a,b],[c,d]) - OR the amount of items are the same, and the second range is inclusive on both ends (e.g.
(a,b),[c,d]/(a,b],[c,d]/[a,b),[c,d]/[a,b],[c,d]) - OR the amount of items are the same, and the first range is exclusive on both ends (e.g.
(a,b),(c,d)/(a,b),(c,d]/(a,b),[c,d)/(a,b),[c,d])
Æ # Reduce the inner pairs of the first (implicit) input by subtracting
# e.g. [[9,8],[2,1]] → [1,1]
D`‹ # Option 1:
D # Duplicate it
` # Pop and push both values separated to the stack
# → 1,1
‹ # Check if the second is larger than the first
# → 0 (falsey)
s # Swap so the duplicated pair is at the top again
Ë # Check if both integers in this pair are the same
# → 1 (truthy)
IË # Option 2:
I # Push the second input-pair of bit-strings
Ë # Check if both are the same
# e.g. ["01","10"] → 0 (falsey)
I`ß # Option 3:
I # Push the second input-pair of bit-strings again
` # Pop and push both separated to the stack
# → "01","10"
ß # Pop the second one, and push its minimum
# (1 if it was 11; 0 if it was 00/01/10)
# "10" → 0 (falsey)
s_ # Option 4:
s # Swap so the first one is at the top now
_ # Pop and check whether it is 0
# (1 if it was 00; 0 if it was 01/10/11)
# "01" → 0 (falsey)
~~*~ # Or,Or,And,Or to combine all checks:
~~ # Options 2, 3, OR 4
# 0,0,0 → 0 (falsey)
* # AND both items were the same
# 1,0 → 0 (falsey)
~ # Or option 1
# 0,0 → 0 (falsey)
# (after which the result is output implicitly)
Charcoal, 24 bytes
≔Eη⁻ι§θκυ‹⊖⊙()№υι⁻§υ²§υ¹
Try it online! Link is to verbose version of code. Takes input as a pair of lists [char_lower, int_lower, int_upper, char_upper]. Explanation:
≔Eη⁻ι§θκυ
Pairwise subtract the two inputs. The ints just subtract normally of course, while the chars turn into empty strings if they match.
‹⊖⊙()№υι⁻§υ²§υ¹
Check that the resulting range length is non-negative unless there was an open range matched with a closed range in which case the range length needs to be positive.
50 bytes for a more parsed version:
FE²S⊞υ⟦§ι⁰↨I⪪✂ι¹±¹¦¹,±¹§ι±¹⟧≔E⊟υ⁻ι§⌈υκυ‹⊖⊙()№υι§υ¹
Try it online! Link is to verbose version of code. Takes input as two newline-separated range strings. Explanation:
FE²S⊞υ⟦§ι⁰↨I⪪✂ι¹±¹¦¹,±¹§ι±¹⟧
Input and parse the two strings, calculating the length of the range.
≔E⊟υ⁻ι§⌈υκυ
Pairwise subtract the two inputs.
‹⊖⊙()№υι§υ¹
Check that the resulting range length is non-negative unless there was an open range matched with a closed range in which case the range length needs to be positive.
Retina 0.8.2, 50 bytes
\d+
$*
(1+),\1
^(.)(1+)(.),(.1\2|(\1|\[)\2(\3|]))
Try it online! Link includes test cases. Explanation:
\d+
$*
Convert to unary.
(1+),\1
Get just the lengths of each range.
^(.)(1+)(.),(.1\2|(\1|\[)\2(\3|]))
Match if the second range is longer than the first, or if it's at least as closed as the first.