Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. let fs = require('fs')
  2.  
  3. function step(c) {
  4. let st = state
  5. let {s, q, x, y} = st
  6. if (c=='[' || c==']') {
  7. st.s = s^1
  8. st.q = 0
  9. }
  10. else if (q == 1) {
  11. if (x == c) {
  12. st.q = 1
  13. }
  14. else {
  15. st.q = 2
  16. st.x = c
  17. }
  18. }
  19. else if (q == 2) {
  20. if (y == c) {
  21. st.q = 3
  22. }
  23. else {
  24. st.q = 2
  25. st.x = y
  26. st.y = c
  27. }
  28. }
  29. else if (x == c) {
  30. st.s = s & 1 ? 4 : 2
  31. st.q = 2
  32. st.x = y
  33. st.y = c
  34. }
  35. else if (q == 0 || y == c) {
  36. st.q = 1
  37. st.x = c
  38. }
  39. else {
  40. st.q = 2
  41. st.x = y
  42. st.y = c
  43. }
  44. }
  45.  
  46. let state = {q: 0, s: 0, x: 0, y: 0}
  47. console.log(fs.readFileSync('./data/7.txt').toString().trim().split('\n')
  48. .reduce((count, ip) => {
  49. let s = state
  50. s.q = s.s = s.x = s.y = 0
  51. let i = 0, c = ip.length
  52. while (i < c && (s.s & 4) == 0) {
  53. step(ip[i])
  54. i++
  55. }
  56. return count + ((s.s >> 1) == 1)
  57. }, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement