\documentclass[
	solutions,
	singlenumbering,
	nopagenumber
]{../../resources/ormc_handout}
\usepackage{../../resources/macros}

\usepackage{ifthen}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{patterns}

% We put nodes in a separate layer, so we can
% slightly overlap with paths for a perfect fit
\pgfdeclarelayer{nodes}
\pgfdeclarelayer{path}
\pgfsetlayers{main,nodes}

% Layer settings
\tikzset{
	% Layer hack, lets us write
	% later = * in scopes.
	layer/.style = {
		execute at begin scope={\pgfonlayer{#1}},
		execute at end scope={\endpgfonlayer}
	},
	%
	% Arrowhead tweak
	>={Latex[ width=2mm, length=2mm ]},
	%
	% Labels inside edges
	label/.style = {
		rectangle,
		% For automatic red background in solutions
		fill = \ORMCbgcolor,
		draw = none,
		rounded corners = 0mm
	},
	%
	% Nodes
	main/.style = {
		draw,
		circle,
		fill = white,
		line width = 0.4mm,
		text width = 5mm,
		align = center
	},
	accept/.style = {
		draw,
		circle,
		fill = white,
		double,
		double distance = 0.5mm,
		line width = 0.4mm,
		text width = 5mm,
		align = center
	},
}



\title{Warm-Up: Snakes}
\subtitle{Prepared by \githref{Mark} on \today}

\begin{document}

	\maketitle
	\newcounter{mycounter}
	\setcounter{mycounter}{0}
	\newif\iftmp

	\begin{center}
	\begin{tikzpicture}[scale = 0.6]
		\begin{scope}[layer = nodes]
			\foreach \r in {1,3,5,7,9} {
				\foreach \i in {1,...,10} {
					\pgfmathparse{Mod(\i-1,10)}
					\let\x\pgfmathresult
					\pgfmathparse{div(\i-1,10) + \r}
					\let\y\pgfmathresult
					\pgfmathparse{(\r-1)*10+\i}
					\let\z\pgfmathresult

					\tmpfalse
					\pgfmathapproxequalto{\z}{4}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{5}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{19}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{21}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{28}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{31}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{35}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{44}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{47}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{52}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{53}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{59}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{70}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{76}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{81}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{88}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{98}
					\ifpgfmathcomparison\tmptrue
					\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi

					\stepcounter{mycounter}
					\iftmp
						\node[accept] (\arabic{mycounter}) at (2*\x, 2*\y) {
							$\pgfmathprintnumber{\z}$
						};
					\else
						\node[main] (\arabic{mycounter}) at (2*\x, 2*\y) {
							$\pgfmathprintnumber{\z}$
						};
					\fi
				}

				\foreach \i in {1,...,10} {
					\pgfmathparse{9 - Mod(\i-1,10)}
					\let\x\pgfmathresult
					\pgfmathparse{div(\i-1,10) + \r + 1}
					\let\y\pgfmathresult
					\pgfmathparse{\r*10+\i}
					\let\z\pgfmathresult

					\tmpfalse
					\pgfmathapproxequalto{\z}{4}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{5}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{19}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{21}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{28}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{31}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{35}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{44}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{47}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{52}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{53}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{59}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{70}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{76}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{81}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{88}
					\ifpgfmathcomparison\tmptrue
					\else\pgfmathapproxequalto{\z}{98}
					\ifpgfmathcomparison\tmptrue
					\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi

					\stepcounter{mycounter}

					\iftmp
						\node[accept] (\arabic{mycounter}) at (2*\x, 2*\y) {
							$\pgfmathprintnumber{\z}$
						};
					\else
						\node[main] (\arabic{mycounter}) at (2*\x, 2*\y) {
							$\pgfmathprintnumber{\z}$
						};
					\fi
				}
			}
		\end{scope}

		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(4) -- +(1,0) -- +(1, 13) -- +(3, 13) -> (75);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(5) -- +(0,-1) -- +(1,-1) -- +(1,1) -- (15);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(19) -- +(1, 0) -- +(1, 5) -- + (-1, 5) -- (41);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(21) -- +(1, 0) -- +(1,-5) -- +(3, -5) -- (3);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(28) -- +(1,0) -- +(1,3) -- +(3,3) -- (50);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(31) -- +(1,0) -- +(1,-7) -- +(-3, -7) -- (8);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(35) -- +(-1,0) -- +(-1,11) -- (96);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(44) -- +(-1,0) -- +(-1,7) -- +(-3,7) -- (82);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(47) -- +(1,0) -- +(1,-3) -- +(5,-3) -- (30);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(52) -- +(-1, 0) -- +(-1, -1) -- +(-5,-1) -- +(-5,-5) -- +(-11,-5) -- (23);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(53) -- +(-1,0) -- +(-1,7) -- (94);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(59) -- +(-1, 0) -- +(-1,1) -- +(-3,1) -- +(-3,9) -- +(7, 9) -- (95);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(70) -- +(-1, 0) -- +(-1, 5) -- (91);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(76) -- +(-1, 0) -- +(-1,-1) -- +(-5,-1) -- +(-5,-5) -- +(-7,-5) -- (41);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(81) -- +(0, -1) -- +(3, -1) -- +(3, -3) -- (62);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(88) -- +(1, 0) -- +(1, -3) -- +(-1,-3) -- (67);
		\draw[->, line width = 0.6mm, rounded corners = 2mm]
			(98) -- +(0, 1.5) -- +(15.5, 1.5) -- +(15.5,-15) -- +(13, -15) -- (12);
	\end{tikzpicture}
	\end{center}

\end{document}