09 A, værre
This commit is contained in:
parent
cc919d8566
commit
694959e6c1
158
09/2022-12-09-A.py
Executable file → Normal file
158
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
|
tpos=tail[-1]
|
||||||
elif abs(logT[-1][1]-logH[-1][1])<2:
|
hpos=head[-1]
|
||||||
#print("Tæt: {} {} {}".format(l,logH[-1],logT[-1]))
|
#print(is_close(tpos,hpos))
|
||||||
logT.append(logT[-1])
|
#continue
|
||||||
continue
|
#print(head_start)
|
||||||
# If above move to one above, else one below
|
print(f"HS {head_start} TP {tpos} HP {hpos}")
|
||||||
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")
|
# Diagonalt.
|
||||||
|
if is_close(hpos,tpos):
|
||||||
|
print("Is close")
|
||||||
|
tail.append(tpos)
|
||||||
|
continue
|
||||||
|
|
||||||
# If same row:
|
if hpos[1]!=tpos[1] and tpos[0]!=hpos[0]:
|
||||||
elif logT[-1][1]==logH[-1][1]:
|
print(f"Udfordringer. {head_move} tpos: {tpos} hpos: {hpos} hs: {head_start}")
|
||||||
# Case next to:
|
temp = []
|
||||||
if abs(logT[-1][1]-logH[-1][1])<2:
|
all_head_move = [head_start]
|
||||||
logT.append(logT[-1])
|
all_head_move.extend(head_move)
|
||||||
continue
|
for move in all_head_move:
|
||||||
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 is_close(move,tpos):
|
||||||
|
temp = move
|
||||||
# If neither:
|
else:
|
||||||
else:
|
break
|
||||||
# Case diagonally next to
|
if temp == []:
|
||||||
if abs(logT[-1][0]-logH[-1][0])<2 and abs(logT[-1][1]-logH[-1][1])<2:
|
print(f"All is lost.\nhpos: {hpos}\ntpos: {tpos}\nhead_start: {head_start}\nhead_move: {head_move}")
|
||||||
#print("Diagon: {} LogH: {} LogT: {}".format(abs(logT[-1][0]-logH[-1][0]),logH[-1],logT[-1]))
|
exit()
|
||||||
logT.append(logT[-1])
|
tail.append(temp)
|
||||||
continue
|
tpos=temp
|
||||||
if l[0] in ["U","D"]:
|
print(f"Sat move til {temp}")
|
||||||
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")
|
# 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))
|
||||||
print(logH)
|
|
||||||
print(logT)
|
|
||||||
print(len(sorted(set(logT))))
|
|
||||||
exit()
|
|
||||||
|
|
||||||
result=[]
|
# Same rpw
|
||||||
for i in range(0,50):
|
elif tpos[1]==hpos[1]:
|
||||||
result.append(list(".")*100)
|
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)
|
||||||
for i in range(0,len(logH)):
|
print(tail)
|
||||||
result[logT[i][0]][logT[i][1]]="T"
|
print(len(sorted(set(tail))))
|
||||||
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