Advertisement
Guest User

AoC, day 5, edc

a guest
Dec 6th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. 0k
  2.  
  3. # embedded perl to read line from stdin and push ASCII values of the characters
  4. { $_ = <>; chomp; return (map {ord} split //) } s?
  5.  
  6. [q] sQ # quit loop
  7. [1+] s+ # increment
  8.  
  9. 0 s1 # variable for storing result of part 1
  10. [ ln s1 ] sS # used for getting max, sets $1 to $n
  11.  
  12. #
  13. # Macro to convert ASCII values on stack into seat number in $n
  14. #
  15. [
  16. z 0 =Q # break if stack empty
  17. 0r # put a zero under the top item to calculate bit on
  18. 8% 4 >+ # using that the bit we want is not of bit 3 in ASCII
  19. ln 2*+ sn # shift n and add bit to bottom
  20. lCx # loop
  21. ] sC
  22.  
  23. #
  24. # Mainline
  25. #
  26. [
  27. l?x # grab input line as pushed ASCII values
  28. z 0 =Q # quit if we don't get any more
  29.  
  30. 0 sn lCx # init $n, then call C to calculate seat in it
  31.  
  32. ln d # push two copies of $n
  33. l1 <S # first: get max of $n and $1
  34. 1 r:a # second: used set a[$n] = 1
  35.  
  36. lMx # loop
  37. ] sM
  38. lMx
  39.  
  40. [Part 1: ]n l1p
  41.  
  42. # Find seat for part 2 (assuming nice input where all seats contiguous but 1)
  43. 1- s2 # take answer from part 1, decrement and store in $2
  44. [
  45. l2 ;a 0 =Q # break if a[$2] = 0 (empty seat)
  46. l2 1- s2 # decrement $2
  47. lIx # loop
  48. ] sI
  49. lIx
  50.  
  51. [Part 2: ]n l2p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement