Day 7: Bridge Repair
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
Python
It is a tree search
def parse_input(path): with path.open("r") as fp: lines = fp.read().splitlines() roots = [int(line.split(':')[0]) for line in lines] node_lists = [[int(x) for x in line.split(':')[1][1:].split(' ')] for line in lines] return roots, node_lists def construct_tree(root, nodes, include_concat): levels = [[] for _ in range(len(nodes)+1)] levels[0] = [(str(root), "")] # level nodes are tuples of the form (val, operation) where both are str # val can be numerical or empty string # operation can be *, +, || or empty string for indl, level in enumerate(levels[1:], start=1): node = nodes[indl-1] for elem in levels[indl-1]: if elem[0]=='': continue if elem[0][-len(str(node)):] == str(node) and include_concat: levels[indl].append((elem[0][:-len(str(node))], "||")) if (a:=int(elem[0]))%(b:=int(node))==0: levels[indl].append((str(int(a/b)), '*')) if (a:=int(elem[0])) - (b:=int(node))>0: levels[indl].append((str(a - b), "+")) return levels[-1] def solve_problem(file_name, include_concat): roots, node_lists = parse_input(Path(cwd, file_name)) valid_roots = [] for root, nodes in zip(roots, node_lists): top = construct_tree(root, nodes[::-1], include_concat) if any((x[0]=='1' and x[1]=='*') or (x[0]=='0' and x[1]=='+') or (x[0]=='' and x[1]=='||') for x in top): valid_roots.append(root) return sum(valid_roots)
I asked ChatGPT to explain your code and mentioned you said it was a binary search. idk why, but it output a matter of fact response that claims you were wrong. lmao, I still don’t understand how your code works
I understand it now! I took your solution and made it faster. it is now like 36 milliseconds faster for me. which is interesting because if you look at the code. I dont process the entire list of integers. I sometimes stop prematurely before the next level, clear it, and add the root. I don’t know why but it just works for my input and the test input.
code
however what I notice is that the parse_input causes it to be the reason why it is slower by 20+ milliseconds. I find that even if I edited your solution like so to be slightly faster, it is still 10 milliseconds slower than mine:
code
Wow I got thrashed by chatgpt. Strictly speaking that is correct, it is more akin to Tree Search. But even then not strictly because in tree search you are searching through a list of objects that is known, you build a tree out of it and based on some conditions eliminate half of the remaining tree each time. Here I have some state space (as chatgpt claims!) and again based on applying certain conditions, I eliminate some portion of the search space successively (so I dont have to evaluate that part of the tree anymore). To me both are very similar in spirit as both methods avoid evaluating some function on all the possible inputs and successively chops off a fraction of the search space. To be more correct I will atleast replace it with tree search though, thanks. And thanks for taking a close look at my solution and improving it.
idk why my gpt decided to be like that. I expected a long winded response with a little bit of ai hallucinations. I was flabbergasted, and just had to post it.
I seemingly realized that working forward through the list of integers was inefficient for me to do, and I was using multiprocessing to do it too! which my old solution took less than 15 seconds for my input. your solution to reverse the operations and eliminate paths was brilliant and made it solve it in milliseconds without spinning up my fans, lol