9
\$\begingroup\$

Given two sorted lists of same length \$\{a_i\}\$ and \$\{b_i\}\$, find the smallest \$w\$ s.t. it's possible to connect each pair of points \$(0,a_i)\$ and \$(w,b_i)\$ with paths that

  • x-coord always in open interval \$(0,w)\$, besides endpoints
  • Every point on every path have at least one integer coord(=Path consists of horizonal and vertical segments, all of whose both end are integer point)
  • No overlap

You can assume all inputs are positive. You can assume it possible(aka, no \$a_1=a_2\$ thing). You can decide increasing or decreasing input order. In case \$\{a_i\} = \{b_i\}\$, resulting 0 or 1 are both reasonable.

E.g. for a={1,3,5}, b={2,4,6}, smallest width is 2:

6  ┌─
5 ─┘
4  ┌─
3 ─┘
2  ┌─
1 ─┘
  012

For a={1,2,5}, b={2,4,6} smallest width is 3:

6  ┌──
5 ─┘
4  ┌──
3  │
2 ─┘┌─
1 ──┘
  0123

For a={1,2,5}, b={2,5,6} smallest width is also 3:

6  ┌──
5 ─┘┌─
4  ┌┘
3  │
2 ─┘┌─
1 ──┘
  0123

Test cases

{1,3,5}, {2,4,6} => 2
{1,2,5}, {2,4,6} => 3
{1,2,5}, {2,5,6} => 3
{1,2,3}, {1,2,3} => 0 or 1
{1,2,3}, {4,5,6} => 4
{1,5,9}, {4,5,6} => 2
{3,5,7,9}, {1,3,5,7} => 3
{2,3,4,5}, {1,2,3,4} => 5
{1,2,3,4}, {2,5,7,9} => 5
{1,4,7}, {11,14,17} => 4
{1,4,7,13}, {11,14,17,18} => 5
{1,4,7,14}, {11,14,17,18} => 4

Shortest code wins

\$\endgroup\$
6
  • \$\begingroup\$ In the examples, all paths start and end in a horizontal segment. May paths start or end in a vertical segment? \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ @doubleunary No. x-coord always in open interval (0,w), besides endpoints, vertical segments at end breaks it \$\endgroup\$ Commented yesterday
  • 1
    \$\begingroup\$ Added packing, I think that is appropriate. \$\endgroup\$ Commented 20 hours ago
  • 1
    \$\begingroup\$ Suggested test case: {1,2,3,4},{2,5,7,9}, which I think should give 5? Would be nice to have an example where that adjacency crowding thing is only on one side \$\endgroup\$ Commented 19 hours ago
  • 1
    \$\begingroup\$ @UnrelatedString Added more cases. Disagree with the only answer [1,4,7,15], [11,14,17,18], not sure who is wrong \$\endgroup\$ Commented 18 hours ago

1 Answer 1

2
\$\begingroup\$

Python3, 394 bytes

def f(a,b):
 w=0
 while(w:=w+1):
  d=[{(x,y):0for x in range(w+1)for y in range(max({*a,*b})+1)}]
  for A,B in zip(a,b):
   A,B=sorted([A,B])
   q,s=[((0,A),(w,B),0,{**u,(0,A):1,(w,B):1})for u in d],[]
   for S,E,C,u in q:
    if S==E:s+=[u];continue
    q+=[(V,E,1,{**u,V:1})for X,Y in[(0,1),(1,0)]if(u.get(V:=(S[0]+X,S[1]+Y))==0 or V==E)and((C and V!=E)or(1,0)==(X,Y))]
   d=s
  if d:return w

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.