| Bytes | Lang | Time | Link |
|---|---|---|---|
| 697 | Python3 | 240624T190048Z | Ajax1234 |
| nan | 150512T182044Z | edc65 | |
| 628 | C++ | 150516T222005Z | Reto Kor |
| 520 | Python 3 | 150512T061415Z | DLosc |
Python3, 697 bytes
Long, but fast.
E=enumerate
def f(b):
d,h={},{}
for x,r in E(b):
for y,v in E(r):d[(x,y)]=v;h[v]=(x,y)
q,s=[(d,[],[])],[d]
M=[(1,0),(0,1),(-1,0),(0,-1)]
while q:
q=sorted(q,key=lambda x:x[-1].count('D'))[::-1]
d,c,C=q.pop(0)
if'D'not in d.values():continue
for i in d:
if d[i].isalpha():
for X,Y in M:
x,y=i;p=[]
while(K:=d.get(v:=(x:=x+X,y:=y+Y)))in['#','*']:p+=[v]
if K and'D'==d[i]and p and'*'==d[p[-1]]:return c+[(X,Y)]
D=eval(str(d))
D[i]='#'
if(F:=K and K.isalpha())and p:
if'*'==D[p[-1]]:continue
D[p[-1]]=d[i]
j,k=h['*']
if D not in s and(d[i]!='D'or F)and any(D[(j+J,k+K)].isalpha()for J,K in M):q+=[(D,c+[(X,Y)],c+[d[i]])];s+=[D]
JavaScript (ES6) 322 334 323
Edit2 Added animation in the snippet
Edit Bug fix, remember initial posiiton of '*', so I find it even when a seal slide over it and erase it.
Implemented as a function with the input string as parameter (probably invalid but waiting for a clarification). Output via popup.
The problem with multiline string input in JavaScript is that prompt does not manage it well.
As for the algorithm: a BFS, should find an optimal solution.
I keep a queue of game status in variable l, the status beeing the character grid g and the sequence of moves so far s. Besides, there is a set of grids obtained so far in variable k, to avoid exploring the same grid again and again.
The main loop is
- dequeue a game status
- try all possibile moves, enqueue the status after each valid move (IIF the resulting grid is not already present)
- if solution found then exit the loop
F=s=>{
o=~s.match(/\d+/),g=[...z=s.replace(/.*/,'')];
for(l=[[g,'']],k=[];[g,s]=l.shift(),!g.some((c,p)=>
c>'A'&&[-1,1,o,-o].some((d,j)=>{
t=s+' '+[c,'LRUD'[j]];
for(q=p;(u=g[q+d])<'.';)q+=d;
return(c<'a'&z[q]=='*')||
c>'D'|u>'.'&&!(
f=[...g],u=='.'?0:f[q]=c,f[p]='#',
k[h=f.map(v=>v>'D'?0:v)]||(k[h]=l.push([f,t]))
)
})
););
alert(t)
}
Run Snippet to test in FireFox
F=s=>{
o=~s.match(/\d+/),g=[...z=s.replace(/.*/,'')];
for(l=[[g,k=[]]];[g,s]=l.shift(),!g.some((c,p)=>
c>'A'&&[-1,1,o,-o].some((d,j)=>{
t=s+' '+[c,'LRUD'[j]];
for(q=p;(u=g[q+d])<'.';)q+=d;
return(c<'a'&z[q]=='*')||
c>'D'|u>'.'&&!(
f=[...g],u=='.'?0:f[q]=c,f[p]='#',
k[h=f.map(v=>v>'D'?0:v)]||(k[h]=l.push([f,t]))
)
})
););
return t
}
$('.T td').each(function(i,td) { $('.R td').eq(i).text(F($(td).text())) });
$(".R").on('click','td',function(){
var i = $(this).index();
var $td = $('.T td').eq(i);
var t = $td.text().trim();
animate($td, $td.text().trim(), $('.R td').eq(i).text().trim());
});
$('#GO').on('click',function() {
var s = T0.value, h=F(s)
console.log(s)
$('#R0').text(h);
animate($('#A'), s,h);
});
function animate($out, s, h){
o=~s.match(/\d+/),g=[...s.replace(/.*/,'')];
fr=[g.join('')];
h.replace(/.,(.)/g, (c,d)=>{
i = g.indexOf(c=c[0]);
d = [-1,1,o,-o]['LRUD'.search(d)]
for(;g[i+d]<'.';i+=d)
{
g[i]='#',z=[...g],g[i+d]=c,z[i+d]='<b>'+c+'</b>', fr.push(z.join(''))
}
if(g[i+d]=='.')
{
g[i]='#', fr.push(g.join(''))
}
})
var fa = ()=>{
var f=fr.shift();
f ? $out.html(f) : (
clearInterval(i),
$out.text(s),
$out.toggleClass('Anim')
)};
$out.toggleClass('Anim');
fa();
var i=setInterval(fa, 500);
}
td { vertical-align: top; font-family: monospace; white-space: pre; }
textarea { width: 99%; height:10em;}
pre { margin: 0; }
.R td { background: #fdc; padding: 6px 0; }
.R td:hover { background: #ffc }
.T td { background: #ddf}
B { color: red }
.T td.Anim, td.Anim { background: #ffc; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellspacing=5>
<tr>
<th colspan=4>Predefined tests</th>
</tr>
<tr class=T>
<td>25,5
.........................
.#######################.
.####D#############*k###.
.#######################.
.........................</td>
<td>9,7
.........
.a#####b.
.#####d#.
.##l*###.
.###m#p#.
.#D#.#c#.
.........</td>
<td>26,5
..........................
.###..................###.
.l*##########v#########D#.
.###..................###.
..........................</td>
<td>8,5
........
.##c###.
.a#*#D#.
.##b###.
........</td>
</tr>
<tr><td colspan=4>Click on solution text to start animation</td>
</tr>
<tr class=R>
<td></td><td></td><td></td><td></td>
</tr>
<tr><th colspan=2>Your test </th></tr>
<tr><td colspan=2><textarea id=T0></textarea></td>
<td colspan=2 id=A></td>
</tr>
<tr>
<td colspan=2>Find solution and animate <button id=GO>go</button></td>
<td colspan=2 id=R0></td>
</tr>
</table>
C++, 628 bytes
Well, this didn't turn out very short:
#include <set>
#include <iostream>
using namespace std;struct R{string b,m;bool operator<(R r)const{return b<r.b;}};int w,h,t,j,k,z=1;char c,f;set<R> p,q;int m(R r,int x,int d,char a){for(j=x,c=r.b[x];(f=r.b[j+=d])==35;);if(c-68||f-46){r.b[x]=35;if(f-46)r.b[j-d]=c;r.m+=c;r.m+=44;r.m+=a;r.m+=10;if(c==68&j-d==t){cout<<r.m;z=0;}if(p.count(r)+q.count(r)==0){q.insert(r);}}}int main(){cin>>w>>c>>h>>c;R r;string l;for(;k++<h;){getline(cin,l);r.b+=l;}t=r.b.find(42);r.b[t]=35;q.insert(r);for(;z;){r=*q.begin();q.erase(q.begin());p.insert(r);for(k=0;z&&k<w*h;++k){if(r.b[k]>64){m(r,k,-1,76);m(r,k,1,82);m(r,k,-w,85);m(r,k,w,68);}}}}
I chose C++ because I wanted to use the data structures (set, string), but it's inherently quite verbose. The solution does reasonably well on performance, solving test 2 in a little over 2 seconds on a MacBook Pro, even though it's not optimized for runtime.
Code before starting to eliminate whitespace and some other length reduction:
#include <set>
#include <iostream>
using namespace std;
struct R {
string b, m;
bool operator<(R r) const {return b < r.b; }
};
int w, h, t;
set<R> p, q;
bool z = true;
void m(R r, int k, int d, char a) {
int j = k;
char s = r.b[k], f;
for (; (f = r.b[j += d]) == 35;);
if (s - 68 || f - 46) {
r.b[k] = 35;
if (f - 46) {
r.b[j - d] = s;
}
r.m += s;
r.m += 44;
r.m += a;
r.m += 10;
if (s == 68 && j - d == t) {
cout << r.m;
z = false;
}
if (p.count(r) + q.count(r) == 0) {
q.insert(r);
}
}
}
int main() {
char c;
cin >> w >> c >> h >> c;
string b, l;
int k;
for (k = 0; k < h; ++k) {
getline(cin, l);
b += l;
}
t = b.find(42);
b[t] = 35;
R r;
r.b = b;
q.insert(r);
for ( ; z; ) {
r = *q.begin();
q.erase(q.begin());
p.insert(r);
for (k = 0; z && k < w * h; ++k) {
c = r.b[k];
if (c > 64) {
m(r, k, -1, 76);
m(r, k, 1, 82);
m(r, k, -w, 85);
m(r, k, w, 68);
}
}
}
return 0;
}
The core idea behind the algorithm is that two sets are maintained:
qis the set of configurations that are pending processing.pis the set of configurations that have been processed.
The algorithm starts with the initial configuration in q. In every step, a configuration is popped from q, added to p, all possible seal movements are generated, and the resulting new configurations inserted into q.
Test run:
bash-3.2$ ./a.out <test1
D,R
bash-3.2$ time ./a.out <test2
p,U
c,U
p,R
c,L
m,L
b,L
a,L
D,U
b,L
D,R
D,D
D,L
real 0m2.267s
user 0m2.262s
sys 0m0.003s
bash-3.2$ ./a.out <test3
v,U
D,L
bash-3.2$
Python 3, 520 bytes
R=range
g=[list(input())for i in R(int(input().split(',')[1]))]
f=set(sum(g,[]))-set(".#*")
L=8
def P(p,g):
if len(p)>L:return
for s in f:
c=sum(y.index(s)for y in g if s in y)
if c<1:continue
r,=[n for n in R(len(g))if s in g[n]]
for d in R(4):
m=p+s+",%s\n"%"LURD"[d];G=[y[:]for y in g];o="#";i,j=I,J=r,c
while"#"==o:G[i][j]="#";G[I][J]=s;i,j=I,J;I,J=i+d%2*(d-2),j+(~d%-2&d-1);o=G[I][J]
if"."==o:G[i][j]="#"
if"D"==s:
if"."==o:continue
if"*"==o:print(m);1/0
P(m,G)
while 1:P("",g);L+=4
I may do a more detailed explanation later if people want, but basically this runs a depth-first search with iterative deepening on the state space of possible moves. If a move causes the daddy seal to fall off, it is immediately rejected. If the daddy ends up next to the transmitter, the sequence of moves is printed, and the program divides by zero to force an exit.
I can make the code run significantly faster by adding if G!=g: at the beginning of the second-last line, for 8 extra bytes--this rejects moves that don't change anything, such as k,L in the first test case.
The runtime varies noticeably from one run to the next, even with the same input--evidently a result of the fact that I pick the next seal to move by iterating over a set, which is an unordered type. I timed the second test case at 5 minutes 30 seconds, though it didn't seem that long the first time I ran it. With the optimization mentioned above, it's more like 40 seconds.