Addressing the LHC flavor anomalies with horizontal gauge symmetries