Attempt (and fail at) day 23
This commit is contained in:
parent
014793e0f1
commit
419bc694b2
8 changed files with 230 additions and 1 deletions
2
.github/README.md
vendored
2
.github/README.md
vendored
|
@ -38,7 +38,7 @@ Puzzle inputs and descriptions are not included in this repository. You'll have
|
|||
| [20](/20-jurassicJigsaw) | ![Partially complete][partial] | [Link](/20-jurassicJigsaw/python) | | Only part one solved |
|
||||
| [21](/21-allergenAmusement) | ![Partially complete][partial] | [Link](/21-allergenAmusement/python) | | |
|
||||
| [22](/22-crabCombat) | ![Partially complete][partial] | [Link](/22-crabCombat/python) | | |
|
||||
| 23 | ![Not yet attempted][pending] | | | |
|
||||
| [23](/23-crabCups) | ![Incomplete][cross] | | | |
|
||||
| 24 | | | | |
|
||||
| 25 | | | | |
|
||||
|
||||
|
|
17
23-crabCups/README.md
Normal file
17
23-crabCups/README.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# [Day 23: Crab Cups](https://adventofcode.com/2020/day/23)
|
||||
|
||||
The point at which I was unable to debug my solution because the sample solution didn't extend to 100 iterations was the point at which I gave up.
|
||||
|
||||
This challenge seemed so simple, yet somehow managed to break me. Oh well. Fingers crossed the final two days are more doable.
|
||||
|
||||
Here is a crab in place of anything actually useful written here.
|
||||
|
||||

|
||||
|
||||
<details><summary>Script output</summary>
|
||||
|
||||
```
|
||||
There is none.
|
||||
```
|
||||
|
||||
</details>
|
BIN
23-crabCups/crab.jpg
Normal file
BIN
23-crabCups/crab.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 175 KiB |
14
23-crabCups/info.json
Normal file
14
23-crabCups/info.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"year": "2020",
|
||||
"day": "23",
|
||||
"title": "Crab Cups",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "389125467",
|
||||
"expected": 67384529
|
||||
}
|
||||
],
|
||||
"two": []
|
||||
}
|
||||
}
|
116
23-crabCups/python/__main__.py
Normal file
116
23-crabCups/python/__main__.py
Normal file
|
@ -0,0 +1,116 @@
|
|||
import json
|
||||
import platform
|
||||
import sys
|
||||
import time
|
||||
|
||||
from rich import print
|
||||
|
||||
from partOne import partOne
|
||||
from partTwo import partTwo
|
||||
|
||||
|
||||
force_time = False
|
||||
|
||||
|
||||
def run_and_time(f):
|
||||
st = time.time()
|
||||
x = f()
|
||||
et = time.time()
|
||||
return x, et-st
|
||||
|
||||
|
||||
def run_tests(test_cases):
|
||||
do_tests = True
|
||||
if len(test_cases) == 0:
|
||||
do_tests = False
|
||||
elif len(test_cases["one"]) == 0 and len(test_cases["two"]) == 0:
|
||||
do_tests = False
|
||||
|
||||
if not do_tests:
|
||||
print("Info: no test cases specified. Skipping tests\n")
|
||||
return
|
||||
|
||||
print("Test cases")
|
||||
|
||||
def rt(tcs, f, n):
|
||||
for i, tc in enumerate(tcs):
|
||||
readable_test_num = f"{n}.{i+1}"
|
||||
print(f"Running {readable_test_num}...", end="\r")
|
||||
|
||||
expectedInt = tc["expected"]
|
||||
|
||||
result, t = run_and_time(lambda: f(str(tc["input"])))
|
||||
|
||||
output_string = f"{readable_test_num} "
|
||||
|
||||
if result == expectedInt:
|
||||
output_string += "[green]pass[/green]"
|
||||
else:
|
||||
output_string += f"[red]fail[/red] (got {result}, expected {expectedInt})"
|
||||
|
||||
if t > 15 or force_time:
|
||||
output_string += f" in {t} seconds"
|
||||
|
||||
print(output_string + " "*12)
|
||||
|
||||
rt(test_cases["one"], partOne, 1)
|
||||
rt(test_cases["two"], partTwo, 2)
|
||||
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
info = open("info.json").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open info.json")
|
||||
sys.exit(-1)
|
||||
|
||||
info = json.loads(info)
|
||||
|
||||
year = info["year"]
|
||||
day = info["day"]
|
||||
title = info["title"]
|
||||
|
||||
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
|
||||
print(f"Python {platform.python_version()}\n")
|
||||
|
||||
try:
|
||||
challenge_input = open("input.txt").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open input.txt")
|
||||
sys.exit(-1)
|
||||
|
||||
if "vis" in sys.argv:
|
||||
import visualise
|
||||
|
||||
print("[green]Running visualisation....[/green]")
|
||||
|
||||
visualise.visualise(challenge_input)
|
||||
sys.exit()
|
||||
|
||||
if "ft" in sys.argv:
|
||||
force_time = True
|
||||
|
||||
run_tests(info["testCases"])
|
||||
|
||||
if "debug" in sys.argv:
|
||||
sys.exit()
|
||||
|
||||
print("Answers")
|
||||
|
||||
print("Running part 1...", end="\r")
|
||||
output_string = "Part 1: "
|
||||
x, t = run_and_time(lambda: partOne(challenge_input))
|
||||
output_string += str(x)
|
||||
if t > 15 or force_time:
|
||||
output_string += f" in {t} seconds"
|
||||
print(output_string + " "*12)
|
||||
|
||||
print("Running part 2...", end="\r")
|
||||
output_string = "Part 2: "
|
||||
x, t = run_and_time(lambda: partTwo(challenge_input))
|
||||
output_string += str(x)
|
||||
if t > 15 or force_time:
|
||||
output_string += f" in {t} seconds"
|
||||
print(output_string + " "*12)
|
5
23-crabCups/python/common.py
Normal file
5
23-crabCups/python/common.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from typing import List
|
||||
|
||||
|
||||
def parse(instr: str) -> List[int]:
|
||||
return [int(x) for x in instr.strip()]
|
71
23-crabCups/python/partOne.py
Normal file
71
23-crabCups/python/partOne.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
from common import *
|
||||
|
||||
|
||||
def partOne(instr: str) -> int:
|
||||
cups = parse(instr)
|
||||
|
||||
total_cups = len(cups)
|
||||
|
||||
current_cup = 0
|
||||
current_cup_val = cups[0]
|
||||
for i in range(100):
|
||||
|
||||
print(i + 1, cups)
|
||||
|
||||
# pick up next three cups
|
||||
next_three = []
|
||||
print(" ", end="")
|
||||
for i in range(3):
|
||||
ni = current_cup + 1 + i
|
||||
if ni >= len(cups):
|
||||
ni -= len(cups)
|
||||
print(ni, cups[ni], end=", ")
|
||||
next_three.append(cups[ni])
|
||||
print()
|
||||
print(" next three ", next_three)
|
||||
|
||||
for rm in next_three:
|
||||
cups.remove(rm)
|
||||
|
||||
# select destination cup
|
||||
destination_cup_val = current_cup_val - 1
|
||||
while destination_cup_val in next_three or destination_cup_val not in cups:
|
||||
|
||||
print(" before dv", destination_cup_val)
|
||||
|
||||
destination_cup_val -= 1
|
||||
if destination_cup_val <= min(cups + next_three):
|
||||
destination_cup_val += 1 + max(cups + next_three)
|
||||
print(" looping dv to", destination_cup_val)
|
||||
|
||||
print(" after dv", destination_cup_val, max(cups + next_three))
|
||||
destination_cup = cups.index(destination_cup_val)
|
||||
|
||||
print(" destination", destination_cup_val)
|
||||
|
||||
# insert cups immediately clockwise of destination
|
||||
for v in next_three[::-1]:
|
||||
cups.insert(destination_cup + 1, v)
|
||||
|
||||
# rotate the list so the current cup is in the same place it started at
|
||||
while current_cup_val != cups[current_cup]:
|
||||
t = cups.pop(0)
|
||||
cups.append(t)
|
||||
|
||||
# select new current cup
|
||||
current_cup += 1
|
||||
if current_cup >= total_cups:
|
||||
current_cup -= total_cups
|
||||
current_cup_val = cups[current_cup]
|
||||
|
||||
print()
|
||||
|
||||
|
||||
# rotate until 1 is the first value
|
||||
while cups[0] != 1:
|
||||
t = cups.pop(0)
|
||||
cups.append(t)
|
||||
|
||||
print(cups)
|
||||
|
||||
return int("".join([str(x) for x in cups[1:]]))
|
6
23-crabCups/python/partTwo.py
Normal file
6
23-crabCups/python/partTwo.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from common import *
|
||||
|
||||
|
||||
def partTwo(instr: str) -> int:
|
||||
input_list = parse(instr)
|
||||
return 0
|
Loading…
Add table
Add a link
Reference in a new issue