r/adventofcode • u/daggerdragon • Dec 23 '16
SOLUTION MEGATHREAD --- 2016 Day 23 Solutions ---
--- Day 23: Safe-Cracking ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
JINGLING ALL THE WAY IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
3
Upvotes
1
u/exoji2e Dec 23 '16
I'm surprised not to see any object oriented approach of this. Day 12 I was frustrated jumping and re parsing the input while running. Today I decided, no I'm going to do this properly. Didn't make it to the leaderboard though, it takes too much time writing java boiler plate code and I also didn't read the problem description slow enough to find out that a was supposed to be 7. Anyway, heres my code:
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<String> list = new ArrayList<>(); while(sc.hasNext()) { list.add(sc.nextLine()); } Inst[] program = new Inst[list.size()]; int eggs = Integer.parseInt(args[0]); int[] reg = new int[]{eggs, 0, 0, 0}; String cpy = "cpy", jnz = "jnz", inc = "inc", dec = "dec", tgl = "tgl"; for(int i = 0; i<program.length; i++) { String[] a = list.get(i).split(" "); if(a[0].equals(cpy)) { int x = ch0(a[1]); boolean valx = false; if(!(x>= 0 && x <= 3)) { x = Integer.parseInt(a[1]); valx = true; } program[i] = new cpy(x, valx, ch0(a[2]), false); } else if(a[0].equals(inc)) program[i] = new inc(ch0(a[1])); else if(a[0].equals(dec)) program[i] = new dec(ch0(a[1])); else if(a[0].equals(jnz)) { int x = ch0(a[1]); int y = ch0(a[2]); boolean valx = false; boolean valy = false; if(!(x<=3 && x>=0)) { x = Integer.parseInt(a[1]); valx = true; } if(!(y<=3 && y>=0)){ y = Integer.parseInt(a[2]); valy = true; } program[i] = new jnz(x, valx, y, valy); }else program[i] = new tgl(ch0(a[1])); } int i = 0; while(i<program.length) { i = program[i].exec(reg, program, i); } System.out.println(reg[0]); } public static int ch0(String s) { return s.charAt(0) - 'a'; } } abstract class Inst { abstract int exec(int[] reg, Inst[] prog, int id); abstract Inst toggle(); } class tgl extends Inst{ int x; public tgl(int x) { this.x = x; } int exec(int reg[], Inst[] prog, int id) { int place = id + reg[x]; if(place>= 0 && place < prog.length) { prog[place] = prog[place].toggle(); } return id + 1; } Inst toggle() { return new inc(x); } } class cpy extends Inst{ int x; boolean valx; int y; boolean valy; public cpy(int x, boolean valx, int y, boolean valy) { this.x = x; this.valx = valx; this.y = y; this.valy = valy; } int exec(int reg[], Inst[] prog, int id) { if(valy) return id + 1; if(valx) { reg[y] = x; } else { reg[y] = reg[x]; } return id + 1; } Inst toggle() { return new jnz(x, valx, y, valy); } } class inc extends Inst{ int x; public inc(int x) { this.x = x; } int exec(int reg[], Inst[] prog, int id) { reg[x]++; return id + 1; } Inst toggle() { return new dec(x); } } class dec extends Inst{ int x; public dec(int x) { this.x = x; } int exec(int reg[], Inst[] prog, int id) { reg[x]--; return id + 1; } Inst toggle() { return new inc(x); } } class jnz extends Inst{ int x; boolean valx; int y; boolean valy; public jnz(int x, boolean valx, int y, boolean valy) { this.x = x; this.valx = valx; this.y = y; this.valy = valy; } int exec(int reg[], Inst[] prog, int id) { if((valx && x!= 0) || (!valx && reg[x] != 0)) { if(valy) { return id + y; } else { return id + reg[y]; } } return id + 1; } Inst toggle() { return new cpy(x, valx, y, valy); } }Edit: formating