# How to get consistent rule lengths for algorithmicx and listings floats?

In the following MWE, I would like the rules (the gray lines above and below the floats) of both environments (listings and algorithmicx, resp. algpseudocode style) to have visually (I'm not aiming for pt perfection) the same indentation relative to the line numbers and the text margins.

Personally, I prefer my current listings setting. So unless I unintentionally broke some typographic rule (no pun intended), I would like to make the algorithmicx float to conform to the code listing.

### MWE

Yes, it's a bit long...

\documentclass{memoir}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{xcolor}
\usepackage{listings}

% algorithm settings
\makeatletter
\newcommand\fs@plainruled{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
\def\@fs@pre{{\color{gray}\hrule height.8pt depth0pt \kern2pt}}%
\def\@fs@post{}%
\def\@fs@mid{{\kern2pt\color{gray}\hrule height.8pt depth0pt\relax\kern\abovecaptionskip}}%
\let\@fs@iftopcapt\iffalse}
\makeatother
\floatstyle{plainruled}
\restylefloat{algorithm}
\algrenewcommand{\alglinenumber}[1]{\color{gray}\tiny#1}

% listings settings
\lstset{
numbers=left,numberstyle=\color{gray}\tiny,numbersep=1em,
showspaces=false,showtabs=false,showstringspaces=false,
frame=tb,tabsize=4,rulecolor=\color{gray},
xleftmargin=1.5em,
breaklines=true,breakatwhitespace=true
}
\begin{document}
\begin{algorithm}
\caption{K-Means algorithm}
\label{alg:k-means-orig}
\begin{algorithmic}[1]
\State select $K$ observations randomly as initial cluster centroids
\Repeat
\State assign each observation to the closest centroid
\State recalculate cluster positions
\Until{cluster centroids do not move anymore}
\end{algorithmic}
\end{algorithm}

\begin{lstlisting}[float,caption={A blind text code listing},label={lst:blind}]
for(k=0; k<N; k++) {
if(k%2=0) {
tmp = foo[k];
foo[k] = foo[k+1];
foo[k+1] = tmp;
}
}
\end{lstlisting}
\end{document}


I produced an approximation with \vspace{}, \hrulefill and \\. It looks how I want it, but the magic numbers (1.55em, -7pt, -9pt) and the use of \\ with negative spacing tell me that I'm doing it horribly wrong.

### Changes

Replace the %algorithm settings part in the preamble with:

% algorithm settings
\makeatletter
\newcommand\fs@plainruled{\def\@fs@cfont{\rmfamily}\let\@fs@capt\floatc@plain
\def\@fs@pre{{\color{gray}\hspace{1.55em}\hrulefill\\[-7pt]}}%
\def\@fs@post{}%
\def\@fs@mid{{\vspace{-9pt}\color{gray}\hspace{1.55em}\hrulefill{}\\[-6pt]}}%
\let\@fs@iftopcapt\iffalse}
\makeatother
\floatstyle{plainruled}
\restylefloat{algorithm}
\algrenewcommand{\alglinenumber}[1]{\color{gray}\tiny#1}