Skip to content

Commit

Permalink
[2022][day 21] add part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IAjimi committed Dec 21, 2022
1 parent ec6fe3b commit 8a06c6a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
74 changes: 46 additions & 28 deletions 2022/AOC21.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,82 @@
import parse
from _utils import read_input, timer, Solution

ROOT = "root"

def process_input(filename: str):

def process_input(filename: str, part1: bool):
_input = read_input(filename)
known = {}
queue = []
for line in _input:
if "+" in line:
parsed_line = parse.parse("{monkey1}: {dep1} + {dep2}", line)
queue.append(
(parsed_line["monkey1"], "+", parsed_line["dep1"], parsed_line["dep2"])
)
elif "-" in line:
parsed_line = parse.parse("{monkey1}: {dep1} - {dep2}", line)
queue.append(
(parsed_line["monkey1"], "-", parsed_line["dep1"], parsed_line["dep2"])
)
elif "*" in line:
parsed_line = parse.parse("{monkey1}: {dep1} * {dep2}", line)
queue.append(
(parsed_line["monkey1"], "*", parsed_line["dep1"], parsed_line["dep2"])
)
elif "/" in line:
parsed_line = parse.parse("{monkey1}: {dep1} / {dep2}", line)
queue.append(
(parsed_line["monkey1"], "/", parsed_line["dep1"], parsed_line["dep2"])
)
else:
if any(c.isdigit() for c in line):
parsed_line = parse.parse("{monkey1}: {:d}", line)
known[parsed_line["monkey1"]] = parsed_line.fixed[0]

else:
parsed_line = parse.parse("{monkey1}: {dep1} {op} {dep2}", line)
if parsed_line["monkey1"] == ROOT and not part1:
op = "-"
else:
op = parsed_line["op"]
queue.append(
(
parsed_line["monkey1"],
op,
parsed_line["dep1"],
parsed_line["dep2"],
)
)
return queue, known


def process_monkeys(queue, known):
def process_monkeys(queue, known) -> int:
while queue:
monkey, op, dep1, dep2 = queue.pop(
0
) # need to pop at front bc appending at back
if dep1 in known and dep2 in known:
n1, n2 = known[dep1], known[dep2]
known[monkey] = eval(f"{n1} {op} {n2}")
if monkey == "root":
if monkey == ROOT:
return known[monkey]
else:
queue.append((monkey, op, dep1, dep2))

return 0


def get_guess_result(filename: str, guess: int) -> int:
queue, known = process_input(filename, part1=False)
known["humn"] = guess
return process_monkeys(queue, known)


def binary_search(filename: str, target: int = 0) -> int:
left = get_guess_result(filename, 0)
right = get_guess_result(filename, 10_000_000_000_000)

while True:
mid = (left + right) // 2
res = get_guess_result(filename, mid)

if res == target:
return mid
elif res < target:
left = mid + 1
else:
right = mid - 1


@timer
def main(filename: str) -> Solution:
queue, known = process_input(filename)
# TODO manually changed input so that root is - numbers
queue, known = process_input(filename, part1=True)
part_1_solution = process_monkeys(queue, known)
part_2_solution = 0
part_2_solution = binary_search(filename, target=0)
return part_1_solution, part_2_solution


if __name__ == "__main__":
part_1_solution, part_2_solution = main("aoc21.txt")
print(f"PART 1: {part_1_solution}") # 145167969204648
print(f"PART 2: {part_2_solution}") #
print(f"PART 2: {part_2_solution}") # 3330805295850
1 change: 1 addition & 0 deletions 2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ This repository contains Python solutions to the 2022 edition of [Advent of Code
| [18](https://adventofcode.com/2022/day/18) | **Boiling Boulders**: Exploring 3D grid | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC18.py) |
| [19](https://adventofcode.com/2022/day/19) | **Not Enough Minerals** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC19.py) |
| [20](https://adventofcode.com/2022/day/20) | **Grove Positioning System** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC20.py) |
| [21](https://adventofcode.com/2022/day/21) | **Monkey Math** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC21.py) |

0 comments on commit 8a06c6a

Please sign in to comment.