Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # day9
- from collections import defaultdict,OrderedDict
- input = "d9_data.txt"
- test = "d9_test.txt"
- def read_file(filename):
- with open(filename) as f:
- return f.read().splitlines()
- cut=5
- data = read_file(test)
- data = list(map(int,data))
- data2 = data.copy()
- check = []
- mydict = defaultdict(list)
- def update_lookup():
- #print("Updating Check...")
- check.clear()
- for i in range(0,cut):
- check.append(data[i])
- def update_data():
- #print("Updading Data...")
- data.pop(0)
- #print(data)
- mydict.clear()
- update_lookup()
- def group_by_lastdigit(int_arr,mydict):
- #print("group_by_lastdigit with:")
- #print(int_arr)
- for number in sorted(int_arr,key=lambda n: n%10):
- i = number%10
- mydict[i].append(number)
- #print (mydict)
- return mydict
- group = group_by_lastdigit(check,mydict)
- def check_sum(number):
- #print("checking_sum with: " +str(number))
- last = number%10
- buckets_to_check = check_buckets(last)
- #print(buckets_to_check)
- for bucket in buckets_to_check:
- add1=group.get(bucket[0])
- add2=group.get(bucket[1])
- for a1 in add1:
- for a2 in add2:
- if (a1+a2==number and a1!=a2): return True
- return False
- def check_buckets(digit):
- #print("checking_buckets with: " +str(digit))
- res = []
- buckets = list(group)
- for i in buckets:
- for j in buckets:
- if (j+i)%10==digit:
- if i>=j:
- res.append((j,i))
- else:
- res.append((i,j))
- #res = list(OrderedDict.fromkeys(res))
- res = list(set(res))
- return res
- print("Starting")
- print("Initial Check:")
- #print(check)
- while (True):
- if(check_sum(data[cut])):
- update_data()
- update_lookup()
- group = group_by_lastdigit(check,mydict)
- else:
- print(data[cut])
- break
- input2=int((data[cut]))
- def high_low(arr,number):
- small = [i for i in arr if i<=number]
- return small
- small_input=high_low(data2,input2)
- print(small_input)
- small_input.reverse()
- print(small_input)
- final=[]
- for i in range(1,len(small_input)):
- j=1
- high=len(small_input)-i
- print(high)
- res=small_input[i]+small_input[i+j]
- while(res<input2):
- j+=1
- #if j==high: break
- res+=small_input[i+j]
- if(res==input2):
- print (res, small_input[i], small_input[i+j])
- for a in range(0,j+1):
- final.append(small_input[a+i])
- print(final)
- final.sort()
- print(final[0] + final[-1])
Advertisement
Add Comment
Please, Sign In to add comment