Skip to content

Instantly share code, notes, and snippets.

@tomtheisen
Last active December 13, 2022 17:39
Show Gist options
  • Save tomtheisen/e95cfde6ff20d902f66913b8b06e8c11 to your computer and use it in GitHub Desktop.
Save tomtheisen/e95cfde6ff20d902f66913b8b06e8c11 to your computer and use it in GitHub Desktop.
AOC 2022 Mungers
#(
@/(\d+)\n(\d+)/=>{$1 $2 +}
fx /\d+/ => { _ get(max) max set(max) }
{ get(max) }
)
#(
@/(\d+)\n(\d+)/=>{$1 $2 +}
fx /\d+/ => { _ 10 zpad push(nums) }
{ sort(nums) 3 times { pop(nums) + } }
)
#(
/A Y|B Z|C X/ => { _ " 6" }
/A X|B Y|C Z/ => { _ " 3" }
'X' => "1"
'Y' => "2"
'Z' => "3"
fx /\d/ => { get(score) _ + set(score) }
{ get(score) }
)
#(
/A X|B Z|C Y/ => { _ " 3"} ! scissors
/A Y|B X|C Z/ => { _ " 1"} ! rock
/A Z|B Y|C X/ => { _ " 2"} ! paper
'Y' => "3"
'Z' => "6"
fx /\d/ => { get(score) _ + set(score) }
{ get(score) }
)
#(
/.+/ => {
_ _ len 2 / take
" "
_ _ len 2 / skip
}
/^\w*(\w)\w* \w*\1\w*$/m => { $1 }
/[a-z]/ => { _ ord "a" ord - 1 + }
/[A-Z]/ => { _ ord "A" ord - 27 + }
fx /\d+/ => { _ get(tot) + set(tot) }
{ get(tot) }
)
#(
/\w*(\w)\w*(?:\n\w*\1\w*){2}/ => { $1 }
/[a-z]/ => { _ ord "a" ord - 1 + }
/[A-Z]/ => { _ ord "A" ord - 27 + }
fx /\d+/ => { _ get(tot) + set(tot) }
{ get(tot) }
)
#(
fx /(\d+)-(\d+),(\d+)-(\d+)/ => {
$1 $3 >= $2 $4 <= *
$3 $1 >= $4 $2 <= *
+ if { inc(overlaps) }
}
{ get(overlaps) }
)
#(
fx /(\d+)-(\d+),(\d+)-(\d+)/ => {
$3 $2 >>
$1 $4 >>
+ not if { inc(overlaps) }
}
{ get(overlaps) }
)
! string reverse
def(rev) /.+/ => #(
fx { "" set(rev) }
fx /./ => { _ get(rev) cat set(rev) }
{ get(rev) }
)
#(
! populate the right numer of stacks
fx /^(?: |\d)+$/m => /\d+/ => { "" push(stacks) }
! build the initial state like a ring buffer
fx /(.*\[.+\n)+/ =>
/\[(\w)\] ?| {3,4}/ =>
{ $1 uncons(stacks) cat push(stacks) }
fx /move (\d+) from (\d+) to (\d+)/ => {
! grab the moving part
$2 times { uncons(stacks) }
copy $1 -1 * take set(moving) drop
$1 -1 * skip
$2 times { cons(stacks) }
! drop it off
$3 times { uncons(stacks) }
get(moving) do(rev) cat
$3 times { cons(stacks) }
}
{ for(stacks) { _ -1 take cat } }
)
#(
! populate the right numer of stacks
fx /^(?: |\d)+$/m => /\d+/ => { "" push(stacks) }
! build the initial state like a ring buffer
fx /(.*\[.+\n)+/ =>
/\[(\w)\] ?| {3,4}/ =>
{ $1 uncons(stacks) cat push(stacks) }
/move (\d+) from (\d+) to (\d+)/ => {
! grab the moving part
$2 times { uncons(stacks) }
copy $1 -1 * take set(moving) drop
$1 -1 * skip
$2 times { cons(stacks) }
! drop it off
$3 times { uncons(stacks) }
get(moving) cat
$3 times { cons(stacks) }
}
{ for(stacks) { _ -1 take cat } }
)
(
/^.*?(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3)(.)/ => { _ len }
/.*$/ => ""
)
(
! Stax codegen lol:
! 14r{{"\`_"m'|* i{"(?!` )"}M m "(.)"* "(.)"+
/^.*?(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3)(.)(?!\1|\2|\3|\4)(.)(?!\1|\2|\3|\4|\5)(.)(?!\1|\2|\3|\4|\5|\6)(.)(?!\1|\2|\3|\4|\5|\6|\7)(.)(?!\1|\2|\3|\4|\5|\6|\7|\8)(.)(?!\1|\2|\3|\4|\5|\6|\7|\8|\9)(.)(?!\1|\2|\3|\4|\5|\6|\7|\8|\9|\10)(.)(?!\1|\2|\3|\4|\5|\6|\7|\8|\9|\10|\11)(.)(?!\1|\2|\3|\4|\5|\6|\7|\8|\9|\10|\11|\12)(.)(?!\1|\2|\3|\4|\5|\6|\7|\8|\9|\10|\11|\12|\13)(.)/ => { _ len }
/.*$/ => ""
)
def(processsize) {
copy "100e3" <= *
get(result) + set(result)
}
#(
(
'$ cd ..' => fx { pop(sizes) do(processsize) }
/\$ cd .+/ => fx { 0 push(sizes) }
/(\d+) [a-z.]+/ => fx {
count(sizes) times { pop(sizes) $1 + cons(sizes) }
}
)
fx { for(sizes) { _ do(processsize)} }
{ get(result) }
)
#(
fx { "30e6" "70e6" set(result) - set(target) }
(
'$ cd ..' => fx { pop(sizes) push(closed) }
/\$ cd .+/ => fx { 0 push(sizes) }
/(\d+) [a-z.]+/ => fx {
get(target) $1 + set(target) drop
count(sizes) times { pop(sizes) $1 + cons(sizes) }
}
)
fx {
for(sizes) { _ push(closed) }
for(closed) {
_ get(target) >= if {
_ get(result) min set(result)
}
}
}
{ get(result) }
)
def(markvisiblefromleft) /.+/ => (
/^/ => { "" set(highest) }
/./ => {
_ _ ord 16 % get(highest) > if
! increase code points of visible trees by 16
{ ord 16 % set(highest) 64 + chr }
}
)
def(rotate) #( ! clockwise
fx { empty(lines) }
/.+/ => fx { _ push(lines) _ len set(width) }
{
get(width) times {
count(lines) times {
pop(lines)
copy 1 skip cons(lines)
1 take 1 rpad
}
"\n"
}
}
)
#(
{ _ 4 times { do(markvisiblefromleft) do(rotate) } }
/\d|\n/ => ""
{ _ len } ! count remaning non-digit trees
)
def(lookup) {
inc(viewup)
get(row) get(viewup) - "," get(col) cat cat get set(curr)
get(hometree) < if
{ get(curr) len if { do(lookup) } { dec(viewup) } }
}
def(lookright) {
inc(viewright)
get(row) "," get(col) get(viewright) + cat cat get set(curr)
get(hometree) < if
{ get(curr) len if { do(lookright) } { dec(viewright) } }
}
def(lookdown) {
inc(viewdown)
get(row) get(viewdown) + "," get(col) cat cat get set(curr)
get(hometree) < if
{ get(curr) len if { do(lookdown) } { dec(viewdown) } }
}
def(lookleft) {
inc(viewleft)
get(row) "," get(col) get(viewleft) - cat cat get set(curr)
get(hometree) < if
{ get(curr) len if { do(lookleft) } { dec(viewleft) } }
}
#(
/.+/ => fx { inc(height) _ len set(width) }
fx { 1 set(col) set(row) }
(
/./ => fx { _ get(row) "," get(col) cat cat set inc(col) }
'\n' => fx { inc(row) 1 set(col) }
)
{
1 set(row) set(col) drop
get(height) times {
get(width) times {
get(row) "," get(col) cat cat get set(hometree) drop
0 set(viewup) drop do(lookup)
0 set(viewright) drop do(lookright)
0 set(viewdown) drop do(lookdown)
0 set(viewleft) drop do(lookleft)
get(viewup) get(viewright) get(viewdown) get(viewleft) * * *
get(best) max set(best) drop
inc(col)
}
inc(row)
1 set(col) drop
}
get(best)
}
)
def(abs) '-' => ""
def(sign) /[1-9]\d*/ => "1"
def(R) { inc(hx) }
def(L) { dec(hx) }
def(U) { inc(hy) }
def(D) { dec(hy) }
#(
fx /(\w) (\d+)/ => {
$2 times {
$1 do
get(hx) get(tx) - do(abs)
get(hy) get(ty) - do(abs)
max 1 >> if {
get(hx) get(tx) - do(sign) get(tx) + set(tx)
","
get(hy) get(ty) - do(sign) get(ty) + set(ty)
cat cat push(tailpos)
}
}
}
{ "0,0" push(tailpos) uniq(tailpos) count(tailpos) }
)
def(abs) '-' => ""
def(sign) /[1-9]\d*/ => "1"
def(R) { uncons(x) 1 + cons(x) }
def(L) { uncons(x) 1 - cons(x) }
def(U) { uncons(y) 1 - cons(y) }
def(D) { uncons(y) 1 + cons(y) }
#(
fx { 10 set(ropelen) times { 0 push(x) 0 push(y) } }
fx /(\w) (\d+)/ => {
$2 times {
$1 do
uncons(x) set(prevx) push(x)
uncons(y) set(prevy) push(y)
get(ropelen) 1 - times {
uncons(x) set(currx) drop
uncons(y) set(curry) drop
get(currx) get(prevx) - do(abs) 1 >>
get(curry) get(prevy) - do(abs) 1 >>
+ if {
get(prevx) get(currx) - do(sign) get(currx) + set(currx) drop
get(prevy) get(curry) - do(sign) get(curry) + set(curry) drop
}
get(currx) set(prevx) push(x)
get(curry) set(prevy) push(y)
}
pop(x) copy push(x)
","
pop(y) copy push(y)
cat cat push(tailpos)
}
}
{ uniq(tailpos) count(tailpos) }
)
def(wait) fx {
get(cycle) 1 + set(cycle)
40 % 20 = if {
get(cycle) get(X) *
get(total) + set(total)
}
}
(
/^/ => fx { 1 set(X) }
/addx (.*)/ => { do(wait) do(wait) get(X) $1 + set(X) clear }
'noop' => { do(wait) clear }
/./s => ""
/$/ => { get(total) }
)
def(wait) {
inc(cycle)
get(cycle) 1 - 40 % get(X) - copy * 2 <<
if { "#" } { " " }
get(cycle) 40 % 0 = if { "\n" }
}
(
/^/ => fx { 1 set(X) }
/addx (.*)/ => { do(wait) do(wait) get(X) $1 + set(X) drop}
'noop' => { do(wait) }
/./s => ""
)
! round toward zero
def(trunc) /\.\d*/ => ""
! evaluate formula
def(operate) #(
'old' => { get(old) }
/(\d+) \* (\d+)/ => { $1 $2 * }
/(\d+) \+ (\d+)/ => { $1 $2 + }
)
#(
! capture input
fx (
/Monkey \d+/ => { _ set(currmonkey) inc(monkeys) }
/Starting items: .+/ => /\d+/ => { _ get(currmonkey) " items" cat push }
/Operation: new = (.+)/ => { $1 push(operation) }
/Test: divisible by (\d+)/ => { $1 push(divisor) }
/If true: throw to monkey (\d+)/ => { $1 push(truetarget) }
/If false: throw to monkey (\d+)/ => { $1 push(falsetarget) }
)
{
! run simulation
20 times {
0 set(i) drop
get(monkeys) times {
"Monkey " get(i) " items" cat cat for {
get(i) push(target)
_ set(old) drop
get(i) getat(operation) do(operate)
3 / do(trunc)
copy get(i) getat(divisor) % 0 = if
{ get(i) getat(truetarget) }
{ get(i) getat(falsetarget) }
"Monkey " swap " items" cat cat push
}
"Monkey " get(i) " items" cat cat empty
inc(i)
}
}
! sort toss targets and print them all flat
sort(target) for(target) { _ " " }
}
! group toss targets
/\b(\d+ )(?!\1)/ => { $1 "\n" }
! count targets within each group
/.+/ => #(
fx { 0 set(count) }
/\d+/ => fx { inc(count) }
{ get(count) 6 zpad }
)
! find the hottest toss targets
/.+/ => fx { _ push(inspections) }
{ sort(inspections) pop(inspections) pop(inspections) * }
)
! round toward zero
def(trunc) /\.\d*/ => ""
! evaluate formula
def(operate) #(
'old' => { get(old) }
/(\d+) \* (\d+)/ => { $1 $2 * }
/(\d+) \+ (\d+)/ => { $1 $2 + }
)
#(
! capture input
fx { 1 set(alldivisors) }
fx (
/Monkey \d+/ => { _ set(currmonkey) inc(monkeys) }
/Starting items: .+/ => /\d+/ => { _ get(currmonkey) " items" cat push }
/Operation: new = (.+)/ => { $1 push(operation) }
/Test: divisible by (\d+)/ => {
$1 push(divisor)
get(alldivisors) $1 * set(alldivisors)
}
/If true: throw to monkey (\d+)/ => { $1 push(truetarget) }
/If false: throw to monkey (\d+)/ => { $1 push(falsetarget) }
)
{
! run simulation
10000 times {
0 set(i) drop
get(monkeys) times {
"Monkey " get(i) " items" cat cat for {
"target " get(i) cat inc
_ set(old) drop
get(i) getat(operation) do(operate)
get(alldivisors) %
copy get(i) getat(divisor) % 0 = if
{ get(i) getat(truetarget) }
{ get(i) getat(falsetarget) }
"Monkey " swap " items" cat cat push
}
"Monkey " get(i) " items" cat cat empty
inc(i)
}
}
! find the hottest monkey inspectors
0 set(i) drop get(monkeys) times {
"target " get(i) cat get 6 zpad push(inspections)
inc(i)
}
sort(inspections)
pop(inspections) pop(inspections) *
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment