Open l-rettberg opened 2 years ago
Hi, thanks for asking!
Reading the puzzle inputs from external files was a deliberate choice that I made, to be able to change the inputs without having to recompile the code.
But yes, you can of course just copy-paste your input into a string and work from there. The answer to "is it faster?" is "yes, but sometimes it doesn't matter". For example, the runtimes of the most simple days can be reduced to almost 0 if you hard-code your input into a string, because most of the time in those solutions is spent reading the file (disk i/o operations are orders of magnitude slower than pure CPU operations). But, if you have to solve a more complicated puzzle and you come up with an idea that takes 1 minute to run, a few microseconds saved by not having to read from a file will be insignificant.
If you want to do that, I recommend using the include_str!
macro, which reads a file and saves it into a string in compilation time. For example, the following code would be equivalent to copy-pasting the contents of input.txt
as a string literal for the variable input_string
, but it keeps your code tidier since AoC inputs tend to be quite big:
let input_string = include_str!("input.txt");
In this case, if the input file changes, the program has to be compiled again in order to update the string that is stored in the compiled binary. The advantage is that, once it is compiled, you can run it anywhere even if the files with the inputs are not present, since their contents have already been built-in into your code as strings.
Hello, I’m considering trying to learn Rust by using it to solve 2022 Advent of Code Puzzles.
Your 2021 solutions seem very efficient!
curious if it’s possible (faster) to copy/paste the puzzle input into a Rust string literal rather than reading from file?
thanks! Rust newbie…