Last active
December 7, 2016 21:37
-
-
Save flupe/5571cc276e394a2fb565b70a814074cf to your computer and use it in GitHub Desktop.
advent of code 2016, day 7, part 1
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.s = s.q = 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