Hedge fund manager David Einhorn looks to be betting on Rite Aid's (NYSE:RAD) resurgence. In the second quarter, Einhorn's fund, Greenlight Capital, bought up 20.2 million shares of the drugstore.

Rite Aid has been on a steady upswing all year, and Einhorn's Rite Aid trade is almost assuredly profitable. Year-to-date, shares of Rite Aid are up more than 158%, and further upside is possible.

Pharmacies have been a great investment
But it isn't just Rite Aid that has rewarded shareholders. The company's competitors, Walgreen (NASDAQ:WBA) and CVS Caremark (NYSE:CVS) have outperformed the S&P 500 this year.

Walgreen shares are up nearly 32%, while CVS has rallied more than 21% in 2013.

Walgreen has actually outperformed Rite Aid from a financial standpoint. In July, Walgreen's same-store sales rose 6.3%, while Rite Aid's increased just 1.3%.

Yet, Rite Aid shares have been able to outperform Walgreen because of the inherent risks. Last December, Rite Aid was literally a penny stock, dropping below $1 per share -- there had even been rumblings of a bankruptcy.

With nearly $6 billion in debt, about twice the firm's market cap, Rite Aid is far more leveraged than its competitors.

Reasons to remain bullish
There are reasons to like the pharmacy stocks going forward. Most notably, the Patient Protection and Affordable Care Act, commonly referred to as Obamacare.

The law should have the effect of driving demand for pharmaceuticals, which could benefit the bottom line's of all three companies. With the expansion of Medicaid, there will be more customers for these companies to sell to.

Analysts at UBS upgraded Walgreen back in March based partly on this increased demand. At the same time, CVS has said it sees the ACA as an opportunity and plans to promote the law's new coverage options to the uninsured.

However, Rite Aid's management has remained conservative. On the company's last earnings call, Rite Aid's CEO called the roll out of the ACA a "fairly murky picture" and noted that the size of the addressable population isn't particularly clear.

But even without the ACA, the aging baby boomer population could also support the sector. As CVS' CEO has said, 10,000 new baby boomers are becoming eligible for Medicare every single day.

But which stock is the best?
Of the three stocks, Rite Aid clearly carries the greatest risk. Its leverage and its (until recently) poor financial performance make it a dangerous stock to own.

Analysts at Goldman Sachs have consistently raised their price target on the stock, but have kept a neutral rating. In June, they said the stock's powerful rally made it difficult to buy shares. But if you had listened to Goldman, you would have missed out on a further 20% rally.

As the recent rally indicates, Rite Aid's risk also carries the potential for greater returns. Even trading at a multi-year high, Rite Aid remains the cheapest of the stocks -- its price-to-earnings ratio of 14.5 is much less than both CVS' (17.4) and Walgreen's (21.4).

CVS is the largest of the three firms, and the market leader. CNBC's Jim Cramer has that, of the three firms, he prefers CVS because of its ownership of Caremark.

Analysts at Wells Fargo gave CVS an outperform rating in May for much the same reason. They have a $70 price target on the stock. Caremark is the second largest pharmacy benefit manager in the US, meaning that it's involved directly in the process of negotiating drug benefits. Wells Fargo believes that its ownership of Caremark allows to better manage the shifts taking place in the industry.

Walgreen is a pure pharmacy retailer, and its retail operations are the best of the three. The company has begun to remodel its stores, leading FUBU's Daymond John to compare the company to Apple.

It also pays the highest dividend at 2.50%, and is emphasizing its in-store clinics. Earlier this year, it said these clinics (which it has rebranded "Healthcare Clinics") would start managing chronic care conditions.

Of the three stocks, CVS is likely the safest pick, though Rite Aid offers the potential for outsized gains. Walgreen's remodeling initiative and dividend makes it attractive, but it's the most expensive stock of the three.

Investing in Rite Aid
Greenlight doesn't have a consistent track record of holding stocks for years, so it's possible that the fund already dumped its stake. Moreover, as Einhorn has said, investors should never blindly follow his fund into a particular stock.

Yet, Rite Aid does remain an attractive investment. It's in a sector -- pharmacies -- that looks to be benefiting from the ACA and retirement of the baby boom generation.

All three pharmacy stocks have been great investments this year, and could continue to reward shareholders going forward.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.