From 9d38fa31d7ef61521a69578cf5b26bbf60e4b3e6 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Wed, 11 Dec 2024 23:40:08 +0100 Subject: [PATCH] Solved 2015/13 P1+P2 --- 2015/13/13.md | 80 +++++++++++++++++++++++++++++++ 2015/13/solution.py | 76 +++++++++++++++++++++++++++++ 2024/11/Sol.py | 45 +++++++++++++++++ __pycache__/fred.cpython-311.pyc | Bin 25507 -> 25606 bytes fred.py | 7 ++- 5 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 2015/13/13.md create mode 100644 2015/13/solution.py create mode 100644 2024/11/Sol.py diff --git a/2015/13/13.md b/2015/13/13.md new file mode 100644 index 0000000..0e6e669 --- /dev/null +++ b/2015/13/13.md @@ -0,0 +1,80 @@ +## \-\-- Day 13: Knights of the Dinner Table \-\-- + +In years past, the holiday feast with your family hasn\'t gone so well. +Not everyone gets along! This year, you resolve, will be different. +You\'re going to find the *optimal seating arrangement* and avoid all +those awkward conversations. + +You start by writing up a list of everyone invited and the amount their +happiness would increase or decrease if they were to find themselves +sitting next to each other person. You have a circular table that will +be just big enough to fit everyone comfortably, and so each person will +have exactly two neighbors. + +For example, suppose you have only four attendees planned, and you +calculate +their potential happiness as follows: + + Alice would gain 54 happiness units by sitting next to Bob. + Alice would lose 79 happiness units by sitting next to Carol. + Alice would lose 2 happiness units by sitting next to David. + Bob would gain 83 happiness units by sitting next to Alice. + Bob would lose 7 happiness units by sitting next to Carol. + Bob would lose 63 happiness units by sitting next to David. + Carol would lose 62 happiness units by sitting next to Alice. + Carol would gain 60 happiness units by sitting next to Bob. + Carol would gain 55 happiness units by sitting next to David. + David would gain 46 happiness units by sitting next to Alice. + David would lose 7 happiness units by sitting next to Bob. + David would gain 41 happiness units by sitting next to Carol. + +Then, if you seat Alice next to David, Alice would lose `2` happiness +units (because David talks so much), but David would gain `46` happiness +units (because Alice is such a good listener), for a total change of +`44`. + +If you continue around the table, you could then seat Bob next to Alice +(Bob gains `83`, Alice gains `54`). Finally, seat Carol, who sits next +to Bob (Carol gains `60`, Bob loses `7`) and David (Carol gains `55`, +David gains `41`). The arrangement looks like this: + + +41 +46 + +55 David -2 + Carol Alice + +60 Bob +54 + -7 +83 + +After trying every other seating arrangement in this hypothetical +scenario, you find that this one is the most optimal, with a total +change in happiness of `330`. + +What is the *total change in happiness* for the optimal seating +arrangement of the actual guest list? + +Your puzzle answer was `733`. + +## \-\-- Part Two \-\-- {#part2} + +In all the commotion, you realize that you forgot to seat yourself. At +this point, you\'re pretty apathetic toward the whole thing, and your +happiness wouldn\'t really go up or down regardless of who you sit next +to. You assume everyone else would be just as ambivalent about sitting +next to you, too. + +So, add yourself to the list, and give all happiness relationships that +involve you a score of `0`. + +What is the *total change in happiness* for the optimal seating +arrangement that actually includes yourself? + +Your puzzle answer was `725`. + +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2015) and +try another puzzle. + +If you still want to see it, you can [get your puzzle +input](13/input). + diff --git a/2015/13/solution.py b/2015/13/solution.py new file mode 100644 index 0000000..a0d66d1 --- /dev/null +++ b/2015/13/solution.py @@ -0,0 +1,76 @@ +#!/bin/python3 +import sys,time,re +from pprint import pprint +sys.path.insert(0, '../../') +from fred import list2int,get_re,nprint,lprint,loadFile,TSP +start_time = time.time() + +input_f = 'input' + +######################################### +# # +# Part 1 # +# # +######################################### + +def parseInput(): + + inst = [] + graph = {} + with open(input_f) as file: + for line in file: + inst = line.rstrip().split(' happiness units by sitting next to ') + t = inst[0].split(' would ') + inst = [t[0]] + [inst[1].replace('.','')] + [int(t[1].replace('lose ','-').replace('gain ',''))] + + if inst[0] not in graph: + graph[inst[0]] = [] + #if inst[1] not in graph: + # graph[inst[1]] = [] + graph[inst[0]].append((inst[1],inst[2])) + #graph[inst[1]].append((inst[0],inst[2])) + + return graph + +def addBothWays(graph): + n_graph = {} + for _,a in enumerate(graph): + for b in graph[a]: + for c in graph[b[0]]: + if c[0] == a: + if a not in n_graph: + n_graph[a] = [] + n_graph[a].append((b[0],b[1]+c[1])) + + + return n_graph + +def part1(): + graph = addBothWays(parseInput()) + pprint(graph) + longest = 0 + people = set(graph.keys()) + for neighbors in graph.values(): + for neighbor, _ in neighbors: + people.add(neighbor) + for c in people: + route,distance = TSP(graph, c,'longest',True) + if longest < distance: + longest = distance + return longest + +start_time = time.time() +print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') + + +######################################### +# # +# Part 2 # +# # +######################################### +def part2(): + # Just add 'Myself' to the input file with gain of 0 towards everyone + return + +start_time = time.time() +print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') \ No newline at end of file diff --git a/2024/11/Sol.py b/2024/11/Sol.py new file mode 100644 index 0000000..02c2bca --- /dev/null +++ b/2024/11/Sol.py @@ -0,0 +1,45 @@ +from functools import cache + +def rule2str(number): + num_str = str(number) + length = len(num_str) + + middle = length // 2 + + left_part = num_str[:middle] + right_part = num_str[middle:] + + return [str(int(left_part)), str(int(right_part))] + +values = {} + +def calc(start,end): + if start == '0': + result = calculateNumber('1',end) + else: + if len(start) % 2 == 0: + x = rule2str(start) + result = 0 + result += calculateNumber(x[0],end) + result += calculateNumber(x[1],end) + + else: + result = calculateNumber(str(int(start)*2024),end) + return result + +def calculateNumber(start,end): + if end == 0: + return 1 + end -= 1 + if (start,end) not in values: + values[(start,end)] = calc(start,end) + result = values[(start,end)] + return result +numbers = ['125','17'] + +# 2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2 +result = 0 +for i in numbers: + result += calculateNumber(i,75) +print(result) + diff --git a/__pycache__/fred.cpython-311.pyc b/__pycache__/fred.cpython-311.pyc index f6fa3e8c5e23203571b37688532b8953434d5713..a00555043c65c5be6727bd6626b31d10db284747 100644 GIT binary patch delta 629 zcmZ2{oU!c$BkyuvUM>b8xFZym-n)@^4GXKGslK7{=6x)0e8gBlGEl$)q?v*EbI{}i zfjP`9H4Kw4dW%iw58{(>VThHlWvyWa(o!)@3=Fkwwd^%43s@$9loDlRo7@;A&d4%( zW{@^B%K}!Qn9AhWLEh{fDeSeJCH#|pv}IEd)N<7@H%innrZbiZ!YpK9sNq`1$iT1~ zh#{Z~NP*OV=vwYPVPe$Ra;I?A@}zLq@=op!mYmEJ!#{a*up}ejx5%-l76lfMRQ zDznxASt(o~b0r~Gfox(eV<-}>;i&=3PIe5D_vGpEP2sNPWn`$~Et08WN@2|g`j&yA zhz+QUBO9!*QA)4%*HJn9EH5{97hU78J z@)tRROj-^kZgCbQmSn`2R2HP(VoS~}O3s-a6OpXR50p|s1x3Oj<;)?$0Y!&^tVh6* zDwb!O{5;~Ftpuy$2L>R~!SaAx@B+8&4H3x+=5s?9sLYI-6LnEoeTC@?^^3xW9WEU% z574AKTsD7<%x7gi4>VGBb85UMBRf;kCLqUe@|1)H+#s8Z=7NaxlkF2_xh?~lpeQP~ Y+?SDH_tKjsWbRjDlxPW#a4o}2wGT_Gf&XaYUqIrrQm4_a_+*7 z%);H-Bseh;=%NVh?faocuEcSpE`(@X$b|dwJk7uZ=lA>0$31s-2i|@McguBc!8Wot zIn%lAzE{TJQ2)R{^hy1`-y&n=pIMya{;1c+K(ngo^e!fQ`SAiX~v;W0VEsD@X2hUM@ z<#%;BYYzfPr$JykY{x`k%z;^a44%Ejb3@F()>DD&u!#t znVzzW9zK3)7`wc2Bl`9<4@n3=azm%js!psER^qTrZ%Q3a`&`EVHh-I6TbBTENVQUT UbW*B8^9T&r#no_!4xaa)0nLnjvj6}9 diff --git a/fred.py b/fred.py index d4ad47e..275b5b4 100644 --- a/fred.py +++ b/fred.py @@ -479,7 +479,7 @@ def dijkstra(graph, start, end): return path, distances[path[-1]] -def TSP(graph, start, path_type='shortest'): +def TSP(graph, start, path_type='shortest',circle=None): """ Solves TSP (Traveling Salesperson Problem) using brute force by generating all possible paths. Handles missing edges by skipping invalid paths. @@ -517,7 +517,10 @@ def TSP(graph, start, path_type='shortest'): # Generate all permutations of the remaining cities for perm in permutations(cities): # Create a complete path starting and ending at the start node - path = [start] + list(perm) + if circle is not None: + path = [start] + list(perm) + [start] + else: + path = [start] + list(perm) # Calculate the total distance of this path total_distance = 0