10 ms·
Solving Advent of Code with jq
- yoaquim 4y agoThis scares me.
- jordanlewis 4y agoImpressively golfed.
- cmcconomy 4y agoinsane
- thelittlenag 4y agoThat this is now their 6th year doing advent of code using jq is deeply, deeply impressive.
- js2 4y agoA nice little Makefile driving each day too: https://github.com/odnoletkov/advent-of-code-jq/blob/master/Makefile https://github.com/odnoletkov/advent-of-code-jq/blob/master/... And I somehow just learned about asciicinema from it: https://asciinema.org https://asciinema.org This is parsing a maze into a graph and finding the shortest path: https://github.com/odnoletkov/advent-of-code-jq/blob/master/2022/12-1.jq https://github.com/odnoletkov/advent-of-code-jq/blob/master/... I can kinda make out how it's parsing the input and converting it to a graph. I can't make heads or tails of the path finding and I don't see anything that really looks like Djikstra's algorithm.
- nneonneo 4y agoI suspect it’s a breadth-first search, which is substantially easier to code. .front looks like the queue of nodes to visit.
- bhrgunatha 4y agoYou would hope so. Saw many people on r/adventofcode using dijkstra or even A* when every path step is just 1. I went with bfs because I'm pretty sure using a priority queue or heuristic function is going to slow those down.
- vjerancrnjak 4y agoFor the input sizes in aoc, the slowdown shouldn't be measurable. Taking input size into account, you do not even need a queue, just loops.
- mklein994 4y agoThis is incredible. I'm learning so much by just reading the source code and trying it out.
- lcnPylGDnU4H9OF 4y agoI appreciate this commit message: https://github.com/odnoletkov/advent-of-code-jq/commit/358038a3a624025008afb591ab283d7424f60edf https://github.com/odnoletkov/advent-of-code-jq/commit/35803....
- wodenokoto 4y agoI'd love to read an AMA with the author. I can't even complete Advent of Code in a normal programming language!
- aarchi 4y agoTry doing it in Whitespace then :) https://github.com/andrewarchi/ws-challenges https://github.com/andrewarchi/ws-challenges
- pingiun 4y agoi’m doing the same thing on my github :D. i think my code looks a bit cleaner too… ;) https://github.com/pingiun/aoc-2022-jq https://github.com/pingiun/aoc-2022-jq
- vgb2k18 4y agoOP's code is golfed + completed every day, on the day + has six years of jq Advent committed. I say, not doing the same thing.
- qmacro 4y agoWas that really necessary? The title of the item, to which the above reply was written, was "Solving Advent of Code with jq". So yes, "doing the same thing" here fits fine.
- odnoletkov 4y agoRepo author here, let me promote jq a bit – it's much more than a simple command-line JSON processor you know it for: * It is a generator-based language which means you operate streams of values rather than single values. Takes some time to get used to, but you'd never want to go back to traditional model, at least for data processing. See 'Generators and iterators' section of the man page: https://github.com/stedolan/jq/blob/cff5336ec71b6fee396a95bb0e4bea365e0cd1e8/docs/content/manual/manual.yml#L2852 https://github.com/stedolan/jq/blob/cff5336ec71b6fee396a95bb... * Designed for CLI, it makes it easy and even pushes you to express your program as a single pipeline. You rarely need variables, functions or any control structure. And pipelines are great to build iteratively, debug and compose (see https://jqplay.org https://jqplay.org, https://jqterm.com https://jqterm.com) * Core language is small but powerful with features like slicing, destructuring, complex assignments and error handling. And you are already an expert in it's (immutable) data structures – it is just JSON * Batteries included in the stdlib – regex, path operations, C math/dates, algorithms. Modules are supported, but I did not need any dependencies for solving Advent of Code. * jq is ubiquitous. Often pre-installed, tiny binary, no dependencies, basically single version (ok, awk is better – anything else?) Being a go-to tool for JSON is sort of a double-edged sword – people just don't look past that. But nowadays JSON is the format for data, everything is convertible to it. And you can feed plain text into jq using --raw-input flag. i.e.: jq -nR '[inputs]' /etc/hosts The main limitation is there is no I/O whatsoever – you can't read from file/network/pipe/system from the program. You need to provide all inputs upfront. Hope that explains why I prefer jq. For any programming really, not just AOC
- spion 4y agoThe one thing I dislike about jq is that it treats many-things and one-thing the same way. This PL design choice is IMO fraught with peril. Its the reason why jq can't have a "slurp" filter. Treating many-things the same way means there is no first-class representation of a json stream, which in turn means there are no functions that can operate on a JSON stream as a whole. If JQ had first-class streams, most code would've looked quite pedestrian. Everything that operates on many values would need some kind of `map`, there would also be `reduce` as a normal filter, as well as slurp - and the overal weirdness of programs would significantly decrease - the code would look exactly like normal functional programming pipelines.
- jowi2020 4y agoelegant solution. Thank you for sharing.
- klevans 4y agolong live jq!!!