diff --git a/09/2022-12-09-A.py b/09/2022-12-09-A.py old mode 100755 new mode 100644 index b4943fb..81a87fe --- a/09/2022-12-09-A.py +++ b/09/2022-12-09-A.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -import os,time - a="""R 4 U 4 L 3 @@ -10,79 +8,101 @@ R 4 D 1 L 5 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} +head=[(10,10)] +tail=[(10,10)] -def mov(cmd,loc): - if cmd[0] in ["U","D"]: - return (loc[0],sum([loc[1],int(cmd[2:])*b[cmd[0]]])) +def is_close(objA,objB): + return abs(objA[0]-objB[0])<2 and abs(objA[1]-objB[1])<2 + +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: - 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)] -logT = [(0,0)] +counter = 1 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") + 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) + + tpos=tail[-1] + hpos=head[-1] + #print(is_close(tpos,hpos)) + #continue + #print(head_start) + print(f"HS {head_start} TP {tpos} HP {hpos}") + + # Diagonalt. + if is_close(hpos,tpos): + print("Is close") + tail.append(tpos) + continue - # 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") + 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: - 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() + print("Head is below tail {} {}".format(hpos[1],tpos[1])) + steps = tpos[1]-hpos[1]-1 + tail.extend(mov(f"D {steps}",tpos)) -result=[] -for i in range(0,50): - result.append(list(".")*100) + # 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