mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
More efficiently pick items
This commit is contained in:
@@ -36,14 +36,22 @@ def send_command(computer: Computer, command: str):
|
|||||||
|
|
||||||
|
|
||||||
def force(computer: Computer, direction: str, items: Set[str]):
|
def force(computer: Computer, direction: str, items: Set[str]):
|
||||||
for combination in powerset(items):
|
# First, drop everything
|
||||||
# Drop everything, disregard what's on the ground
|
for item in items:
|
||||||
for item in items:
|
send_command(computer, f"drop {item}")
|
||||||
send_command(computer, f"drop {item}")
|
|
||||||
|
|
||||||
# Now pick up whatever we want to try
|
holding = tuple()
|
||||||
|
|
||||||
|
for combination in powerset(items):
|
||||||
|
# drop everything we don't want to keep holding
|
||||||
|
for item in holding:
|
||||||
|
if item not in combination:
|
||||||
|
send_command(computer, f"drop {item}")
|
||||||
|
|
||||||
|
# pick up what we want to pick up
|
||||||
for item in combination:
|
for item in combination:
|
||||||
send_command(computer, f"take {item}")
|
if item not in holding:
|
||||||
|
send_command(computer, f"take {item}")
|
||||||
|
|
||||||
send_command(computer, direction)
|
send_command(computer, direction)
|
||||||
|
|
||||||
@@ -53,6 +61,7 @@ def force(computer: Computer, direction: str, items: Set[str]):
|
|||||||
return True
|
return True
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print_output(computer)
|
print_output(computer)
|
||||||
|
holding = combination
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -107,7 +116,7 @@ def part1(data: TextIO):
|
|||||||
elif command.startswith("force "):
|
elif command.startswith("force "):
|
||||||
direction = command.removeprefix("force ")
|
direction = command.removeprefix("force ")
|
||||||
if force(computer, direction, items):
|
if force(computer, direction, items):
|
||||||
return
|
return "success"
|
||||||
continue
|
continue
|
||||||
|
|
||||||
send_command(computer, command)
|
send_command(computer, command)
|
||||||
|
|||||||
Reference in New Issue
Block a user