From 9c2fc7db770d701e353f4a3f507af011f99819ef Mon Sep 17 00:00:00 2001 From: mid-kid Date: Sat, 5 Dec 2020 15:16:59 +0100 Subject: [PATCH] Optimize day 1 a little --- d01.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/d01.py b/d01.py index ca233dd..f90ee63 100755 --- a/d01.py +++ b/d01.py @@ -1,31 +1,26 @@ #!/usr/bin/env python3 -# 99920044 instead of 2020 for bigboy -# p1: 134283487359328 -# p2: 1655132197274893972 +# ./d01.py d01_bigboy.txt 99920044 +# 1939883877222459 +# 32625808479480099854130 from sys import argv -i = [int(x) for x in open(argv[1])] +t = 2020 +if len(argv) > 2: + t = int(argv[2]) -for x in i: +i = set(int(x) for x in open(argv[1])) + +def f(x): for y in i: - if x+y == 2020: - print(x*y) - break - else: - continue - break + if x-y in i: + return y*(x-y) + return None +print(f(t)) for x in i: - for y in i: - for z in i: - if x+y+z == 2020: - print(x*y*z) - break - else: - continue + y = f(t-x) + if y: + print(x*y) break - else: - continue - break