diff --git a/d09/part1.awk b/d09/part1.awk new file mode 100644 index 0000000..3090876 --- /dev/null +++ b/d09/part1.awk @@ -0,0 +1,20 @@ +BEGIN{P["0_0"]=1; Hx=Hy=Tx=Ty=0} +function abs(x) { return x>0?x:-x} +{ + print "==", Hx","Hy, ";", Tx","Ty, "==", $1, $2 + for(i=1;i<=$2;i++){ + if($1=="R") {Hx++} + if($1=="L") {Hx--} + if($1=="U") {Hy++} + if($1=="D") {Hy--} + dh = abs(Hx-Tx) # tension horizontale + dv = abs(Hy-Ty) # tension vertivale + if(dh>1 && $1=="R") {Tx++; if (dv>0) {Ty=Hy}} + if(dh>1 && $1=="L") {Tx--; if (dv>0) {Ty=Hy}} + if(dv>1 && $1=="U") {Ty++; if (dh>0) {Tx=Hx}} + if(dv>1 && $1=="D") {Ty--; if (dh>0) {Tx=Hx}} + P[Ty "_" Tx]=1 + print Hx, Hy, ";", "dh:"dh, "dv:"dv, "=>", Tx, Ty + } +} +END{print length(P)} \ No newline at end of file diff --git a/d09/part2.py b/d09/part2.py new file mode 100644 index 0000000..3a7a5b7 --- /dev/null +++ b/d09/part2.py @@ -0,0 +1,30 @@ +import sys + +rope = [(0,0) for _ in range(10)] +P = [{(0,0)} for _ in range(10) ] +D = {"R":(1,0),"L":(-1,0),"U":(0,1),"D":(0,-1)} + +for l in sys.stdin.read().splitlines(): + M, q = l.split(" ") + q = int(q) + for k in range(q): + (x,y) = rope[0] + (dx,dy) = D[M] + rope[0] = (x+dx, y+dy) + for i in range(1,10): + (Hx,Hy) = rope[i-1] + (Tx,Ty) = rope[i] + dh = Hx-Tx # tension horizontale + dv = Hy-Ty # tension vertivale + if (abs(dh)>1): + Tx += 1 if dh > 0 else -1 + if (abs(dv)>0): + Ty += 1 if dv > 0 else -1 + elif (abs(dv)>1): + Ty += 1 if dv > 0 else -1 + if (abs(dh)>0): + Tx += 1 if dh > 0 else -1 + P[i].add((Tx,Ty)) + rope[i] = (Tx,Ty) + +print(*(len(p) for p in P)) \ No newline at end of file