Skip to content

Instantly share code, notes, and snippets.

@beanz
Created December 4, 2019 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beanz/1d943f65218ab0c10481b0ecd5e3976c to your computer and use it in GitHub Desktop.
Save beanz/1d943f65218ab0c10481b0ecd5e3976c to your computer and use it in GitHub Desktop.
Advent of Code 2019, Day 4 in jq
#!/bin/sh
exec jq -r --raw-input --slurp \
'def part1(r):
range(r[0];r[1]+1) | tostring |
select((. | length) == 6) |
select(. | test("(.)\\1")) |
split("") |
select(.[0] <= .[1] and
.[1] <= .[2] and
.[2] <= .[3] and
.[3] <= .[4] and
.[4] <= .[5]);
def part2(r):
range(r[0];r[1]+1) | tostring |
select((. | length) == 6) |
select(. | test("^(?:.*(.)((?!\\1).)\\2(?!\\2)|(.)\\3(?!\\3))")) |
split("") |
select(.[0] <= .[1] and
.[1] <= .[2] and
.[2] <= .[3] and
.[3] <= .[4] and
.[4] <= .[5]);
. | split("-") | map(tonumber) |
{
p1: [part1(.)] | length,
p2: [part2(.)] | length
} | "Part 1: \(.p1)\nPart 2: \(.p2)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment