09 A, værre
This commit is contained in:
parent
cc919d8566
commit
694959e6c1
156
09/2022-12-09-A.py
Executable file → Normal file
156
09/2022-12-09-A.py
Executable file → Normal 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]]]))
|
|
||||||
|
def mov(cmd,obj):
|
||||||
|
steps=int(cmd[2:])+1
|
||||||
|
result=[]
|
||||||
|
if cmd[0] == "U":
|
||||||
|
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:
|
else:
|
||||||
return (sum([loc[0],int(cmd[2:])*b[cmd[0]]]),loc[1])
|
for i in range(1,steps):
|
||||||
|
result.append((obj[0]-i,obj[1]))
|
||||||
|
print("cmd: {} result: {}".format(cmd,result))
|
||||||
|
return result
|
||||||
|
|
||||||
logH = [(0,0)]
|
counter = 1
|
||||||
logT = [(0,0)]
|
|
||||||
for l in a.split("\n"):
|
for l in a.split("\n"):
|
||||||
#print("LogT: {}\n".format(logT[-1]))
|
print(f"\nNew line ({counter})")
|
||||||
logH.append(mov(l,logH[-1]))
|
counter+=1
|
||||||
#print("Diff: {} {}, l {}".format(logH[-1][0]-logT[-1][0],logH[-1][1]-logT[-1][1],l))
|
head_start = head[-1]
|
||||||
#print("LogH: {}".format(logH[-1]))
|
#dia_jump = mov(l,head_start)[-2]
|
||||||
# If in same column:
|
#print(f"-{dia_jump}-")
|
||||||
if logT[-1][0]==logH[-1][0]:
|
#break
|
||||||
# Case new head is next to or covering tail:
|
print(f"head start: {head_start}")
|
||||||
if logT[-1][1]==logH[-1][1]:
|
head_move = mov(l,head_start)
|
||||||
#print("Samme: {} {} {}".format(l,logH[-1],logT[-1]))
|
head.extend(head_move)
|
||||||
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:
|
tpos=tail[-1]
|
||||||
elif logT[-1][1]==logH[-1][1]:
|
hpos=head[-1]
|
||||||
# Case next to:
|
#print(is_close(tpos,hpos))
|
||||||
if abs(logT[-1][1]-logH[-1][1])<2:
|
#continue
|
||||||
logT.append(logT[-1])
|
#print(head_start)
|
||||||
continue
|
print(f"HS {head_start} TP {tpos} HP {hpos}")
|
||||||
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:
|
# Diagonalt.
|
||||||
else:
|
if is_close(hpos,tpos):
|
||||||
# Case diagonally next to
|
print("Is close")
|
||||||
if abs(logT[-1][0]-logH[-1][0])<2 and abs(logT[-1][1]-logH[-1][1])<2:
|
tail.append(tpos)
|
||||||
#print("Diagon: {} LogH: {} LogT: {}".format(abs(logT[-1][0]-logH[-1][0]),logH[-1],logT[-1]))
|
continue
|
||||||
logT.append(logT[-1])
|
|
||||||
continue
|
if hpos[1]!=tpos[1] and tpos[0]!=hpos[0]:
|
||||||
if l[0] in ["U","D"]:
|
print(f"Udfordringer. {head_move} tpos: {tpos} hpos: {hpos} hs: {head_start}")
|
||||||
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))
|
temp = []
|
||||||
#print("UD")
|
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:
|
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("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(logH)
|
print(head)
|
||||||
print(logT)
|
print(tail)
|
||||||
print(len(sorted(set(logT))))
|
print(len(sorted(set(tail))))
|
||||||
exit()
|
|
||||||
|
|
||||||
result=[]
|
|
||||||
for i in range(0,50):
|
|
||||||
result.append(list(".")*100)
|
|
||||||
|
|
||||||
|
|
||||||
for i in range(0,len(logH)):
|
|
||||||
result[logT[i][0]][logT[i][1]]="T"
|
|
||||||
result[logH[i][0]][logH[i][1]]="H"
|
|
||||||
|
|
||||||
for b in result:
|
|
||||||
print("".join(b))
|
|
||||||
print(f"{i}\n\n")
|
|
||||||
time.sleep(2)
|
|
||||||
os.system("clear")
|
|
||||||
result[logH[i][0]][logH[i][1]]="."
|
|
||||||
result[logT[i][0]][logT[i][1]]="."
|
|
||||||
|
|
||||||
print(len(sorted(set(logT))))
|
|
Loading…
Reference in a new issue