Skip to content

Instantly share code, notes, and snippets.

@qmacro
Created December 2, 2022 14:26
Show Gist options
  • Save qmacro/f55e748f1c2436d5f263efa0ce1dfc69 to your computer and use it in GitHub Desktop.
Save qmacro/f55e748f1c2436d5f263efa0ce1dfc69 to your computer and use it in GitHub Desktop.
AOC2022 Day 1
#!/usr/bin/env jq
# Run with:
# -R (read raw strings, not JSON texts)
# -s (slurp all inputs into an array)
# i.e. jq -f solution.jq -R -s input.dat
# When initially read in, the input will look like this:
# "1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n"
# Marshall the input into an array of arrays of numbers
# - chop of last newline (so the split won't end up with a last empty value)
# - split on blank lines
# - split on newlines and convert values to numbers
def getinput:
sub("\n$";"")
| split("\n\n")
| map(split("\n")|map(tonumber))
;
def part1:
map(add) | max;
def part2:
map(add) | sort[-3:] | add;
getinput as $input
| [
($input | part1),
($input | part2)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment