09 A, værre

This commit is contained in:
Rasmus Malver 2022-12-09 23:57:56 +01:00
parent cc919d8566
commit 694959e6c1

160
09/2022-12-09-A.py Executable file → Normal file
View file

@ -1,7 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os,time
a="""R 4 a="""R 4
U 4 U 4
L 3 L 3
@ -10,79 +8,101 @@ R 4
D 1 D 1
L 5 L 5
R 2""" R 2"""
#a=open("/Users/rasmus/Gits/aoc2022/09/ii").read().strip() a=open("/Users/rasmus/Gits/aoc2022/09/i").read().strip()
b={"L":-1, "U":1, "R":1, "D":-1} b={"L":-1, "U":1, "R":1, "D":-1}
head=[(10,10)]
tail=[(10,10)]
def mov(cmd,loc): def is_close(objA,objB):
if cmd[0] in ["U","D"]: return abs(objA[0]-objB[0])<2 and abs(objA[1]-objB[1])<2
return (loc[0],sum([loc[1],int(cmd[2:])*b[cmd[0]]]))
else:
return (sum([loc[0],int(cmd[2:])*b[cmd[0]]]),loc[1])
logH = [(0,0)]
logT = [(0,0)]
for l in a.split("\n"):
#print("LogT: {}\n".format(logT[-1]))
logH.append(mov(l,logH[-1]))
#print("Diff: {} {}, l {}".format(logH[-1][0]-logT[-1][0],logH[-1][1]-logT[-1][1],l))
#print("LogH: {}".format(logH[-1]))
# If in same column:
if logT[-1][0]==logH[-1][0]:
# Case new head is next to or covering tail:
if logT[-1][1]==logH[-1][1]:
#print("Samme: {} {} {}".format(l,logH[-1],logT[-1]))
logT.append(logT[-1])
continue
elif abs(logT[-1][1]-logH[-1][1])<2:
#print("Tæt: {} {} {}".format(l,logH[-1],logT[-1]))
logT.append(logT[-1])
continue
# If above move to one above, else one below
logT.append((logT[-1][0],logH[-1][1]+1)) if logT[-1][1]>logH[-1][1] else logT.append((logT[-1][0],logH[-1][1]-1))
print("Her")
# If same row:
elif logT[-1][1]==logH[-1][1]:
# Case next to:
if abs(logT[-1][1]-logH[-1][1])<2:
logT.append(logT[-1])
continue
logT.append((logH[-1][0]+1,logT[-1][1])) if logT[-1][0]>logH[-1][0] else logT.append((logH[-1][0]-1,logT[-1][1]))
# If neither:
else:
# Case diagonally next to
if abs(logT[-1][0]-logH[-1][0])<2 and abs(logT[-1][1]-logH[-1][1])<2:
#print("Diagon: {} LogH: {} LogT: {}".format(abs(logT[-1][0]-logH[-1][0]),logH[-1],logT[-1]))
logT.append(logT[-1])
continue
if l[0] in ["U","D"]:
logT.append((logH[-1][0],logH[-1][1]+1)) if logT[-1][1]>logH[-1][1] else logT.append((logH[-1][0],logH[-1][1]-1))
#print("UD")
else:
logT.append((logH[-1][0]+1,logH[-1][1])) if logT[-1][0]>logH[-1][0] else logT.append((logH[-1][0]-1,logH[-1][1]))
print(logH)
print(logT)
print(len(sorted(set(logT))))
exit()
def mov(cmd,obj):
steps=int(cmd[2:])+1
result=[] result=[]
for i in range(0,50): if cmd[0] == "U":
result.append(list(".")*100) for i in range(1,steps):
result.append((obj[0],obj[1]+i))
elif cmd[0] == "D":
for i in range(1,steps):
result.append((obj[0],obj[1]-i))
elif cmd[0] == "R":
for i in range(1,steps):
result.append((obj[0]+i,obj[1]))
else:
for i in range(1,steps):
result.append((obj[0]-i,obj[1]))
print("cmd: {} result: {}".format(cmd,result))
return result
counter = 1
for l in a.split("\n"):
print(f"\nNew line ({counter})")
counter+=1
head_start = head[-1]
#dia_jump = mov(l,head_start)[-2]
#print(f"-{dia_jump}-")
#break
print(f"head start: {head_start}")
head_move = mov(l,head_start)
head.extend(head_move)
for i in range(0,len(logH)): tpos=tail[-1]
result[logT[i][0]][logT[i][1]]="T" hpos=head[-1]
result[logH[i][0]][logH[i][1]]="H" #print(is_close(tpos,hpos))
#continue
#print(head_start)
print(f"HS {head_start} TP {tpos} HP {hpos}")
for b in result: # Diagonalt.
print("".join(b)) if is_close(hpos,tpos):
print(f"{i}\n\n") print("Is close")
time.sleep(2) tail.append(tpos)
os.system("clear") continue
result[logH[i][0]][logH[i][1]]="."
result[logT[i][0]][logT[i][1]]="."
print(len(sorted(set(logT)))) if hpos[1]!=tpos[1] and tpos[0]!=hpos[0]:
print(f"Udfordringer. {head_move} tpos: {tpos} hpos: {hpos} hs: {head_start}")
temp = []
all_head_move = [head_start]
all_head_move.extend(head_move)
for move in all_head_move:
if is_close(move,tpos):
temp = move
else:
break
if temp == []:
print(f"All is lost.\nhpos: {hpos}\ntpos: {tpos}\nhead_start: {head_start}\nhead_move: {head_move}")
exit()
tail.append(temp)
tpos=temp
print(f"Sat move til {temp}")
# Same column
if tpos[0]==hpos[0]:
# If head above of tail
if hpos[1]>tpos[1]:
print("Head is above of tail {} {}".format(hpos[1],tpos[1]))
steps = hpos[1]-tpos[1]-1
tail.extend(mov(f"U {steps}",tpos))
else:
print("Head is below tail {} {}".format(hpos[1],tpos[1]))
steps = tpos[1]-hpos[1]-1
tail.extend(mov(f"D {steps}",tpos))
# Same rpw
elif tpos[1]==hpos[1]:
print(f"Same row. tpos: {tpos} hpos: {hpos}")
if hpos[0]>tpos[0]:
steps = hpos[0]-tpos[0]-1
print(f"Steps (hpos>tpos) {steps}")
tail.extend(mov(f"R {steps}",tpos))
#print(mov(f"R {steps}",tpos))
else:
steps = tpos[0]-hpos[0]-1
print(f"Steps hpos<tpos {steps}")
tail.extend(mov(f"L {steps}",tpos))
#print(mov(f"L {steps}",tpos))
print(head)
print(tail)
print(len(sorted(set(tail))))