day 9, the hardest so far, spent to much time debugging

This commit is contained in:
setop 2022-12-10 13:19:04 +01:00
parent e51de70c36
commit e4d64206de
2 changed files with 50 additions and 0 deletions

20
d09/part1.awk Normal file
View File

@ -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)}

30
d09/part2.py Normal file
View File

@ -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))