Skip to content

Commit

Permalink
script to evaluate day 5 problem paths
Browse files Browse the repository at this point in the history
  • Loading branch information
polo-diemunsch committed Apr 29, 2024
1 parent f6e9758 commit 674de19
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
18 changes: 18 additions & 0 deletions nix/evaluate-path/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ lib
, python3Packages
}:

python3Packages.buildPythonApplication rec {
pname = "evaluate-path";
version = "1.0.0";

format = "other";

src = ./.;

installPhase = ''
install -Dm755 $src/evaluate-path.py $out/bin/evaluate-path
'';

doCheck = false;
}
133 changes: 133 additions & 0 deletions nix/evaluate-path/evaluate-path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#! /usr/bin/env python3

import sys
import argparse
from copy import deepcopy
from queue import deque

def shortest_path_length(graph):
q = deque([(0, 0, 0)])
visited = set()

while q:
i, j, length = q.popleft()

if i == len(graph)-1 and j == len(graph[-1])-1:
return length

if (i, j) in visited:
continue

visited.add((i, j))

for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
ni = i + di
nj = j + dj
if 0 <= ni < len(graph) and 0 <= nj < len(graph[ni]) and (ni, nj) not in visited:
q.append((ni, nj, length + 1))

return -1

def evaluate_path(graph, path):
if len(graph) == 0 or len(graph[0]) == 0:
print("❓ Empty graph", file=sys.stderr)
return False, graph

expected_length = shortest_path_length(graph)
if expected_length == -1:
print("❗ There is no path from top left to bottom right in this graph")
if len(path) < expected_length:
print(f"❕ Path is too short (expected {expected_length} but got {len(path)})")
elif len(path) > expected_length:
print(f"❕ Path is too long (expected {expected_length} but got {len(path)})")

out_graph = deepcopy(graph)
i = 0
j = 0
if graph[i][j] == '#':
out_graph[i][j] = 'T'
else:
out_graph[i][j] = 'X'
print(f"🤯 Timmy falls into the water at spawn ({i}, {j})")
return False, out_graph

for direction in path:
if direction == 'R':
j += 1
elif direction == 'D':
i += 1
elif direction == 'L':
j -= 1
elif direction == 'U':
i -= 1

if i < 0 or i >= len(graph) or j < 0 or j >= len(graph[i]):
print(f"😱 Timmy falls out of bounds at coordinnates ({i}, {j})")
return False, out_graph

if graph[i][j] == '#':
out_graph[i][j] = 'T'
else:
out_graph[i][j] = 'X'
print(f"🥶 Timmy falls into the water at coordinnates ({i}, {j})")
return False, out_graph

result = False
if i == len(graph)-1 and j == len(graph[-1])-1:
result = True

return result, out_graph


if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="evaluate_path",
description="Test if a path is valid for the given input for the 5th problem")
parser.add_argument(
"INPUT_FILE", help="input file, the graph to test the path on", action="store", type=str)
parser.add_argument(
"PATH_FILE", help="path file, the path to evaluate", action="store", type=str)
parser.add_argument(
"OUTPUT_FILE", help="output file, file to write the out-graph with the path displayed to (T for valid squares, X for invalid squares)",
action="store", type=str, nargs="?")

args = parser.parse_args()
INPUT_FILE = args.INPUT_FILE
PATH_FILE = args.PATH_FILE
OUTPUT_FILE = args.OUTPUT_FILE

try:
inputfile = open(INPUT_FILE, "r")
except (OSError, IOError):
print(f"Error: {INPUT_FILE}: cannot read input file", file=sys.stderr)
exit(1)

try:
pathfile = open(PATH_FILE, "r")
except (OSError, IOError):
print(f"Error: {PATH_FILE}: cannot read path file", file=sys.stderr)
exit(1)

graph = [list(line[:-1]) for line in inputfile.readlines()]
path = [line[:-1] for line in pathfile.readlines()]

result, out_graph = evaluate_path(graph, path)

if result:
print("✅ Valid path")
else:
print("❌ Invalid path")


if OUTPUT_FILE:
try:
outfile = open(OUTPUT_FILE, "w")
except (OSError, IOError):
print(f"Error: {OUTPUT_FILE}: cannot open output file", file=sys.stderr)
exit(1)

for line in out_graph:
outfile.write("".join(line))
outfile.write("\n")

print(f"🗺️ Wrote out-graph to {OUTPUT_FILE}")
1 change: 1 addition & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
python3
(pkgs.callPackage ./05AB1E { })
(pkgs.callPackage ./05AB1E-encode { })
(pkgs.callPackage ./evaluate-path { })
];
}

0 comments on commit 674de19

Please sign in to comment.