%!TEX pdflatex

\documentclass{article}
\usepackage[a5paper,margin=1cm]{geometry}
\usepackage{simplekv,xstring,xintexpr,tikz}
\usepackage{fontawesome5,twemojis}

\newlength\difficultylevelsoffset

\setKVdefault[difficultylevels]{opacity=0.25,hoffset=0.05em,swap=false,size={}}
\defKV[difficultylevels]{hoffset=\setlength\difficultylevelsoffset{#1},opacity=\def\difficultylevelsopacity{#1},size=\def\difficultylevelssize{#1}}

\NewDocumentCommand\difficultylevels{ O{} m m }{%
	%#1 = keys
	%#2 = symbol
	%#3 = levels
	\restoreKV[difficultylevels]%
	\setKV[difficultylevels]{#1}%
	\IfSubStr{#3}{/}{\StrCut{#3}{/}{\tmpdifflevelsnb}{\tmpdifflevelstot}}{\def\tmpdifflevelsnb{#3}\def\tmpdifflevelstot{4}}%
	{\difficultylevelssize%
	\ifboolKV[difficultylevels]{swap}%
		{%
			\xintifboolexpr{\tmpdifflevelsnb != \tmpdifflevelstot}%
			{%mix of
				\xintFor* ##1 in {\xintSeq{1}{\tmpdifflevelstot-\tmpdifflevelsnb}}\do{%
					#2\hspace*{\difficultylevelsoffset}%
				}%
			}{}%
			\xintFor* ##1 in {\xintSeq{1}{\tmpdifflevelsnb}}\do{%
				\xintifForLast{{\tikz[baseline=(ZZZ.base)] \node[opacity=\difficultylevelsopacity,inner sep=0pt] (ZZZ) {#2} ;}\relax}{{\tikz[baseline=(ZZZ.base)] \node[opacity=\difficultylevelsopacity,inner sep=0pt] (ZZZ) {#2} ;}\hspace*{\difficultylevelsoffset}}%
			}%
		}%
		{%
			\xintifboolexpr{\tmpdifflevelsnb != \tmpdifflevelstot}%
			{%mix of
				\xintFor* ##1 in {\xintSeq{1}{\tmpdifflevelstot-\tmpdifflevelsnb}}\do{%
					{\tikz[baseline=(ZZZ.base)] \node[opacity=\difficultylevelsopacity,inner sep=0pt] (ZZZ) {#2} ;}%
					\hspace*{\difficultylevelsoffset}%
				}%
			}{}%
			\xintFor* ##1 in {\xintSeq{1}{\tmpdifflevelsnb}}\do{%
				\xintifForLast{#2\relax}{#2\hspace*{\difficultylevelsoffset}}%
			}%
		}%
	}%
}

\begin{document}

\noindent{\LARGE Exercice 1\dotfill\difficultylevels[size=\large]{\faHeart}{1}}

\medskip

\noindent{\LARGE Exercice 1\dotfill\difficultylevels[size=\large,swap]{\faHeart}{1}}

\medskip

\noindent{\sffamily\LARGE Exercice 2\dotfill\difficultylevels{\texttwemoji{snake}}{2}}

\medskip

\noindent\textcolor{brown!75!black}{\bfseries Exercice 2\dotfill\difficultylevels[size=\small,opacity=0.4]{\faPaperPlane}{1}}

\end{document}