Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let fs = require('fs')
- function step(c) {
- let st = state
- let {s, q, x, y} = st
- if (c=='[' || c==']') {
- st.s = s^1
- st.q = 0
- }
- else if (q == 1) {
- if (x == c) {
- st.q = 1
- }
- else {
- st.q = 2
- st.x = c
- }
- }
- else if (q == 2) {
- if (y == c) {
- st.q = 3
- }
- else {
- st.q = 2
- st.x = y
- st.y = c
- }
- }
- else if (x == c) {
- st.s = s & 1 ? 4 : 2
- st.q = 2
- st.x = y
- st.y = c
- }
- else if (q == 0 || y == c) {
- st.q = 1
- st.x = c
- }
- else {
- st.q = 2
- st.x = y
- st.y = c
- }
- }
- let state = {q: 0, s: 0, x: 0, y: 0}
- console.log(fs.readFileSync('./data/7.txt').toString().trim().split('\n')
- .reduce((count, ip) => {
- let s = state
- s.q = s.s = s.x = s.y = 0
- let i = 0, c = ip.length
- while (i < c && (s.s & 4) == 0) {
- step(ip[i])
- i++
- }
- return count + ((s.s >> 1) == 1)
- }, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement