├── README.md └── src ├── main └── java │ └── year2019 │ ├── Problem01.java │ └── Problem02.java └── y2018 ├── day5 └── Day5.java └── day2 └── Day2.java /README.md: -------------------------------------------------------------------------------- 1 | # Advent of C-C-C-Code 2 | -------------------------------------------------------------------------------- /src/main/java/year2019/Problem01.java: -------------------------------------------------------------------------------- 1 | package year2019; 2 | 3 | import java.io.IOException; 4 | import java.net.URISyntaxException; 5 | import java.nio.file.Files; 6 | import java.nio.file.Path; 7 | import java.nio.file.Paths; 8 | import java.util.function.IntFunction; 9 | import java.util.stream.IntStream; 10 | import java.util.stream.Stream; 11 | 12 | public class Problem01 { 13 | 14 | public static void main(String[] args) throws URISyntaxException, IOException { 15 | final Path path = Paths.get(Problem01.class.getClassLoader() 16 | .getResource("20191201.adventofcode").toURI()); 17 | 18 | try (final Stream lines = Files.lines(path)) { 19 | doProblem(lines); 20 | } 21 | doProblem(Stream.of("1969")); 22 | doProblem(Stream.of("100756")); 23 | } 24 | 25 | private static void doProblem(Stream initialStream) { 26 | final int sumofRequirements = initialStream 27 | .mapToInt(Integer::parseInt) 28 | .flatMap(Problem01::calculateFuel) 29 | .sum(); 30 | 31 | System.out.format("Initial cost: %d\n", sumofRequirements); 32 | } 33 | 34 | private static IntStream calculateFuel(int moduleMass) { 35 | return IntStream.iterate(moduleMass, Problem01::requiresFuel, Problem01::simpleSum) 36 | .skip(1); 37 | } 38 | 39 | private static boolean requiresFuel(int moduleMass) { 40 | return 0 < moduleMass; 41 | } 42 | 43 | private static int simpleSum(int moduleMass) { 44 | return (moduleMass / 3) - 2; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/y2018/day5/Day5.java: -------------------------------------------------------------------------------- 1 | package y2018.day5; 2 | 3 | import java.io.IOException; 4 | import java.nio.ByteBuffer; 5 | import java.nio.channels.FileChannel; 6 | import java.nio.file.Path; 7 | import java.nio.file.Paths; 8 | import java.util.Stack; 9 | 10 | public class Day5 { 11 | 12 | public static void main(String[] args) throws IOException { 13 | 14 | final Path p = Paths.get(args[0]); 15 | int min; 16 | 17 | try (FileChannel channel = FileChannel.open(p)) { 18 | min = part1(channel, '1', '1'); 19 | 20 | System.out.format("Base is %d\n", min); 21 | } 22 | 23 | for (int test = 'a'; test <= 'z'; ++test) { 24 | final char uppercase = (char) (test - 32); 25 | // bleh 26 | try (FileChannel channel = FileChannel.open(p)) { 27 | int newMin = part1(channel, (char) test, uppercase); 28 | 29 | if (newMin < min) { 30 | min = newMin; 31 | System.out.format("New min is %d\n", min); 32 | } 33 | 34 | } 35 | 36 | } 37 | } 38 | 39 | private static int part1(FileChannel channel, char ignore1, char ignore2) throws IOException { 40 | 41 | final Stack stack = new Stack<>(); 42 | 43 | final ByteBuffer bb = ByteBuffer.allocate(1024); 44 | 45 | while (channel.read(bb) > 0) { 46 | bb.flip(); 47 | 48 | while (bb.hasRemaining()) { 49 | final char c = (char) bb.get(); 50 | 51 | if ('\n' == c || ignore1 == c || ignore2 == c) { 52 | continue; 53 | } 54 | 55 | Character top = stack.isEmpty() ? null : stack.peek(); 56 | if (comparable(top, c)) { 57 | stack.pop(); 58 | } else { 59 | stack.push(c); 60 | } 61 | } 62 | 63 | bb.clear(); 64 | } 65 | 66 | return stack.size(); 67 | } 68 | 69 | private static boolean comparable(Character top, char buf) { 70 | if (top == null) { 71 | return false; 72 | } 73 | return Math.abs(top.charValue() - buf) == 32; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/year2019/Problem02.java: -------------------------------------------------------------------------------- 1 | package year2019; 2 | 3 | import java.io.IOException; 4 | import java.net.URISyntaxException; 5 | import java.nio.file.Files; 6 | import java.nio.file.Path; 7 | import java.nio.file.Paths; 8 | import java.util.Optional; 9 | import java.util.function.IntBinaryOperator; 10 | import java.util.stream.Stream; 11 | 12 | public class Problem02 { 13 | 14 | public static void main(String[] args) throws URISyntaxException { 15 | final Path path = Paths.get(Problem01.class.getClassLoader() 16 | .getResource("20191202.adventofcode").toURI()); 17 | 18 | try (final Stream lines = Files.lines(path)) { 19 | final String line = lines.findFirst().get(); 20 | doProblem(line, 12, 2); 21 | 22 | // find 19690720 23 | for (int noun = 0; noun < 100; ++noun) { 24 | for (int verb = 0; verb < 100; ++verb) { 25 | if (19690720 == doProblem(line, noun, verb)) { 26 | System.out.format("Found noun %d and verb %d: %d", noun, verb, 100 * noun + verb); 27 | return; 28 | } 29 | } 30 | } 31 | } catch (IOException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | 36 | public static int doProblem(String line, int one, int two) { 37 | final int[] array = Stream.of(line.split(",")) 38 | .mapToInt(Integer::parseInt) 39 | .toArray(); 40 | 41 | array[1] = one; 42 | array[2] = two; 43 | 44 | for (int i = 0; i < array.length; i += 4) { 45 | final Optional operation = getOperation(array[i]); 46 | if (operation.isPresent()) { 47 | final int param1Loc = array[i + 1]; 48 | final int param2Loc = array[i + 2]; 49 | final int resultLoc = array[i + 3]; 50 | 51 | array[resultLoc] = operation.get().applyAsInt(array[param1Loc], array[param2Loc]); 52 | } else { 53 | System.out.println(array[0]); 54 | break; 55 | } 56 | } 57 | return array[0]; 58 | } 59 | 60 | private static Optional getOperation(int op) { 61 | switch (op) { 62 | case 1: 63 | return Optional.of((a, b) -> a + b); 64 | case 2: 65 | return Optional.of((a, b) -> a * b); 66 | default: 67 | return Optional.empty(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/y2018/day2/Day2.java: -------------------------------------------------------------------------------- 1 | package y2018.day2; 2 | 3 | import java.io.IOException; 4 | import java.nio.ByteBuffer; 5 | import java.nio.channels.FileChannel; 6 | import java.nio.file.Path; 7 | import java.nio.file.Paths; 8 | import java.util.ArrayList; 9 | import java.util.Arrays; 10 | import java.util.List; 11 | 12 | public class Day2 { 13 | 14 | private static class LineTracker { 15 | private static final int MAGIC = 97; 16 | 17 | final int[] base = new int[26]; 18 | final int[] letterTracker = new int[26]; 19 | 20 | private void reset() { 21 | System.arraycopy(base, 0, letterTracker, 0, 26); 22 | } 23 | 24 | int numberOf(int match) { 25 | int count = 0; 26 | for (int i = 0; i < letterTracker.length; ++i) { 27 | if (letterTracker[i] == match) { 28 | ++count; 29 | } 30 | } 31 | return count; 32 | } 33 | } 34 | 35 | public static void main(String[] args) { 36 | 37 | final Path p = Paths.get(args[0]); 38 | 39 | try (FileChannel channel = FileChannel.open(p)) { 40 | final List lines = part1(channel); 41 | 42 | //part 2 43 | for (int i = 0; i < lines.size(); ++i) { 44 | for (int j = 0; j < lines.size(); ++j) { 45 | if (i != j) { 46 | // remove a character and compare. 47 | for (int k = 0; k < lines.get(i).length(); ++k) { 48 | final String iPrime = removeChar(lines.get(i), k); 49 | final String jPrime = removeChar(lines.get(j), k); 50 | 51 | if (iPrime.equals(jPrime)) { 52 | System.out.println(iPrime); 53 | return; 54 | } 55 | } 56 | } 57 | } 58 | } 59 | 60 | } catch (IOException e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | 65 | private static String removeChar(String str, int indexToRemove) { 66 | return str.substring(0, indexToRemove) + str.substring(indexToRemove + 1); 67 | } 68 | 69 | private static List part1(FileChannel channel) throws IOException { 70 | 71 | final List fileContents = new ArrayList<>(); 72 | 73 | final ByteBuffer bb = ByteBuffer.allocate(1024); 74 | final LineTracker tracker = new LineTracker(); 75 | 76 | final int[] twosThrees = new int[]{0, 0}; 77 | final StringBuilder sb = new StringBuilder(); 78 | 79 | while (channel.read(bb) > 0) { 80 | bb.flip(); 81 | 82 | while (bb.hasRemaining()) { 83 | final char c = (char) bb.get(); 84 | switch (c) { 85 | case '\n': 86 | if (tracker.numberOf(2) > 0) { 87 | twosThrees[0]++; 88 | } 89 | if (tracker.numberOf(3) > 0) { 90 | twosThrees[1]++; 91 | } 92 | tracker.reset(); 93 | 94 | fileContents.add(sb.toString()); 95 | sb.setLength(0); 96 | break; 97 | default: 98 | sb.append(c); 99 | tracker.letterTracker[c - LineTracker.MAGIC] += 1; 100 | } 101 | } 102 | 103 | bb.clear(); 104 | } 105 | System.out.println(Arrays.toString(twosThrees) + " = " + (twosThrees[0] * twosThrees[1])); 106 | 107 | return fileContents; 108 | } 109 | } 110 | --------------------------------------------------------------------------------