Go to opposite corner of rectangle in all directions - Harder
Pyth, 46 45 44 bytes
eSKtMQjsM_mm?sJ,kd?q#J.umtWbbNeSKK\#\.\XhQeQ
Try it here.
Explanation:
move-count-printing:
K assign K to...
Q the input, a 2-length array...
tM with each element decremented
eS take the max and output it (this is the number of moves)
main path-finding logic:
mm hQeQ map over x-values and y-values...
J,kd assign J to [x,y]
?s if both x and y are NOT zero (sum is truthy)...
?q#J[...] if [x,y] is present in [...] (see below)...
\# ... put a # at this position in the output
\. ... else, put a . at this position
\X ... else, put the X here (at [0,0])
jsM_ reverse and output in the proper format
the [...] part in the code above, which finds positions where #s go:
.u eSKK cumulative reduce on <number of moves> elements, starting at K,
which is [max_x, max_y] as assigned at the beginning
m N map over x and y...
tWbb decrement, only if the value is > 0
JavaScript (ES6), 132
Edit 2 bytes saved thx @Neil
(w,h)=>[...Array(--h)].map((_,i)=>R(w-i)+'#'+R(i),--w,R=(n,c='.')=>c.repeat(n>w?w:n>0&&n)).join`
`+`
x${R(w-h,'#')+R(h)}
`+(h>w?h:w)
Test
f=(w,h)=>[...Array(--h)].map((_,i)=>R(w-i)+'#'+R(i),--w,R=(n,c='.')=>c.repeat(n>w?w:n>0&&n)).join`
`+`
x${R(w-h,'#')+R(h)}
`+(h>w?h:w)
function test() {
var w,h
[w,h]=I.value.match(/\d+/g)
O.textContent=f(w,h)
}
test()
Test <input id=I value="4 5"><button onclick="test()">-></button>
<pre id=O></pre>