Advertisement
musifter

AoC 2021 day 10 (dc)

Dec 10th, 2021
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #!/usr/bin/dc
  2.  
  3. [q] sX
  4.  
  5. # calculate syntax scoring table
  6. 3 d 1:s
  7. 19* d 2:s
  8. 21* d 3:s
  9. 21* 4:s
  10.  
  11. 0 ss # syntax score = 0
  12.  
  13. [ lt;s ls+ ss 3Q ] sS # syntax error: add value from table, exit 3 levels
  14.  
  15. [ 4- 0 ] sO # open token: tok exp-stack -> 0 close-tok exp-stack
  16. [ lt !=S ] sC # close token: (tok != exp-top, syntax) exp-rest
  17.  
  18. [
  19. 10~ st sl # line exp-stack -> exp-stack (t = token, l = rest of line)
  20. lt d 4<O # exp -> (tok > 4, open) tok exp, open returns: 0 exp
  21. 0!=C # else ((tok|0) != 0 closed) exp
  22.  
  23. ll d 0!=P # exp -> line exp (loop if line not zero)
  24. ] sP
  25.  
  26. [
  27. c 0 ? d 0=X # clear stack, get input line, exit if 0
  28. rs. # remove sentinel zero
  29. lPx # attempt parse
  30. lMx # loop
  31. ] sM
  32. lMx
  33.  
  34. [Part 1: ]n lsp
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement