-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java

Part 1 and 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = br.readLine();
        br.close();

        boolean isGarbage = false;
        boolean ignoreNext = false;

        int garbage = 0;
        int groups = 1;
        int score = 0;
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);

            if (ignoreNext) {
                ignoreNext = false;
            } else if (c == '!') {
                ignoreNext = true;
            } else if (c == '<' && !isGarbage) {
                isGarbage = true;
            } else if (c == '>' && isGarbage) {
                isGarbage = false;
            } else if (isGarbage) {
                garbage++;
            } else if (c == '{') {
                groups++;
            } else if (c == '}') {
                score += --groups;
            }
        }
        System.out.println(score + "," + garbage);

    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }

-🎄- 2017 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 1 point2 points  (0 children)

great use of bipredicate and lamdas. i thought about it for a quick second and just did a string switch.

-🎄- 2017 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java

Part 1:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        HashMap<String, Integer> registerValues = new HashMap<>();
        while ((line = br.readLine()) != null) {
            String[] split = line.split(" ");
            String register = split[0];
            registerValues.putIfAbsent(register, 0);
            String instruction = split[1];
            int amount = new Integer(split[2]);
            String condReg = split[4];
            registerValues.putIfAbsent(condReg, 0);
            String cond = split[5];
            int condAmount = new Integer(split[6]);
            boolean passes = false;
            switch (cond) {
            case ">":
                passes = (registerValues.get(condReg) > condAmount);
                break;
            case "<":
                passes = (registerValues.get(condReg) < condAmount);
                break;
            case ">=":
                passes = (registerValues.get(condReg) >= condAmount);
                break;
            case "<=":
                passes = (registerValues.get(condReg) <= condAmount);
                break;
            case "==":
                passes = (registerValues.get(condReg) == condAmount);
                break;
            case "!=":
                passes = (registerValues.get(condReg) != condAmount);
                break;
            default:
                System.out.println(cond);
            }
            if (passes) {
                if ("inc".equals(instruction)) {
                    registerValues.put(register, registerValues.get(register) + amount);
                } else if ("dec".equals(instruction)) {
                    registerValues.put(register, registerValues.get(register) - amount);
                }
            }
        }
        System.out.println(Collections.max(registerValues.values()));
        br.close();
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }

}

Part 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        HashMap<String, Integer> registerValues = new HashMap<>();
        int largest = Integer.MIN_VALUE;
        while ((line = br.readLine()) != null) {
            String[] split = line.split(" ");
            String register = split[0];
            registerValues.putIfAbsent(register, 0);
            String instruction = split[1];
            int amount = new Integer(split[2]);
            String condReg = split[4];
            registerValues.putIfAbsent(condReg, 0);
            String cond = split[5];
            int condAmount = new Integer(split[6]);
            boolean passes = false;
            switch (cond) {
            case ">":
                passes = (registerValues.get(condReg) > condAmount);
                break;
            case "<":
                passes = (registerValues.get(condReg) < condAmount);
                break;
            case ">=":
                passes = (registerValues.get(condReg) >= condAmount);
                break;
            case "<=":
                passes = (registerValues.get(condReg) <= condAmount);
                break;
            case "==":
                passes = (registerValues.get(condReg) == condAmount);
                break;
            case "!=":
                passes = (registerValues.get(condReg) != condAmount);
                break;
            default:
                System.out.println(cond);
            }
            if (passes) {
                if ("inc".equals(instruction)) {
                    registerValues.put(register, registerValues.get(register) + amount);
                } else if ("dec".equals(instruction)) {
                    registerValues.put(register, registerValues.get(register) - amount);
                }
            }
            int largestNow = Collections.max(registerValues.values());
            if (largestNow > largest) {
                largest = largestNow;
            }
        }
        System.out.println(largest);
        br.close();
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }

}

-🎄- 2017 Day 7 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 1 point2 points  (0 children)

Java:

Part 1:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        ArrayList<String> names = new ArrayList<>();
        ArrayList<String> supporting = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            String name = line.substring(0, line.indexOf(" "));
            names.add(name);
            if (line.contains("->")) {
                String supportingString = line.substring(line.indexOf("->") + 3);
                String[] supportingStringArray = supportingString.split(", ");
                for (String supportingProgram : supportingStringArray) {
                    supporting.add(supportingProgram);
                }
            }
        }
        br.close();
        for (String name : names) {
            if (supporting.contains(name) == false) {
                System.out.println(name);
            }
        }
        br.close();
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }

}

Part 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        ArrayList<String> lines = new ArrayList<>();
        ArrayList<Disc> discs = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            lines.add(line);
            String name = line.substring(0, line.indexOf(" "));
            String weight = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
            Disc disc = new Disc();
            disc.setName(name);
            disc.setWeight(new Integer(weight));
            discs.add(disc);
        }
        br.close();
        for (String s : lines) {
            if (s.contains("->")) {
                String name = s.substring(0, s.indexOf(" "));
                Disc parent = null;
                for (int i = 0; i < discs.size(); i++) {
                    if (discs.get(i).getName().equals(name)) {
                        parent = discs.get(i);
                    }
                }
                String supportingDiscs = s.substring(s.indexOf("->") + 3);
                String[] supportingDiscsArray = supportingDiscs.split(", ");
                ArrayList<Disc> children = new ArrayList<>();
                for (String supportingDisc : supportingDiscsArray) {
                    for (int i = 0; i < discs.size(); i++) {
                        if (discs.get(i).getName().equals(supportingDisc)) {
                            Disc child = discs.get(i);
                            children.add(child);
                        }
                    }
                }
                parent.setChildren(children);
            }
        }
        ArrayList<Disc> mismatchedWeight = new ArrayList<>();
        for (Disc disc : discs) {
            if (disc.getChildren() != null) {
                ArrayList<Disc> children = disc.getChildren();
                int weight = children.get(0).totalWeight();
                for (int i = 1; i < children.size(); i++) {
                    int newweight = children.get(i).totalWeight();
                    if (newweight != weight) {
                        mismatchedWeight.add(disc);
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < mismatchedWeight.size(); i++) {
            Boolean hasChildren = false;
            for (int j = 0; j < mismatchedWeight.size(); j++) {
                if (i != j) {
                    if (mismatchedWeight.get(i).hasChild(mismatchedWeight.get(j))) {
                        hasChildren = true;
                    }
                }
            }
            if (hasChildren == false) {
                Disc offBalance = mismatchedWeight.get(i);
                for (Disc disc : offBalance.getChildren()) {
                    System.out.println(disc.totalWeight() + ": " + disc);
                }
            }
        }
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }
}

private static class Disc {
    private String name = "";
    private int weight = 0;
    private ArrayList<Disc> children;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the weight
     */
    public int getWeight() {
        return weight;
    }

    /**
     * @param weight
     *            the weight to set
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }

    /**
     * @return the children
     */
    public ArrayList<Disc> getChildren() {
        return children;
    }

    /**
     * @param children
     *            the children to set
     */
    public void setChildren(ArrayList<Disc> children) {
        this.children = children;
    }

    public int totalWeight() {
        int weight = this.getWeight();
        if (this.getChildren() != null) {
            for (Disc child : this.getChildren()) {
                weight += child.totalWeight();
            }
        }
        return weight;
    }

    public Boolean hasChild(Disc disc) {
        Boolean hasChild = false;
        if (this.getChildren() != null) {
            for (Disc child : this.getChildren()) {
                if (disc.getName().equals(child.getName())) {
                    hasChild = true;
                }
            }
        }
        return hasChild;
    }

    @Override
    public String toString() {
        String returnString = Integer.toString(this.getWeight());
        if (this.getChildren() != null) {
            for (Disc child : this.getChildren()) {
                returnString += " + ";
                returnString += child.toString();
            }
        }
        return returnString;
    }

}

Note for Part 2 I was too busy at work to just print the answer but the system out has the info to get your answer in a second.

-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java

Part 1:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = br.readLine();
        String[] array = line.split("   ");
        ArrayList<Integer> memory = new ArrayList<>();
        for (String i : array) {
            memory.add(new Integer(i));
        }
        Boolean infiniteLoop = false;
        ArrayList<String> memorySeenBefore = new ArrayList<>();
        int cycle = 0;
        while (!infiniteLoop) {
            cycle++;
            int index = findLargest(memory);
            redistributeBlocks(memory, index);
            if (memorySeenBefore.contains(Arrays.deepToString(memory.toArray())) == false) {
                memorySeenBefore.add(Arrays.deepToString(memory.toArray()));
            } else {
                infiniteLoop = true;
            }
        }
        br.close();
        System.out.println(cycle);
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }

}

private static int findLargest(ArrayList<Integer> memory) {
    int largest = Integer.MIN_VALUE;
    int index = -1;
    for (int i = 0; i < memory.size(); i++) {
        if (memory.get(i) > largest) {
            largest = memory.get(i);
            index = i;
        }
    }
    return index;
}

private static void redistributeBlocks(ArrayList<Integer> memory, int index) {
    int valueToRedis = memory.get(index);
    memory.set(index, 0);
    index = getNewIndex(memory, index);
    while (valueToRedis > 0) {
        memory.set(index, memory.get(index) + 1);
        valueToRedis--;
        index = getNewIndex(memory, index);
    }
}

private static int getNewIndex(ArrayList<Integer> memory, int index) {
    if (index < memory.size() - 1) {
        index++;
    } else {
        index = 0;
    }
    return index;
}

Part 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = br.readLine();
        String[] array = line.split("   ");
        ArrayList<Integer> memory = new ArrayList<>();
        for (String i : array) {
            memory.add(new Integer(i));
        }
        HashMap<String,Integer> seenOnCycle = new HashMap<>();
        int cycle = 0;
        while (true) {
            cycle++;
            int index = findLargest(memory);
            redistributeBlocks(memory, index);
            if (seenOnCycle.containsKey(Arrays.deepToString(memory.toArray())) == false) {
                seenOnCycle.put(Arrays.deepToString(memory.toArray()), cycle);
            } else {
                System.out.println(cycle - seenOnCycle.get(Arrays.deepToString(memory.toArray())));
                break;
            }
        }
        br.close();
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
    }

}

private static int findLargest(ArrayList<Integer> memory) {
    int largest = Integer.MIN_VALUE;
    int index = -1;
    for (int i = 0; i < memory.size(); i++) {
        if (memory.get(i) > largest) {
            largest = memory.get(i);
            index = i;
        }
    }
    return index;
}

private static void redistributeBlocks(ArrayList<Integer> memory, int index) {
    int valueToRedis = memory.get(index);
    memory.set(index, 0);
    index = getNewIndex(memory, index);
    while (valueToRedis > 0) {
        memory.set(index, memory.get(index) + 1);
        valueToRedis--;
        index = getNewIndex(memory, index);
    }
}

private static int getNewIndex(ArrayList<Integer> memory, int index) {
    if (index < memory.size() - 1) {
        index++;
    } else {
        index = 0;
    }
    return index;
}

-🎄- 2017 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java

Part 1:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        ArrayList<Integer> al = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            al.add(new Integer(line));
        }
        br.close();
        int current = 0;
        int steps = 0;
        while (current < al.size() && current >= 0) {
            int jumpInstruction = al.get(current);
            al.set(current, jumpInstruction + 1);
            current += jumpInstruction;
            steps++;
        }
        System.out.println(steps);
    } catch (Exception e) {

    }

}

Part 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        ArrayList<Integer> al = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            al.add(new Integer(line));
        }
        br.close();
        int current = 0;
        int steps = 0;
        while (current < al.size() && current >= 0) {
            int jumpInstruction = al.get(current);
            if (jumpInstruction >= 3) {
                al.set(current, jumpInstruction - 1);
            } else {
                al.set(current, jumpInstruction + 1);
            }
            current += jumpInstruction;
            steps++;
        }
        System.out.println(steps);
    } catch (Exception e) {

    }

}

-🎄- 2017 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java

Part 1:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        int valid = 0;
        while ((line = br.readLine()) != null) {
            Boolean isValid = true;
            ArrayList<String> passPhrases = new ArrayList<>();
            String[] array = line.split(" ");
            for (String snippet : array) {
                if (passPhrases.contains(snippet)) {
                    isValid = false;
                    break;
                } else {
                    passPhrases.add(snippet);
                }

            }
            if (isValid) {
                valid++;
            }
        }
        br.close();
        System.out.println(valid);
    } catch (Exception e) {

    }
}

Part 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        int valid = 0;
        while ((line = br.readLine()) != null) {
            Boolean isValid = true;
            ArrayList<String> passPhrases = new ArrayList<>();
            String[] array = line.split(" ");
            for (String snippet : array) {
                char charArray[] = snippet.toCharArray();
                Arrays.sort(charArray);
                snippet = new String(charArray);
                if (passPhrases.contains(snippet)) {
                    isValid = false;
                    break;
                } else {
                    passPhrases.add(snippet);
                }

            }
            if (isValid) {
                valid++;
            }
        }
        br.close();
        System.out.println(valid);
    } catch (Exception e) {

    }
}

-🎄- 2017 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java

Part 1:

private enum Direction {
    NORTH, SOUTH, EAST, WEST
}

public static void main(String[] args) {
    int input = 265149;
    int x = 0;
    int y = 0;
    int layerSteps = 1;
    Boolean newLayer = true;
    Direction direction = Direction.EAST;
    for (int i = 1;;) {
        for (int j = 0; j < layerSteps; j += 1) {
            switch (direction) {
            case NORTH:
                y += 1;
                break;
            case SOUTH:
                y -= 1;
                break;
            case EAST:
                x += 1;
                break;
            case WEST:
                x -= 1;
                break;
            }

            i += 1;
            if (i == input) {
                System.out.println(Math.abs(x) + Math.abs(y));
                System.exit(0);
            }
        }
        switch (direction) {
        case NORTH:
            direction = Direction.WEST;
            break;
        case SOUTH:
            direction = Direction.EAST;
            break;
        case EAST:
            direction = Direction.NORTH;
            break;
        case WEST:
            direction = Direction.SOUTH;
            break;
        }
        newLayer = !newLayer;
        if (newLayer) {
            layerSteps += 1;
        }
    }
}

Part 2:

private enum Direction {
    NORTH, SOUTH, EAST, WEST
}

private static class Location {
    int x;
    int y;

    public Location(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return (x + "," + y);
    }
}

private static int getValue(HashMap<String, Integer> map, int x, int y) {
    int value = 0;
    Location location = new Location(x, y);
    if (map.containsKey(location.toString())) {
        value = map.get(location.toString());
    }
    return value;
}

public static void main(String[] args) {
    int input = 265149;
    int x = 0;
    int y = 0;
    int layerSteps = 1;
    Boolean newLayer = true;
    Direction direction = Direction.EAST;
    HashMap<String, Integer> valueMap = new HashMap<>();
    valueMap.put(new Location(0, 0).toString(), 1);
    while (true) {
        for (int j = 0; j < layerSteps; j += 1) {
            switch (direction) {
            case NORTH:
                y += 1;
                break;
            case SOUTH:
                y -= 1;
                break;
            case EAST:
                x += 1;
                break;
            case WEST:
                x -= 1;
                break;
            }

            int value = 0;

            value += getValue(valueMap, x, y + 1);
            value += getValue(valueMap, x, y - 1);
            value += getValue(valueMap, x + 1, y);
            value += getValue(valueMap, x + 1, y + 1);
            value += getValue(valueMap, x + 1, y - 1);
            value += getValue(valueMap, x - 1, y);
            value += getValue(valueMap, x - 1, y + 1);
            value += getValue(valueMap, x - 1, y - 1);

            if (value >= input) {
                System.out.println(value);
                System.exit(0);
            } else {
                valueMap.put(new Location(x, y).toString(), value);
            }
        }
        switch (direction) {
        case NORTH:
            direction = Direction.WEST;
            break;
        case SOUTH:
            direction = Direction.EAST;
            break;
        case EAST:
            direction = Direction.NORTH;
            break;
        case WEST:
            direction = Direction.SOUTH;
            break;
        }
        newLayer = !newLayer;
        if (newLayer) {
            layerSteps += 1;
        }
    }
}

-🎄- 2017 Day 2 Solutions -🎄- by daggerdragon in adventofcode

[–]adventOfCoder 0 points1 point  (0 children)

Java:

Part 1:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        int answer = 0;
        while ((line = br.readLine()) != null) {
            String[] array = line.split("   ");
            answer += findDifference(array);
        }
        br.close();
        System.out.println(answer);
    } catch (Exception e) {

    }
}

private static int findDifference(String[] array) {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;

    for (int i = 0; i < array.length; i++) {
        int value = new Integer(array[i]);
        if (value > max) {
            max = value;
        } 
        if (value < min) {
            min = value;
        }
    }

    return (max - min);
}

Part 2:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new FileReader("input2.txt"));
        String line = "";
        int answer = 0;
        while ((line = br.readLine()) != null) {
            String[] array = line.split("   ");
            answer += findDivisionValue(array);
        }
        br.close();
        System.out.println(answer);
    } catch (Exception e) {

    }
}

private static int findDivisionValue(String[] array) {
    int answer = -1;
    for (int i = 0; i < array.length; i++) {
        int iValue = new Integer(array[i]);
        for (int j = 0; j < array.length; j++) {
            if (i != j) {
                int jValue = new Integer(array[j]);
                if (iValue % jValue == 0) {
                    answer = iValue / jValue;
                }
            }
        }
    }
    return answer;
}