Skip to content

Commit

Permalink
solve 2021 day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
KristobalJunta committed Dec 7, 2021
1 parent fb1ffd5 commit 07199de
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2021/d07/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,436,430,89,4,797,118,1591,1350,376,737,57,653,729,863,647,9,109,388,220,686,334,739,147,750,1009,657,86,162,275,1315,148,27,1076,153,780,67,1025,898,584,377,67,125,225,50,8,599,145,724,15,594,64,1050,16,452,3,852,17,32,72,721,1390,1043,28,27,709,463,113,146,1148,45,258,13,1025,50,97,1033,220,94,414,306,609,258,1080,331,1241,365,612,90,213,845,1234,1466,811,42,1682,340,212,283,3,186,20,1048,363,200,830,73,26,1155,517,102,136,481,437,967,30,26,508,95,124,124,528,154,314,959,22,42,30,462,190,1243,624,355,377,174,530,58,8,363,1098,791,70,23,1634,99,85,172,83,654,1761,155,665,458,755,478,424,301,353,342,333,520,117,616,397,1148,271,311,37,5,885,143,862,576,234,683,685,150,593,10,480,298,297,500,45,1045,5,12,105,85,14,303,851,189,246,18,727,347,580,72,933,736,51,452,393,292,457,950,1027,55,818,734,298,98,38,153,173,717,189,664,627,943,411,189,68,350,676,74,162,727,224,1160,51,14,1160,18,1092,99,983,238,519,113,89,863,1290,85,47,66,553,155,11,828,29,901,140,434,234,647,756,341,16,1004,707,2,1001,185,149,799,189,659,1134,463,355,84,1341,750,1479,717,108,188,24,519,779,732,201,452,118,5,22,1111,175,53,345,704,119,406,80,216,922,560,639,284,932,624,636,31,524,1827,1670,553,590,743,1187,436,127,283,343,298,186,74,291,440,154,1782,355,15,269,114,253,615,357,357,437,1439,638,524,509,112,509,170,83,1693,808,635,1038,703,879,1048,1158,1095,96,56,1004,1046,33,1,945,121,16,327,599,394,130,219,472,920,97,288,1688,355,223,92,133,645,626,154,531,824,103,1148,136,417,364,271,705,293,1789,1671,575,860,10,1827,661,52,108,265,126,165,1096,1345,157,1559,103,372,257,1758,1,1277,18,146,257,57,992,567,1507,227,197,340,641,5,717,969,529,336,1388,71,397,122,146,1011,341,169,721,247,1140,406,16,473,456,136,384,43,648,100,155,230,397,614,563,371,341,79,318,41,910,1067,908,426,1423,186,67,1067,485,1065,179,98,76,336,1018,142,1181,31,714,193,33,376,131,272,905,1104,97,606,93,118,267,1760,1052,29,293,186,243,1098,875,123,773,272,522,179,663,238,110,1083,1586,5,586,138,331,79,44,306,389,402,992,617,477,137,738,585,837,86,731,29,572,42,52,827,459,956,34,527,590,369,409,69,218,347,457,803,428,40,916,806,251,802,1066,152,685,657,230,502,19,630,67,786,777,880,257,415,1004,47,273,257,143,284,456,244,137,251,779,159,486,217,396,438,12,1009,441,56,89,489,449,2,205,131,353,84,78,1319,95,665,202,165,1025,1015,662,165,148,507,142,18,815,999,32,64,439,16,743,132,257,544,705,0,152,792,721,416,377,648,333,43,194,519,192,378,506,272,615,77,775,1647,1061,457,77,114,950,27,351,76,211,1321,197,497,685,6,53,262,746,850,782,323,615,413,422,179,12,154,120,1735,317,917,199,220,876,21,357,536,30,1048,84,627,864,909,261,206,87,545,252,462,306,251,1913,1017,256,406,280,463,394,143,186,557,820,969,708,429,72,191,213,656,161,9,109,999,400,32,458,174,903,25,106,322,37,97,1126,641,851,508,394,86,241,287,4,44,564,184,1122,288,535,808,287,220,313,1427,85,372,163,205,267,340,867,54,43,613,107,1050,213,176,792,394,172,54,770,1839,88,1732,211,1882,635,300,1319,226,669,205,1277,10,1519,275,1106,566,293,1161,45,543,114,381,1336,581,6,371,75,647,963,179,521,121,275,352,19,7,237,234,905,340,1032,415,116,56,132,1270,223,172,126,172,911,849,89,764,690,349,767,312,119,279,22,66,54,398,1000,615,38,403,8,111,615,99,907,1251,1521,1177,84,675,1003,301,191,346,182,119,1388,999,939,354,123,449,252,531,702,124,79,161,20,657,95,879,1646,382,27,608,282,262,329,4,868,401,422,403,52,206,882,431,1709,1198,893,174,366,2,176,210,149,873,1371,505,582,691,45,373,577,236,124,619,77,48,686,923,78,374,956,1191,311,211,361,1139,156,314,240,106,1728,71,1109,170,29,58,65,110,1612,280,62,942,1322,75,755,1289,606,377,1434,241,49,372,222,526,163,441,175,20,401,38,137,48,61
46 changes: 46 additions & 0 deletions 2021/d07/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fileinput
import math
import functools


def get_input():
data = [s.strip() for s in fileinput.input()]
data = list(map(int, data.pop(0).split(',')))
return data


@functools.lru_cache
def prog_sum(n, a1, an):
return int(n * (a1 + an) / 2)


def fuel_cost(origin, dest):
return abs(dest - origin)


def fuel_cost2(origin, dest):
n = abs(dest - origin)
return prog_sum(n, 1, n)


def find_best(data, cost_func):
fuel = math.inf
bestpos = -1

# goal is to minimize fuel value which is function from position
# brute force solution for finding best postition doesn't seem optimal

for pos in range(data[0], data[-1] + 1):
curval = sum(cost_func(x, pos) for x in data)

if curval < fuel:
bestpos = pos
fuel = curval

return fuel, bestpos


if __name__ == '__main__':
data = sorted(get_input())
print(find_best(data, fuel_cost))
print(find_best(data, fuel_cost2))
1 change: 1 addition & 0 deletions 2021/d07/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16,1,2,0,4,2,7,1,2,14

0 comments on commit 07199de

Please sign in to comment.