day7 & day8
This commit is contained in:
parent
9f5ce69cf7
commit
1cc130df8d
33
day7/day7.py
Normal file
33
day7/day7.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
def part1(crabs):
|
||||||
|
moves = OrderedDict()
|
||||||
|
for pos in range(min(crabs), max(crabs) + 1):
|
||||||
|
# calculate total fuel required to move to pos:
|
||||||
|
for crab in crabs:
|
||||||
|
fuel_cost = abs(pos - crab)
|
||||||
|
moves[pos] = moves.get(pos, 0) + fuel_cost
|
||||||
|
|
||||||
|
min_move = min(moves, key=moves.get)
|
||||||
|
print(f"Part 1, min move {min_move}, cost {moves[min_move]}")
|
||||||
|
|
||||||
|
def part2(crabs):
|
||||||
|
moves = OrderedDict()
|
||||||
|
for pos in range(min(crabs), max(crabs) + 1):
|
||||||
|
# calculate total fuel required to move to pos:
|
||||||
|
for crab in crabs:
|
||||||
|
dx = abs(pos - crab)
|
||||||
|
# S = (n+1)(u0 + un)/2
|
||||||
|
fuel_cost = dx * (dx+1)//2
|
||||||
|
moves[pos] = moves.get(pos, 0) + fuel_cost
|
||||||
|
|
||||||
|
min_move = min(moves, key=moves.get)
|
||||||
|
print(f"Part 1, min move {min_move}, cost {moves[min_move]}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("input.txt") as infile:
|
||||||
|
crabs = [int(x) for x in infile.readline().split(",")]
|
||||||
|
part1(crabs)
|
||||||
|
part2(crabs)
|
||||||
|
|
1
day7/input.txt
Normal file
1
day7/input.txt
Normal file
@ -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,76,50,481,307,723,1100,235,147,851,504,1068,780,490,126,771,831,358,711,68,281,645,644,862,705,372,217,679,1097,1005,728,1739,571,40,1293,782,378,802,245,1370,1512,9,372,448,64,36,1212,141,585,1468,278,286,476,239,285,664,277,43,665,1037,654,205,1238,36,41,276,945,624,47,91,1569,284,107,845,60,961,30,21,269,1091,304,364,339,169,430,176,156,1483,1510,111,146,447,590,1227,611,483,428,396,839,307,901,380,128,80,535,461,482,379,59,281,977,44,966,545,37,163,845,845,151,936,269,938,612,1284,863,516,927,511,825,125,599,101,394,1062,140,483,218,83,443,404,492,78,507,860,1741,43,128,60,566,181,5,554,937,163,280,655,184,480,408,127,935,613,766,46,312,770,697,275,13,84,600,168,83,281,599,607,1441,197,344,0,302,414,147,370,748,421,844,871,319,666,117,640,247,167,529,324,252,235,303,443,1305,796,109,20,364,532,1388,708,769,916,340,405,90,47,504,516,97,535,28,69,960,590,254,106,188,190,1388,698,246,264,98,229,1648,292,710,14,421,31,147,1493,552,1371,454,4,146,674,452,1267,1027,170,141,936,1341,884,558,276,631,68,39,2,464,1,839,318,881,413,2,452,352,34,89,323,884,1439,243,79,56,128,1273,1134,606,11,682,747,415,599,782,179,269,320,682,177,336,466,10,370,159,1636,367,888,573,171,682,60,9,59,332,10,1496,637,1029,413,186,1183,77,309,461,883,1079,699,233,69,259,108,1160,435,480,495,13,858,718,126,115,728,1008,133,442,7,598,1475,1156,226,162,415,3,151,72,527,792,494,763,144,64,490,273,1245,300,465,744,36,1465,251,8,494,1126,362,180,1263,175,141,1041,103,163,205,568,93,699,103,437,204,931,563,550,88,415,146,265,31,221,1123,835,375,1101,578,388,92,1417,845,308,343,499,158,293,242,4,509,574,254,1556,69,668,691,0,558,16,687,1210,166,748,400,863,66,600,771,1073,561,738,398,384,232,350,393,1113,1222,153,462,907,797,712,18,1463,1185,1055,994,57,130,265,131,52,463,902,453,38,132,783,1560,232,169,1162,173,311,5,1477,397,336,480,540,491,67,340,27,291,341,35,275,78,1525,387,218,63,79,533,4,569,1643,595,1508,851,39,1200,912,10,53,42,60,154,1174,155,275,137,677,367,1373,4,708,441,756,647,1054,872,1039,109,530,1179,939,429,567,866,1411,436,23,212,184,66,79,831,538,90,827,678,549,313,434,60,907,284,171,570,1091,603,447,122,1092,29,789,563,462,15,310,340,16,365,393,614,48,368,42,457,736,737,1008,513,61,764,366,400,525,1683,1177,909,908,112,734,16,79,917,541,127,107,79,1208,32,258,596,166,376,1313,735,1457,864,563,55,439,54,694,81,93,48,470,1028,689,1177,1331,155,412,847,250,405,387,8,456,18,619,533,729,1475,1182,935,210,55,355,958,15,32,598,85,175,471,1087,280,652,53,13,225,12,488,717,353,2,134,351,698,276,456,209,535,604,19,12,785,3,63,879,437,216,1,1275,811,786,417,33,51,733,1074,143,309,65,555,557,78,611,909,260,973,701,998,490,213,9,233,760,933,916,437,1369,1952,372,324,859,670,73,296,1391,127,407,230,52,16,547,803,883,258,308,710,343,1290,184,8,41,9,68,104,175,1034,1544,219,752,327,690,134,601,1574,214,385,1233,231,267,944,1533,349,431,97,632,278,1505,162,888,62,90,489,351,990,846,14,159,134,14,314,148,214,1153,513,114,6,49,10,14,957,219,16,204,954,863,50,482,90,696,99,253,252,433,57,385,54,343,106,154,78,1595,590,380,102,825,1933,191,1328,374,263,355,137,494,60,781,1113,391,274,325,326,14,965,269,15,32,742,81,393,730,892,982,103,890,499,58,816,292,29,480,173,831,132,1033,1511,1137,1511,22,1105,146,344,308,915,540,1371,1238,414,352,304,841,749,6,491,30,1322,415,293,1207,31,90,636,303,1551,354,23,275,18,32,623,1483,49,12,311,407,1551,296,252,647,778,1499,98,1220,264,1020,1440,377,1125,8,72,270,162,348,3,1023,965,719,62,1467,1176,663,439,557,654,85,1493,70,349,10,727,15,1173,387,529,608,1398,905,619,173,849,1493,49,88,4,708,1084,370,1007,285,4,530,770,561,26,669,1100,30,876,649,178,32,354,621,911,334,514,9,449,1019,107
|
46
day8/day8.py
Normal file
46
day8/day8.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
def part1(inp):
|
||||||
|
total = 0
|
||||||
|
for display in inp:
|
||||||
|
_, output_values = display
|
||||||
|
for value in output_values:
|
||||||
|
if len(value) in [2, 4, 3, 7]:
|
||||||
|
total += 1
|
||||||
|
print(f"Part 1 : {total}")
|
||||||
|
|
||||||
|
|
||||||
|
def part2(inp):
|
||||||
|
for display in inp:
|
||||||
|
patterns, values = display
|
||||||
|
patterns = sorted(patterns, key=lambda x: len(x))
|
||||||
|
print(patterns)
|
||||||
|
# easy
|
||||||
|
d1 = [x for x in patterns if len(x) == 2][0]
|
||||||
|
print("1", d1)
|
||||||
|
d4 = [x for x in patterns if len(x) == 4][0]
|
||||||
|
print("4", d4)
|
||||||
|
d7 = [x for x in patterns if len(x) == 3][0]
|
||||||
|
print("7", d7)
|
||||||
|
d8 = [x for x in patterns if len(x) == 7][0]
|
||||||
|
print("8", d8)
|
||||||
|
|
||||||
|
# 3 is the only digit that has all common segments with 1
|
||||||
|
breakpoint()
|
||||||
|
d3 = [x for x in patterns if set(d1).issubset(set(x)) and len(x) == 5][0]
|
||||||
|
print("3", d3)
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
|
def main(infile):
|
||||||
|
inp = []
|
||||||
|
with open(infile) as f:
|
||||||
|
for display in f:
|
||||||
|
display = display.rstrip().split(" | ")
|
||||||
|
signal_patterns = display[0].split(" ")
|
||||||
|
output_values = display[1].split(" ")
|
||||||
|
inp.append([signal_patterns, output_values])
|
||||||
|
part1(inp)
|
||||||
|
part2(inp)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main("input.txt")
|
1
day8/input.txt
Normal file
1
day8/input.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf
|
Loading…
Reference in New Issue
Block a user