Day 21
This commit is contained in:
		
							
								
								
									
										100
									
								
								day21/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								day21/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,100 @@
 | 
				
			|||||||
 | 
					move position 0 to position 3
 | 
				
			||||||
 | 
					rotate right 0 steps
 | 
				
			||||||
 | 
					rotate right 1 step
 | 
				
			||||||
 | 
					move position 1 to position 5
 | 
				
			||||||
 | 
					swap letter h with letter b
 | 
				
			||||||
 | 
					reverse positions 1 through 3
 | 
				
			||||||
 | 
					swap letter a with letter g
 | 
				
			||||||
 | 
					swap letter b with letter h
 | 
				
			||||||
 | 
					rotate based on position of letter c
 | 
				
			||||||
 | 
					swap letter d with letter c
 | 
				
			||||||
 | 
					rotate based on position of letter c
 | 
				
			||||||
 | 
					swap position 6 with position 5
 | 
				
			||||||
 | 
					rotate right 7 steps
 | 
				
			||||||
 | 
					swap letter b with letter h
 | 
				
			||||||
 | 
					move position 4 to position 3
 | 
				
			||||||
 | 
					swap position 1 with position 0
 | 
				
			||||||
 | 
					swap position 7 with position 5
 | 
				
			||||||
 | 
					move position 7 to position 1
 | 
				
			||||||
 | 
					swap letter c with letter a
 | 
				
			||||||
 | 
					move position 7 to position 5
 | 
				
			||||||
 | 
					rotate right 4 steps
 | 
				
			||||||
 | 
					swap position 0 with position 5
 | 
				
			||||||
 | 
					move position 3 to position 1
 | 
				
			||||||
 | 
					swap letter c with letter h
 | 
				
			||||||
 | 
					rotate based on position of letter d
 | 
				
			||||||
 | 
					reverse positions 0 through 2
 | 
				
			||||||
 | 
					rotate based on position of letter g
 | 
				
			||||||
 | 
					move position 6 to position 7
 | 
				
			||||||
 | 
					move position 2 to position 5
 | 
				
			||||||
 | 
					swap position 1 with position 0
 | 
				
			||||||
 | 
					swap letter f with letter c
 | 
				
			||||||
 | 
					rotate right 1 step
 | 
				
			||||||
 | 
					reverse positions 2 through 4
 | 
				
			||||||
 | 
					rotate left 1 step
 | 
				
			||||||
 | 
					rotate based on position of letter h
 | 
				
			||||||
 | 
					rotate right 1 step
 | 
				
			||||||
 | 
					rotate right 5 steps
 | 
				
			||||||
 | 
					swap position 6 with position 3
 | 
				
			||||||
 | 
					move position 0 to position 5
 | 
				
			||||||
 | 
					swap letter g with letter f
 | 
				
			||||||
 | 
					reverse positions 2 through 7
 | 
				
			||||||
 | 
					reverse positions 4 through 6
 | 
				
			||||||
 | 
					swap position 4 with position 1
 | 
				
			||||||
 | 
					move position 2 to position 1
 | 
				
			||||||
 | 
					move position 3 to position 1
 | 
				
			||||||
 | 
					swap letter b with letter a
 | 
				
			||||||
 | 
					rotate based on position of letter b
 | 
				
			||||||
 | 
					reverse positions 3 through 5
 | 
				
			||||||
 | 
					move position 0 to position 2
 | 
				
			||||||
 | 
					rotate based on position of letter b
 | 
				
			||||||
 | 
					reverse positions 4 through 5
 | 
				
			||||||
 | 
					rotate based on position of letter g
 | 
				
			||||||
 | 
					reverse positions 0 through 5
 | 
				
			||||||
 | 
					swap letter h with letter c
 | 
				
			||||||
 | 
					reverse positions 2 through 5
 | 
				
			||||||
 | 
					swap position 7 with position 5
 | 
				
			||||||
 | 
					swap letter g with letter d
 | 
				
			||||||
 | 
					swap letter d with letter e
 | 
				
			||||||
 | 
					move position 1 to position 2
 | 
				
			||||||
 | 
					move position 3 to position 2
 | 
				
			||||||
 | 
					swap letter d with letter g
 | 
				
			||||||
 | 
					swap position 3 with position 7
 | 
				
			||||||
 | 
					swap letter b with letter f
 | 
				
			||||||
 | 
					rotate right 3 steps
 | 
				
			||||||
 | 
					move position 5 to position 3
 | 
				
			||||||
 | 
					move position 1 to position 2
 | 
				
			||||||
 | 
					rotate based on position of letter b
 | 
				
			||||||
 | 
					rotate based on position of letter c
 | 
				
			||||||
 | 
					reverse positions 2 through 3
 | 
				
			||||||
 | 
					move position 2 to position 3
 | 
				
			||||||
 | 
					rotate right 1 step
 | 
				
			||||||
 | 
					move position 7 to position 0
 | 
				
			||||||
 | 
					rotate right 3 steps
 | 
				
			||||||
 | 
					move position 6 to position 3
 | 
				
			||||||
 | 
					rotate based on position of letter e
 | 
				
			||||||
 | 
					swap letter c with letter b
 | 
				
			||||||
 | 
					swap letter f with letter d
 | 
				
			||||||
 | 
					swap position 2 with position 5
 | 
				
			||||||
 | 
					swap letter f with letter g
 | 
				
			||||||
 | 
					rotate based on position of letter a
 | 
				
			||||||
 | 
					reverse positions 3 through 4
 | 
				
			||||||
 | 
					rotate left 7 steps
 | 
				
			||||||
 | 
					rotate left 6 steps
 | 
				
			||||||
 | 
					swap letter g with letter b
 | 
				
			||||||
 | 
					reverse positions 3 through 6
 | 
				
			||||||
 | 
					rotate right 6 steps
 | 
				
			||||||
 | 
					rotate based on position of letter c
 | 
				
			||||||
 | 
					rotate based on position of letter b
 | 
				
			||||||
 | 
					rotate left 1 step
 | 
				
			||||||
 | 
					reverse positions 3 through 7
 | 
				
			||||||
 | 
					swap letter f with letter g
 | 
				
			||||||
 | 
					swap position 4 with position 1
 | 
				
			||||||
 | 
					rotate based on position of letter d
 | 
				
			||||||
 | 
					move position 0 to position 4
 | 
				
			||||||
 | 
					swap position 7 with position 6
 | 
				
			||||||
 | 
					rotate right 6 steps
 | 
				
			||||||
 | 
					rotate based on position of letter e
 | 
				
			||||||
 | 
					move position 7 to position 3
 | 
				
			||||||
 | 
					rotate right 3 steps
 | 
				
			||||||
 | 
					swap position 1 with position 2
 | 
				
			||||||
							
								
								
									
										32
									
								
								day21/part1.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								day21/part1.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					swapPos = (str, a, b)=>{
 | 
				
			||||||
 | 
						str = str.split("");
 | 
				
			||||||
 | 
						temp = str[a];
 | 
				
			||||||
 | 
						str[a] = str[b];
 | 
				
			||||||
 | 
						str[b] = temp;
 | 
				
			||||||
 | 
						return str.join("");
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					swapLetter = (str, a, b) => (str.split("").map((char)=>((char == a) ? (b) : ((char == b) ? (a) : (char)))).join(""));
 | 
				
			||||||
 | 
					rotate = (str, steps) => (str.split("").map((a, i)=>(str[(i + steps + str.length)%(str.length)])).join("")); //left pos, right neg
 | 
				
			||||||
 | 
					rotatePos = (str, char) => (str.split("").map((a, i)=>(str[(i - str.indexOf(char) - ((str.indexOf(char) >= 4) ? (2) : (1)) + (str.length*2))%(str.length)])).join(""));
 | 
				
			||||||
 | 
					reverseBetween = (str, a, b) => (str.split("").map((char, i)=>(((i >= a) && (i <= b)) ? (str[b - (i - a)]) : (char))).join(""));
 | 
				
			||||||
 | 
					moveToPos = (str, a, b) => {
 | 
				
			||||||
 | 
						var str = str.split("");
 | 
				
			||||||
 | 
						var char = str.splice(a, 1)[0];
 | 
				
			||||||
 | 
						str.splice(b, 0, char);
 | 
				
			||||||
 | 
						return str.join("");
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
 | 
				
			||||||
 | 
					var instructions = input.split("\n").filter((a)=>(a)).map((str)=>(
 | 
				
			||||||
 | 
						str
 | 
				
			||||||
 | 
						.replace(/swap position ([0-9]+) with position ([0-9]+)/g, "swapPos(text, $1, $2)")
 | 
				
			||||||
 | 
						.replace(/swap letter (.) with letter (.)/g, "swapLetter(text, \"$1\", \"$2\")")
 | 
				
			||||||
 | 
						.replace(/rotate left ([0-9]+) step(s)?/g, "rotate(text, $1)")
 | 
				
			||||||
 | 
						.replace(/rotate right ([0-9]+) step(s)?/g, "rotate(text, -$1)")
 | 
				
			||||||
 | 
						.replace(/rotate based on position of letter (.)/g, "rotatePos(text, \"$1\")")
 | 
				
			||||||
 | 
						.replace(/reverse positions ([0-9]+) through ([0-9]+)/g, "reverseBetween(text, $1, $2)")
 | 
				
			||||||
 | 
						.replace(/move position ([0-9]+) to position ([0-9]+)/g, "moveToPos(text, $1, $2)")
 | 
				
			||||||
 | 
					));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var out = instructions.reduce((text, instruction)=>(eval(instruction)), "abcdefgh")
 | 
				
			||||||
 | 
					console.log(out);
 | 
				
			||||||
							
								
								
									
										32
									
								
								day21/part2.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								day21/part2.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					swapPos = (str, a, b)=>{
 | 
				
			||||||
 | 
						str = str.split("");
 | 
				
			||||||
 | 
						temp = str[a];
 | 
				
			||||||
 | 
						str[a] = str[b];
 | 
				
			||||||
 | 
						str[b] = temp;
 | 
				
			||||||
 | 
						return str.join("");
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					swapLetter = (str, a, b) => (str.split("").map((char)=>((char == a) ? (b) : ((char == b) ? (a) : (char)))).join(""));
 | 
				
			||||||
 | 
					rotate = (str, steps) => (str.split("").map((a, i)=>(str[(i + steps + str.length)%(str.length)])).join("")); //left pos, right neg
 | 
				
			||||||
 | 
					reverseRotatePos = (str, char) => (str.split("").map((a, i)=>(str[(i + ((str.indexOf(char)%2 == 0) ? (/*even*/ (((str.indexOf(char) == 0) ? (str.length) : (str.indexOf(char)))/2) + 5) : (/*odd*/ Math.ceil((str.indexOf(char))/2))) + (str.length*2))%(str.length)])).join(""));
 | 
				
			||||||
 | 
					reverseBetween = (str, a, b) => (str.split("").map((char, i)=>(((i >= a) && (i <= b)) ? (str[b - (i - a)]) : (char))).join(""));
 | 
				
			||||||
 | 
					moveToPos = (str, a, b) => {
 | 
				
			||||||
 | 
						var str = str.split("");
 | 
				
			||||||
 | 
						var char = str.splice(a, 1)[0];
 | 
				
			||||||
 | 
						str.splice(b, 0, char);
 | 
				
			||||||
 | 
						return str.join("");
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
 | 
				
			||||||
 | 
					var instructions = input.split("\n").reverse().filter((a)=>(a)).map((str)=>(
 | 
				
			||||||
 | 
						str
 | 
				
			||||||
 | 
						.replace(/swap position ([0-9]+) with position ([0-9]+)/g, "swapPos(text, $1, $2)")
 | 
				
			||||||
 | 
						.replace(/swap letter (.) with letter (.)/g, "swapLetter(text, \"$1\", \"$2\")")
 | 
				
			||||||
 | 
						.replace(/rotate left ([0-9]+) step(s)?/g, "rotate(text, -$1)")
 | 
				
			||||||
 | 
						.replace(/rotate right ([0-9]+) step(s)?/g, "rotate(text, $1)")
 | 
				
			||||||
 | 
						.replace(/rotate based on position of letter (.)/g, "reverseRotatePos(text, \"$1\")")
 | 
				
			||||||
 | 
						.replace(/reverse positions ([0-9]+) through ([0-9]+)/g, "reverseBetween(text, $1, $2)")
 | 
				
			||||||
 | 
						.replace(/move position ([0-9]+) to position ([0-9]+)/g, "moveToPos(text, $2, $1)")
 | 
				
			||||||
 | 
					));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var out = instructions.reduce((text, instruction)=>(eval(instruction)), "fbgdceah")
 | 
				
			||||||
 | 
					console.log(out);
 | 
				
			||||||
		Reference in New Issue
	
	Block a user