From 870c49e2a23bded9870c0bec6d94b75ca3d30662 Mon Sep 17 00:00:00 2001 From: Alex Olivas Date: Wed, 30 Dec 2020 15:39:01 -0700 Subject: [PATCH] Moving from an empty peg is also an illegal move, but the code that checks this segfaults. --- epi_judge_cpp/hanoi.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/epi_judge_cpp/hanoi.cc b/epi_judge_cpp/hanoi.cc index c6d076b23..12501db87 100644 --- a/epi_judge_cpp/hanoi.cc +++ b/epi_judge_cpp/hanoi.cc @@ -25,10 +25,11 @@ void ComputeTowerHanoiWrapper(TimedExecutor& executor, int num_rings) { for (const vector& operation : result) { int from_peg = operation[0], to_peg = operation[1]; - if (!pegs[to_peg].empty() && pegs[from_peg].top() >= pegs[to_peg].top()) { + if (pegs[from_peg].empty() || (!pegs[to_peg].empty() && + pegs[from_peg].top() >= pegs[to_peg].top())) { throw TestFailure("Illegal move from " + - std::to_string(pegs[from_peg].top()) + " to " + - std::to_string(pegs[to_peg].top())); + std::to_string(from_peg) + " to " + + std::to_string(to_peg)); } pegs[to_peg].push(pegs[from_peg].top()); pegs[from_peg].pop();