Advertisement
Guest User

Day 7

a guest
Dec 7th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Stack;
  6.  
  7. /**
  8. * Created by mdawg on 12/4/2015.
  9. */
  10. public class Day1 {
  11.  
  12. public static Map<String, Integer> VARS = new HashMap<>();
  13. public static ArrayList<String> INSN = new ArrayList<>();
  14.  
  15. public static void main(String[] args) throws IOException {
  16. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("./input.txt")));
  17. String str;
  18. while ((str = reader.readLine()) != null) {
  19. String[] strs = str.split(" ");
  20. if (strs.length == 3) {
  21. try {
  22. int i = Integer.parseInt(strs[0]);
  23. VARS.put(strs[2], i);
  24. continue;
  25. } catch (Exception e) {
  26. }
  27. }
  28. INSN.add(str);
  29. }
  30.  
  31. doVar("a");
  32.  
  33. System.out.println(VARS.get("a"));
  34. }
  35.  
  36. public static String doVar(String name) {
  37. if (VARS.get(name) != null) {
  38. return name;
  39. } else {
  40. for (String string : INSN) {
  41. String[] split = string.split(" ");
  42. if (split[split.length - 1].equals(name)) {
  43. System.out.println(string);
  44. String str = (split[split.length - 1]);
  45. if (split.length == 3) {
  46. VARS.put(str, getValue(split[0]));
  47. } else if (split[0].equals("NOT")) {
  48. VARS.put(str, (~getValue(split[1])) & 0xFFFF);
  49. } else {
  50. switch (split[1]) {
  51. case "OR":
  52. VARS.put(str, getValue(split[0]) | getValue(split[2]));
  53. break;
  54. case "AND":
  55. VARS.put(str, getValue(split[0]) & getValue(split[2]));
  56. break;
  57. case "LSHIFT":
  58. VARS.put(str, getValue(split[0]) << getValue(split[2]));
  59. break;
  60. case "RSHIFT":
  61. VARS.put(str, getValue(split[0]) >> getValue(split[2]));
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return name;
  69. }
  70.  
  71. public static int getValue(String input) {
  72. try {
  73. return Integer.parseInt(input);
  74. } catch (Exception e) {
  75. doVar(input);
  76. return VARS.get(input);
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement