2020 AoC, Days 1-4
This commit is contained in:
commit
ad04dffb69
9 changed files with 2782 additions and 0 deletions
76
2020/Day 1/Solution.py
Normal file
76
2020/Day 1/Solution.py
Normal file
|
@ -0,0 +1,76 @@
|
|||
target = 2020
|
||||
input_file = "/tmp/aocinput"
|
||||
|
||||
def find_sum_of_two():
|
||||
large_n = []
|
||||
small_n = []
|
||||
|
||||
with open(input_file) as file:
|
||||
line = file.readline()
|
||||
while line and line != "\n":
|
||||
halftarget_count = 0
|
||||
|
||||
number = int(line)
|
||||
if number > target/2:
|
||||
large_n.append(number)
|
||||
elif number == target/2:
|
||||
halftarget_count += 1
|
||||
else:
|
||||
small_n.append(number)
|
||||
|
||||
if halftarget_count == 2:
|
||||
print(f"Found a sum : {target/2}*2 = {target}\nAnswer is {(target/2)**2}")
|
||||
break
|
||||
|
||||
line = file.readline()
|
||||
|
||||
for big_one in large_n:
|
||||
complement = target-big_one
|
||||
if complement in small_n:
|
||||
print(f"Found a sum : {big_one}+{complement} = {target}\nAnswer is {big_one*complement}")
|
||||
return
|
||||
|
||||
print("No sum found that can reach {target}")
|
||||
|
||||
def sum_search(target_sum, inputs):
|
||||
lower = []
|
||||
upper = []
|
||||
|
||||
for num in inputs:
|
||||
if num < target_sum/2:
|
||||
lower.append(num)
|
||||
else:
|
||||
upper.append(num)
|
||||
|
||||
search_list = lower if len(lower) < len(upper) else upper
|
||||
iter_list = upper if len(lower) < len(upper) else lower
|
||||
|
||||
for num in iter_list:
|
||||
if target_sum - num in search_list:
|
||||
return num, target_sum-num
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def find_sum_of_three():
|
||||
data = []
|
||||
subtracted_to_target = []
|
||||
with open(input_file) as file:
|
||||
line = file.readline()
|
||||
while line and line != "\n":
|
||||
data.append(int(line))
|
||||
subtracted_to_target.append((target-data[-1],data[-1]))
|
||||
|
||||
line = file.readline()
|
||||
|
||||
data.sort()
|
||||
|
||||
for sub_target in subtracted_to_target:
|
||||
result = sum_search(sub_target[0],data)
|
||||
if result:
|
||||
print(f"Sum found : {result[0]}+{result[1]}+{sub_target[1]} = {target}\nResult is {result[0]*result[1]*sub_target[1]}")
|
||||
return
|
||||
|
||||
print("No sum found")
|
||||
|
||||
|
200
2020/Day 1/input.txt
Normal file
200
2020/Day 1/input.txt
Normal file
|
@ -0,0 +1,200 @@
|
|||
1322
|
||||
1211
|
||||
1427
|
||||
1428
|
||||
1953
|
||||
1220
|
||||
1629
|
||||
1186
|
||||
1354
|
||||
1776
|
||||
1906
|
||||
1849
|
||||
1327
|
||||
1423
|
||||
401
|
||||
1806
|
||||
1239
|
||||
1934
|
||||
1256
|
||||
1223
|
||||
1504
|
||||
1365
|
||||
1653
|
||||
1706
|
||||
1465
|
||||
1810
|
||||
1089
|
||||
1447
|
||||
1983
|
||||
1505
|
||||
1763
|
||||
1590
|
||||
1843
|
||||
1534
|
||||
1886
|
||||
1842
|
||||
1878
|
||||
1785
|
||||
1121
|
||||
1857
|
||||
1496
|
||||
1696
|
||||
1863
|
||||
1944
|
||||
1692
|
||||
1255
|
||||
1572
|
||||
1767
|
||||
1509
|
||||
1845
|
||||
1479
|
||||
1935
|
||||
1507
|
||||
1852
|
||||
1193
|
||||
1797
|
||||
1573
|
||||
1317
|
||||
1266
|
||||
1707
|
||||
1819
|
||||
925
|
||||
1976
|
||||
1908
|
||||
1571
|
||||
1646
|
||||
1625
|
||||
1719
|
||||
1980
|
||||
1970
|
||||
1566
|
||||
1679
|
||||
1484
|
||||
1818
|
||||
1985
|
||||
1794
|
||||
1699
|
||||
1530
|
||||
1645
|
||||
370
|
||||
1658
|
||||
1345
|
||||
1730
|
||||
1340
|
||||
1281
|
||||
1722
|
||||
1623
|
||||
1148
|
||||
1545
|
||||
1728
|
||||
1325
|
||||
1164
|
||||
1462
|
||||
1893
|
||||
1736
|
||||
160
|
||||
1543
|
||||
1371
|
||||
1930
|
||||
1162
|
||||
2010
|
||||
1302
|
||||
1967
|
||||
1889
|
||||
1547
|
||||
1335
|
||||
1416
|
||||
1359
|
||||
1622
|
||||
1682
|
||||
1701
|
||||
1939
|
||||
1697
|
||||
1436
|
||||
1367
|
||||
1119
|
||||
1741
|
||||
1466
|
||||
1997
|
||||
1856
|
||||
1824
|
||||
1323
|
||||
1478
|
||||
1963
|
||||
1832
|
||||
1748
|
||||
1260
|
||||
1244
|
||||
1834
|
||||
1990
|
||||
1567
|
||||
1147
|
||||
1588
|
||||
1694
|
||||
1487
|
||||
1151
|
||||
1347
|
||||
1315
|
||||
1502
|
||||
546
|
||||
730
|
||||
1742
|
||||
1869
|
||||
1277
|
||||
1224
|
||||
1169
|
||||
1708
|
||||
1661
|
||||
174
|
||||
1207
|
||||
1801
|
||||
1880
|
||||
1390
|
||||
1747
|
||||
1215
|
||||
1684
|
||||
1498
|
||||
1965
|
||||
1933
|
||||
1693
|
||||
1129
|
||||
1578
|
||||
1189
|
||||
1251
|
||||
1727
|
||||
1440
|
||||
1178
|
||||
746
|
||||
1564
|
||||
944
|
||||
1822
|
||||
1225
|
||||
1523
|
||||
1575
|
||||
1185
|
||||
37
|
||||
1866
|
||||
1766
|
||||
1737
|
||||
1800
|
||||
1633
|
||||
1796
|
||||
1161
|
||||
1932
|
||||
1583
|
||||
1395
|
||||
1288
|
||||
1991
|
||||
229
|
||||
1875
|
||||
1540
|
||||
1876
|
||||
1191
|
||||
1858
|
||||
1713
|
||||
1725
|
||||
1955
|
||||
1250
|
||||
1987
|
||||
1724
|
Loading…
Add table
Add a link
Reference in a new issue