I'm doing the Advent of Code (2022) in LaTeX3 (no, really I am). Each challenge involves some input, which for Day 6 consists of a single line which is very long (4096 characters). This line needs processing character by character, so I thought that I would get TeX to read it in character by character. Now, I know that TeX can cope with a token list of that length easily, but my purpose in doing the AoC in LaTeX3 is to better understand the language rather than to find the most efficient or effective solutions. So even though I ended up not using a character-by-character method, and read the whole file into a single token list, I'd still like to know how to do it.
Here's what I tried:
\cs_set_eq:Nc \aoc_input: {@@input} % to get an expandable file input
\cs_new:Npn \aoc_process_char:w #1
{
\tl_if_eq:nnF {#1} {\par} % this doesn't work
{
\aoc_process_char:w
}
}
\exp_after:wN \aoc_process_char:w \aoc_input: file.txt\relax
This doesn't work because the \par
at the end of a file is \outer
so can't be used in a parameter of a function. So reading around on this site, I saw the suggestion to \let
something to each token in turn and test that, something like:
\def\dostuff{%
\ifx\tmpvar\par
\else
\expandafter
\slurptoken
\fi
}
\def\slurptoken{\afterassignment\dostuff\let\tmpvar=}
\expandafter\slurptoken\@@input file.txt\relax
I'm struggling to convert that to LaTeX3 syntax. It feels like one of the \peek_after:Nw
type of functions should be what I'm looking for, but I need one that absorbs the token from the stream and the ones with remove
in their name just test the charcode or catcode whereas I want to be able to do more processing with that token.
So what is the best LaTeX3 equivalent of the TeX code above?