Day 1 (part 2 linear runtime)
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="Python 3.6" project-jdk-type="Python SDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="Python 3.8" project-jdk-type="Python SDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,14 +1,33 @@
|
|||||||
import math
|
import math
|
||||||
from itertools import combinations
|
|
||||||
|
|
||||||
items = [int(x.strip()) for x in open("input.txt")]
|
items = (int(x.strip()) for x in open("input.txt"))
|
||||||
|
values = {(2020, 3)}
|
||||||
|
tree = dict()
|
||||||
|
|
||||||
COUNT = 3
|
|
||||||
|
|
||||||
for vals in combinations(items, COUNT):
|
def tree_up(idx, step=1, offset = 0):
|
||||||
if sum(vals) == 2020:
|
print(idx, step)
|
||||||
|
next = tree[(idx, step)] if (idx, step) in tree else None
|
||||||
|
appendix = [] if next is None else tree_up(next+idx, step=step+1, offset=idx)
|
||||||
|
return [idx - offset] + appendix
|
||||||
|
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
tmp_values = set(values)
|
||||||
|
for elem in values:
|
||||||
|
new = (elem[0] - item, elem[1]-1)
|
||||||
|
if new == (0, 0):
|
||||||
|
vals = tree_up(item)
|
||||||
print("Found [{}]: Sum={}, Product={}".format(
|
print("Found [{}]: Sum={}, Product={}".format(
|
||||||
", ".join(map(str, vals)),
|
", ".join(map(str, vals)),
|
||||||
sum(vals),
|
sum(vals),
|
||||||
math.prod(vals)
|
math.prod(vals)
|
||||||
))
|
))
|
||||||
|
exit(0)
|
||||||
|
elif new[0] < 0 or new[1] < 0:
|
||||||
|
# target value too low or to many sums
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
tree[new] = item
|
||||||
|
tmp_values.add(new)
|
||||||
|
values = tmp_values
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<module type="WEB_MODULE" version="4">
|
<module type="WEB_MODULE" version="4">
|
||||||
<component name="FacetManager">
|
<component name="FacetManager">
|
||||||
<facet type="Python" name="Python">
|
<facet type="Python" name="Python">
|
||||||
<configuration sdkName="Python 3.6" />
|
<configuration sdkName="Python 3.8" />
|
||||||
</facet>
|
</facet>
|
||||||
</component>
|
</component>
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
@@ -11,6 +11,6 @@
|
|||||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="Python 3.6 interpreter library" level="application" />
|
<orderEntry type="library" name="Python 3.8 interpreter library" level="application" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
Reference in New Issue
Block a user