Created
December 7, 2016 21:28
-
-
Save flupe/f5a2219b010bb112bac48412d84b79c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment