Yearning to hike the John Muir Trail but didn’t win a coveted lottery permit? There’s a bot for that.

Scoring a permit for the southbound trail from Yosemite to Mount Whitney is perhaps the most challenging part of planning for the legendary 211-mile journey. Only 45 permits are available each day for southbound hikers entering across all of Yosemite’s trailheads and exiting the park.

But a frustrated UC San Francisco-trained data scientist has written and released a computer code that creates an automated web robot (bot) to access the reservation system and alert you when someone suddenly has cancelled, creating a precious opening.

“I kept getting rejected day after day,” said Daniel Himmelstein, 29, who wrote the Python code for his bot, dubbed “hackjohn,” from leafy Philadelphia, where he now does postdoctoral research while dreaming of soaring massifs of granite.

“A tool like this that can help people be more efficient,” he said. “It can benefit the overall system.”

The John Muir Trail has become an international destination, and as its popularity has soared, it’s become ever harder to get access.

Overwhelmed, the park created a lottery for 35 of the southbound slots, saving the other 10 slots for walk-ins. It’s a fiercely competitive process, with only a one-day window to submit your lottery application exactly 168 days in advance of your trip. The most prized trailhead is Happy Isles, exiting at Donohue Pass, located 13.3 miles from Tuolumne Meadows.

The vast majority of applications are denied. The lottery’s quota system is essential to preserve the solitude and fragility of the trail, according to the National Park Service. Yet it feels infuriatingly random for aspiring backpackers who pride themselves on plans and prep.

When a reservation is cancelled, the Park Service opens that slot. It takes near-constant vigilance to find these sudden cancellations, posted on a table buried within Yosemite’s “Full Trailhead Report” web page. You’re most likely to succeed if you have time and flexibility on your dates. Vacancies typically last just a few minutes.

That’s where hackjohn comes in.

Himmelstein realized the odds were stacked against him. He wanted to hike out of the popular Happy Isles trailhead. He needed to go in August, peak season. He didn’t have much time, due to his demanding postdoctoral research. He didn’t have much flexibility either, with a Bay Area wedding to attend and a plane ticket already in hand.

Weeks before leaving, he said, there was yet another complication: “I got a girlfriend — and a hiking companion.”

Now the couple needed the near-impossible: Not one, but two permits.

Every day, the dreaded email landed in his inbox, titled “Denied: Yosemite Wilderness Lottery.”

But Himmelstein is a problem-solving kind of guy, said girlfriend Trang Le, 27, also a postdoctoral data scientist. He reminds you to grab a windbreaker before ascending Half Dome. He worries that your toothbrush might poke a hole your pack. In a raging thunderstorm, he’ll suggest stuffing a Therm-a-Rest seat cushion up under your jacket to keep your shoulders dry.

Himmelstein, a postdoctoral fellow at the University of Pennsylvania who analyzes data for medical research, said he was drawn to backpacking for the most geeky of reasons: “I really like tying knots.”

So he wrote a code that scans Yosemite’s website for any John Muir Trail permit cancellations — so he didn’t have to manually check it all day, every day. Bots, skilled at simple and repetitive tasks, are used throughout the Internet to find things, from cheap concert tickets to the perfect clinical trial for cancer therapy.

Himmelstein’s code is not long or particularly complicated. But those few lines of script can download the park’s website, extract the table that holds vacancy information and scan it at superhuman speeds to see if there’s an opening that matches your schedule. Hackjohn allows you to specify the starting dates, trailheads, and group size that you’re looking for.

Then it notifies you using a chat app called Telegram, so you can jump on the phone and quickly call Yosemite to reserve the spot.

For his trip, Himmelstein scheduled the script to run several times a day particularly around 11 a.m., when he knew the rangers updated the calendar.

His hackjohn bot almost immediately found some slots. But the dates weren’t perfect, so he kept running the script, reserving and releasing spots, until they landed the precise day they wanted.

“He does everything this way,” laughed Le.

Finally — together, at the peak of the season, from Yosemite’s most coveted trailhead — Himmelstein and Le started their hike.

Their 17-day John Muir Trail experience was full of exhaustion and awe, scary climbs and exhilarating descents, a fierce thunderstorm and stunning sunrises.

But there was no pain in the permit process.

His adventure over, Himmelstein no longer needs hackjohn. But he’s a big believer in the citizen science movement and “open source” software, so this spring he posted his code online on the sharing site GitHub. At this point, users must run hackjohn for themselves — and it takes some technical skill to clone the code and adapt it for your personal dates. This code is unique to the John Muir Trail, but Le said she hoped it might be modified for use with similar reservation systems.

“I could have let it perish, as a program or let it be of use to others,” he said. “I want it to be useful.”

The source code for hackjohn is available at https://github.com/dhimmel/hackjohn under the permissive MIT License. To learn more, go to https://busy.org/@dhimmel/introducing-the-hackjohn-bot-for-southbound-john-muir-trail-permits.

To learn more about the John Muir Trail, or to submit an application to the National Park Service’s lottery, go to https://www.nps.gov/yose/planyourvisit/jmtfaq.htm.