|
| 1 | +package by.andd3dfx.game; |
| 2 | + |
| 3 | +import lombok.SneakyThrows; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | + |
| 10 | +/** |
| 11 | + * <a href="https://adventofcode.com/2015/day/1">Task definition</a> |
| 12 | + * <pre> |
| 13 | + * --- Day 1: Not Quite Lisp --- |
| 14 | + * |
| 15 | + * Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th. |
| 16 | + * |
| 17 | + * Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! |
| 18 | + * |
| 19 | + * Here's an easy puzzle to warm you up. |
| 20 | + * |
| 21 | + * Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time. |
| 22 | + * |
| 23 | + * An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor. |
| 24 | + * |
| 25 | + * The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors. |
| 26 | + * |
| 27 | + * For example: |
| 28 | + * |
| 29 | + * (()) and ()() both result in floor 0. |
| 30 | + * ((( and (()(()( both result in floor 3. |
| 31 | + * ))((((( also results in floor 3. |
| 32 | + * ()) and ))( both result in floor -1 (the first basement level). |
| 33 | + * ))) and )())()) both result in floor -3. |
| 34 | + * |
| 35 | + * To what floor do the instructions take Santa? |
| 36 | + * </pre> |
| 37 | + */ |
| 38 | +public class NotQuiteLisp { |
| 39 | + |
| 40 | + @SneakyThrows |
| 41 | + public static int determineFloor(String input) { |
| 42 | + var result = 0; |
| 43 | + for (var ch : input.toCharArray()) { |
| 44 | + switch (ch) { |
| 45 | + case '(': |
| 46 | + result++; |
| 47 | + break; |
| 48 | + case ')': |
| 49 | + result--; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + @SneakyThrows |
| 58 | + public static void main(String[] args) { |
| 59 | + var inputString = readFromInputStream("/game/not-quite-lisp.txt"); |
| 60 | + var result = determineFloor(inputString); |
| 61 | + System.out.println(result); |
| 62 | + } |
| 63 | + |
| 64 | + private static String readFromInputStream(String filePathName) throws IOException { |
| 65 | + InputStream inputStream = NotQuiteLisp.class.getResourceAsStream(filePathName); |
| 66 | + var resultStringBuilder = new StringBuilder(); |
| 67 | + try (var br = new BufferedReader(new InputStreamReader(inputStream))) { |
| 68 | + String line; |
| 69 | + while ((line = br.readLine()) != null) { |
| 70 | + resultStringBuilder.append(line).append("\n"); |
| 71 | + } |
| 72 | + } |
| 73 | + return resultStringBuilder.toString(); |
| 74 | + } |
| 75 | +} |
0 commit comments