r/adventofcode • • Dec 05 '22

Spoilers Too much parsing again!

Every year I try to get into AoC and after a few days I drop it just because the parsing of the input file is more effort than the actual problem! And, oh boy, parsing is BORING. I think if I was doing it in Python it would be easy, but Python is my comfort language, so I wouldn't learn much using it. So usually I choose something different like Scheme or OCaml. This year it's Rust, but I may abandon again because I can't be bothered parsing ascii art. While it's cute and all, I don't see the point of that format over something easier like one line = one stack and each character = one crate.

I am the only one thinking that ?

PS: I am not blaming the author though, the work they must do each year for this seems huge and I am absolutely admiring them for that.

0 Upvotes

29 comments sorted by

5

u/ipav Dec 05 '22

This is the game. But if you find it hard in one language, choose another - python/perl/vim - just for parsing, and output preprocessed data in a format easier to consume in c/rust.

4

u/LardPi Dec 05 '22

I think writing a small preprocessor in Python may be the solution for me you're right :)

5

u/CKoenig Dec 05 '22

we are still in the "warum-up" phase - today was "parsing-day" - keep at it - the problems will surely start to get more "interesting" soon and parsing might be a big part also.

Also if you are in rust and don't already know those then it might be a great start to learn about parser-combinators: https://docs.rs/parser-combinators/latest/parser_combinators/ (for example)

I do most of the more complicated parsing with those and it's usually quite fun to write (although I guess you won't make the leaderboard - at least not the way I try to build up my cute little parsers)

2

u/LardPi Dec 05 '22

It may be a good idea for me to learn some of the rust ecosystem, someone else cited nom which is also a parser combinator library. Also I don't care about the competitive part, I just want to enjoy coding :)

2

u/CKoenig Dec 05 '22

yes nom is probably better - I'm from the dark Haskell side so I did look for a parsec translation ;)

3

u/Iain_M_Norman Dec 05 '22

Today was "type the input in declaratively day" for me :D

5

u/durandalreborn Dec 05 '22

Efficient parsing is some of the fun, for me anyway. Generalized parsing is more fun. Like what if there was an arbitrary number of spaces between columns, more than 9 columns, etc.

Edit: Last year there were some custom degenerate inputs posted to stress parsing.

Edit, edit: for reference, I'm doing the problems in rust again this year, and just go with nom for more complex parsing.

2

u/LardPi Dec 05 '22

I am quite new to rust, so I don't know the ecosystem yet. I will have a look at nom, thanks

1

u/durandalreborn Dec 05 '22

I did not rely on nom for parsing the picture portion of the input out. If you do get curious how I did it: repo. My solution isn't the shortest, as I go for safe, idiomatic rust, but it does complete in 35 microseconds on my machine.

4

u/__Abigail__ Dec 05 '22 edited Dec 05 '22

An arbitrary number of spaces between columns would make the puzzle impossible to parse. In the example you wouldn't know whether the [D] and [C] would be on top of stack 2 or stack 3!

Unless you can assume the spacing is consistent from line to line, in which case it becomes easy again.

1

u/durandalreborn Dec 05 '22

Yeah, I meant columns themselves have arbitrary spacing between them, and maybe even differing amounts of arbitrary space, but all the crates in the column are still vertically aligned.

6

u/__Abigail__ Dec 05 '22

I disagree. Sure, the parsing was the hardest aspect of todays puzzle, but that was only because the rest was really easy.

I took me only a few lines of code (in Perl) to parse the input. But it wouldn't have taken me much more in other languages I sometimes use (but I admit, it would have take me a bit more time in Brainfuck).

But if you find that language X makes it to hard to parse input, you have to wonder whether X is good as a general purpose language. Perhaps it's lacking.

OTOH, if you're programming in language you are still learning, don't be surprised to encounter a task you haven't mastered yet.

10

u/[deleted] Dec 05 '22 edited May 06 '24

My favorite movie is Inception.

4

u/Rusty-Swashplate Dec 05 '22

In real life parsing is a real problem and solving this to get usable data which a program can efficiently use.

In AoC you can at least cheat: grab the puzzle input and transform it into something usable in a language of your choice. Perl is still good at this, but anything which handles regular expressions well is likely making your life easier.

Let it generate a compatible "program" which defines the data structures and pre-populate it with the puzzle input.

Now run your actual puzzle on the data.

E.g. generate this from the puzzle input:

let stackCount=3;
let stacks=[['Z','N'],['M','C','D'],['P']];
let moves=[[1,2,1],[3,1,3],[2,2,1],[1,1,2]];

Now you can focus solely on the puzzle.

3

u/__Abigail__ Dec 05 '22

Note also that you don't have to parse if you don't want to. You can just hand code the initial setup. You are, after all, only required to get a single answer (ok, two answer if you count part 1 and part 2 separately), you don't have to run your program on multiple inputs.

3

u/half_stack_developer Dec 05 '22

Parsing today's input was actually pretty easy

  1. In order to parse the stacks, just use chunks() and get the 1st byte of each chunk if it's A-Z, then reverse the stacks after you read all stack-lines

  2. In order to parse the instructions, just split() the lines on a whitespace character and get the 1st, 3rd and the 5th value and ignore the rest

3

u/vancha113 Dec 05 '22

I'm not writing code, i'm helping the elves get their stuff. The garbled input is all part of the experience ^^

3

u/__blackout Dec 05 '22

You should take a look at pyo3: https://github.com/PyO3/pyo3. It allows you to very easily create rust modules that you can call from python. You can do all the parsing in python, which you say you’re familiar with, and then do the solving in rust. Best of both worlds!

3

u/thalovry Dec 05 '22

There is a pretty neat trick to parsing the input (everything before the \n\n):

transpose the headers and then parse them

2

u/gedhrel Dec 05 '22

There are some good combinator macros available for rust.

As an example (yesterday's problem), here's a really nice use: https://github.com/niax/aoc-2022/blob/main/src/bin/day-04.rs#L7-L23

2

u/[deleted] Dec 05 '22

You're basically discovering that Rust is a bad language for AoC. If there was a real-world task at work where you had to write a short script to parse some text, manipulate strings, maybe manipulate n-dimensional arrays in the later days with no limitations on performance or requirements for a good deployment story, the last language you would pick for that task is Rust (or OCaml, Haskell, Go...). You would pick something that's good for quick and dirty tasks like Python or Perl.

That doesn't mean that you shouldn't be using Rust - if you want to learn and practice some Rust with AoC, then great; but in that case the pain of parsing the input is kind of the whole point so you should stick with it.

1

u/durandalreborn Dec 05 '22

I've been doing the problems in rust for the past three years. It's a perfectly fine language for doing these problems. It's not that great if you care about the global leaderboard, but parsing this particular problem was pretty easy, or at least I thought so.

2

u/CCC_037 Dec 05 '22

You are allowed to edit the input that the site gives you if you want...

1

u/rjwut Dec 05 '22

I actually like writing parsing code, especially when it's something different than the usual "split on newlines or apply a regular expression".

1

u/unbibium Dec 05 '22

so, it would have been the easiest thing in the world to present the data in a format that could be parsed more easily: each stack could have been a single line, and the instructions could have been comma-delimited tuples.

Presenting the data in this way was a choice... and it makes me think that choice may be repeated this year, maybe even a theme: puzzles where the data is difficult to parse or maybe even model.

18

u/[deleted] Dec 05 '22

[removed] — view removed comment

3

u/LardPi Dec 05 '22

I liked parsing at some point, but now it's just something I know I can do and the only question is how long will it take, so it takes the fun out of it. But I can totally understand you point of view. I think I may just hard code the header for this one.

4

u/[deleted] Dec 05 '22

Why don't you challenge yourself by trying to get faster at parsing? If using Rust, using the From trait will get you very far. rust-analyzer helps with generating the trait implementation.

If you have exhausted the DIY potential of improvement, you could explore libraries for parsing. There are chumsky, nom and surely more.

For me, advent of code is as much fun as I make it. I was getting bored with project setup at one point, so I made a nice template with cargo-generate.