Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def possibilities(values, limit):
- queue = [values[0]]
- for v in values[1:]:
- new_queue = []
- for q in queue[:]:
- summation = v + q
- product = v * q
- abso = int(f"{q}{v}")
- if summation <= limit:
- new_queue.append(summation)
- if product <= limit:
- new_queue.append(product)
- if abso <= limit:
- new_queue.append(abso)
- queue = new_queue
- output = []
- for q in queue:
- if q == limit:
- output.append(q)
- return output
- def iterate(file):
- for line in open(file):
- line = line.strip()
- limit, values = line.split(':')
- limit = int(limit)
- values = [int(x) for x in values.strip().split()]
- yield limit, values
- def main(file):
- total = 0
- for limit, values in iterate(file):
- combinations = possibilities(values, limit)
- if len(combinations) == 0:
- continue
- total += limit
- return total
- if __name__ == '__main__':
- print(main('input.txt'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement