Guest User

AoC Day 9 Python 3

a guest
Dec 9th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. # day9
  2. from collections import defaultdict,OrderedDict
  3.  
  4. input = "d9_data.txt"
  5. test = "d9_test.txt"
  6.  
  7. def read_file(filename):
  8.     with open(filename) as f:
  9.         return f.read().splitlines()
  10.  
  11. cut=5
  12. data = read_file(test)
  13. data = list(map(int,data))
  14. data2 = data.copy()
  15.  
  16.  
  17. check = []
  18. mydict = defaultdict(list)
  19.  
  20. def update_lookup():
  21.     #print("Updating Check...")
  22.     check.clear()
  23.     for i in range(0,cut):
  24.         check.append(data[i])
  25.  
  26.  
  27. def update_data():
  28.     #print("Updading Data...")
  29.     data.pop(0)
  30.     #print(data)
  31.     mydict.clear()
  32.  
  33.  
  34. update_lookup()
  35.  
  36.  
  37. def group_by_lastdigit(int_arr,mydict):
  38.     #print("group_by_lastdigit with:")
  39.     #print(int_arr)
  40.     for number in sorted(int_arr,key=lambda n: n%10):
  41.         i = number%10
  42.         mydict[i].append(number)
  43.     #print (mydict)
  44.     return mydict
  45.  
  46.  
  47. group = group_by_lastdigit(check,mydict)
  48.  
  49.  
  50. def check_sum(number):
  51.     #print("checking_sum with: " +str(number))
  52.     last = number%10
  53.     buckets_to_check = check_buckets(last)
  54.     #print(buckets_to_check)
  55.     for bucket in buckets_to_check:
  56.         add1=group.get(bucket[0])
  57.         add2=group.get(bucket[1])
  58.         for a1 in add1:
  59.             for a2 in add2:
  60.                 if (a1+a2==number and a1!=a2): return True
  61.     return False
  62.  
  63.  
  64. def check_buckets(digit):
  65.     #print("checking_buckets with: " +str(digit))
  66.     res = []
  67.     buckets = list(group)
  68.     for i in buckets:
  69.         for j in buckets:
  70.             if (j+i)%10==digit:
  71.                 if i>=j:
  72.                     res.append((j,i))
  73.                 else:
  74.                     res.append((i,j))
  75.     #res = list(OrderedDict.fromkeys(res))            
  76.     res = list(set(res))
  77.     return res
  78.  
  79.  
  80. print("Starting")
  81. print("Initial Check:")
  82. #print(check)
  83.  
  84. while (True):
  85.     if(check_sum(data[cut])):
  86.         update_data()
  87.         update_lookup()
  88.         group = group_by_lastdigit(check,mydict)
  89.     else:
  90.         print(data[cut])
  91.         break
  92.  
  93. input2=int((data[cut]))
  94.  
  95. def high_low(arr,number):
  96.     small = [i for i in arr if i<=number]
  97.     return small
  98.  
  99. small_input=high_low(data2,input2)
  100. print(small_input)
  101. small_input.reverse()
  102.  
  103. print(small_input)
  104.  
  105. final=[]
  106.  
  107. for i in range(1,len(small_input)):
  108.     j=1
  109.     high=len(small_input)-i
  110.     print(high)
  111.     res=small_input[i]+small_input[i+j]
  112.     while(res<input2):
  113.         j+=1
  114.         #if j==high: break
  115.         res+=small_input[i+j]
  116.         if(res==input2):
  117.             print (res, small_input[i], small_input[i+j])
  118.             for a in range(0,j+1):
  119.                 final.append(small_input[a+i])
  120.                 print(final)
  121.             final.sort()
  122.  
  123.             print(final[0] + final[-1])      
Advertisement
Add Comment
Please, Sign In to add comment