aoc2022/09/2022-12-09-A.py

89 lines
2.4 KiB
Python
Raw Normal View History

2022-12-09 22:23:52 +00:00
#!/usr/bin/env python3
import os,time
a="""R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2"""
#a=open("/Users/rasmus/Gits/aoc2022/09/ii").read().strip()
b={"L":-1, "U":1, "R":1, "D":-1}
def mov(cmd,loc):
if cmd[0] in ["U","D"]:
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()
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))))