How good things and a good company can help you from falling into a pitfall in bad times has been clearly mentioned by Sage Valmiki in Ramayana. Actually, every chapter of Ramayana teaches a lesson of life and trains one to be disciplined.
As per one of the chapters of Ramayana, when Goddess Sita was kidnapped by Raavana and Lord Rama was looking for her, they met a demon called Kabandh. The demon suggested Rama a wonderful way to kill Raavana. His advice however, had a hidden meaning as well.