Advent of Code Inputs: Why Trailing Newlines and Line Endings Break Parsers
Your parser passes the sample and fails on the real puzzle input by exactly one phantom line. The usual cause isn't the puzzle. It's an invisible character: a final newline you didn't expect, or a carriage return you didn't know was there.
Puzzle inputs are plain text that you download or paste into a file yourself. The Advent of Code About page tells you to make sure you have the entire puzzle input, and asks that you not publish your inputs in a public repository. It says nothing about line endings or a final newline, so treat the bytes of your own copy as the thing to check. If you are still setting up files for the month, see how to keep inputs, notes, and solutions in files.
LF, CRLF and CR: three ways to end a line
A line ends with one of three conventions, depending on the system that wrote the file. According to Wikipedia's newline article:
- LF (
\n) is used by Unix-like systems, including Linux and macOS. - CR+LF (
\r\n) is used by Windows and MS-DOS compatibles. - CR (
\r) alone was used by classic Mac OS and several 8-bit machines.
The split goes back to Teletype printers. A carriage return moved the carriage to column one and a line feed moved the paper up, so a newline took two characters. Multics, which began development in 1964, used LF alone, and Unix followed. MS-DOS adopted CP/M's CR+LF for compatibility, and Windows inherited it.
You rarely notice, because many languages hide the difference when reading text. Python's universal newline support is on by default and maps \n, \r and \r\n to \n, and Java's BufferedReader.readLine() accepts all three. Trouble starts when you read the whole input as one string and split it yourself.
The trailing newline: does the last line end?
A file can end right after its last character, or after a line break. Many tools write the break, many editors add it on save, and some copy-paste paths drop it. Both versions look the same on screen.
Splitting shows the difference:
text = "1\n2\n3\n"
text.split("\n") # ['1', '2', '3', '']
text.splitlines() # ['1', '2', '3']
text.strip().split("\n") # ['1', '2', '3']With a final newline, split("\n") returns an empty last element, because the delimiter after the last line has nothing after it. Without that newline there is no empty element at all. Your code now behaves differently depending on a byte you can't see.
Two failures follow. Parsing the empty string as an integer raises an error. And if the puzzle separates groups with blank lines, an empty tail looks like one more group, so you count or sum a group that does not exist. Often that gives you a plausible wrong answer instead of a crash.
How a pasted input changes without you noticing
The same input can come out with different bytes on two machines, or on the same machine on two days:
- Your editor may add a final newline on save, or strip trailing whitespace, including the break itself.
- Copying from the browser and downloading the file are different paths, so they can differ at the end.
- Git can rewrite line endings. According to the Pro Git book, Git can convert CRLF to LF when you add a file to the index and back when it checks out code. On Windows you set
core.autocrlftotrue, and on Linux or macOS you can useinput, which converts CRLF to LF on commit but not the other way.
Per-path rules are in the gitattributes documentation. The text attribute normalizes line endings to LF in the index, and eol picks the style used in the working tree. If text is unspecified, Git falls back to core.autocrlf. A solution that works on your laptop can therefore fail on a friend's Windows checkout of the same repository.
If a file already has stray carriage returns, a quick find and replace with regex removes them. Replace \r with nothing, with regex mode on.
Check what you actually have
Don't guess. Open the input and look:
- In a code editor, turn on invisible characters if it has them, and jump to the end of the file. If the cursor lands on an empty last line, the text ends with a newline.
- In a terminal,
tail -c 1 input.txt | xxdprints the last byte.0ameans the file ends with a line feed. file input.txtreports "with CRLF line terminators" when the file uses Windows endings.
Run these on the file your program reads, not the one you downloaded earlier. The two can differ.
Parse defensively, once
Write the loader so the answer does not depend on either detail:
- Read with universal newlines, or use
splitlines(), which does not care about\r\nand returns no empty tail. - For blank-line groups, strip the whole text first, then split on a blank line. Normalize
\r\nto\nbefore you split. - Strip each line before comparing strings or converting numbers.
- Keep the rules in your project. An .editorconfig file and a
.gitattributesfile make the choice explicit for every machine and editor that touches the repository.
Put this in one small read_input function and reuse it every day, so the fix lives in one place.
Conclusion
The input is rarely wrong. The assumption about its bytes is what breaks. Check your own copy for a final newline and for CRLF, parse once in a way that tolerates both, and spend the rest of the evening on the puzzle.
Frequently Asked Questions
Does an Advent of Code input end with a newline?
The About page does not say. Open your own input file and check whether the last line is followed by a line break, and write the parser so it works either way.
Why do I get an extra empty string when I split my input?
Splitting on \n produces an empty last element when the text ends with a newline, because the delimiter after the final line has nothing after it. Use splitlines() or strip the text before splitting.
Why does my answer look right but get rejected on Windows?
If the file has CRLF endings and you split on \n only, each line keeps a trailing \r. It is invisible when printed but breaks string comparisons and can break number parsing.
Should I commit my puzzle input to Git?
Advent of Code asks that you not include your inputs in a public repository. In a private one, set text and eol attributes so Git does not rewrite line endings differently on each machine.