It outputs a json-style tree of all towers/programs that are not balanced.
You need to get the value of the last tower that is unbalanced, but has no unbalanced sub-towers.
To get a good visualization do the following:
- Clone this repository
- Replace the file
puzzle7.py
with this version - Run:
$ ./aoc 7 2 -v
Using the input in the repo you'll get this output:
"xegshds": # Top tower (solution of part 1)
{
"WEIGHT": 53,
"TOTAL_WEIGHT": 510014, # Also contains the WEIGHT of this tower
"upqkbq": "26244 / 101991", # 26244 is the own weight and 101991 the total with all sub towers/programs.
"hsdkmdv": "50286 / 101991", # These nodes are not objects because they are balanced.
"ggxgmci": {
"WEIGHT": 66063,
"TOTAL_WEIGHT": 101997,
"nsvan": "1749 / 5988",
"qcbfbfz": "2322 / 5988",
"anygv": {
"WEIGHT": 3678,
"TOTAL_WEIGHT": 5994,
"tghfe": "89 / 770",
"ybzqi": "395 / 770",
"fabacam": "305 / 776",
"UNBLANCED": true
},
"gefrwix": "99 / 5988",
"kkeafw": "1602 / 5988",
"jepbpw": "383 / 5988",
"UNBLANCED": true
},
"owjvny": "68748 / 101991",
"gqiczm": "61 / 101991",
"UNBLANCED": true
}
The root tower/program "xegshds" is of course unbalanced. But neither it, nor its unbalanced child are the problem. Its the tower/program that introduced the lack of balance. Following the nested towers you'll find "fabacam" to have a different total weight than its neighbors (776 instead of 770).
Remember: Since there is a total weight, this tower still has sub towers. But they are not the problem.
This gist is not supposed to tell you the answer or formula on how to solve it. It just wants to help you make your own solution and to give an idea on how I made mine. However if you insist the solution can be found here of course.