From b3521f6253d41616d23d95182a8c9049b2d56f7a Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 1 Dec 2025 20:44:46 -0500 Subject: [PATCH] day 1 in the books --- main.java.tmpl | 21 +++++++++++++-- src/Day01.java | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ today.sh | 10 ++++---- 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/Day01.java diff --git a/main.java.tmpl b/main.java.tmpl index c427344..fc6fbea 100644 --- a/main.java.tmpl +++ b/main.java.tmpl @@ -5,8 +5,25 @@ import java.util.Scanner; public class Day00 { public static void main(String[] args) { - try (Scanner scanner = new Scanner(new FileReader("resources/00/input")).useDelimiter("\\R")) { - StringBuilder sb = new StringBuilder(); + part1(); + part2(); + } + + private static void part1() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/00/input")).useDelimiter("\\R")) { + while (scanner.hasNext()) { + sb.append(scanner.next()); + sb.append("\n"); + } + String input = sb.toString(); + System.out.println(input); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + private static void part2() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/00/input")).useDelimiter("\\R")) { while (scanner.hasNext()) { sb.append(scanner.next()); sb.append("\n"); diff --git a/src/Day01.java b/src/Day01.java new file mode 100644 index 0000000..f15a9c8 --- /dev/null +++ b/src/Day01.java @@ -0,0 +1,69 @@ +package src; + +import java.io.FileReader; +import java.util.Scanner; + +public class Day01 { + public static void main(String[] args) { + part1(); + part2(); + } + + private static void part1() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/01/input")).useDelimiter("\\R")) { + int dial = 50; + int password = 0; + while (scanner.hasNext()) { + String instruction = scanner.next(); + int change = Integer.parseInt(instruction.substring(1)); + switch (instruction.charAt(0)) { + case 'L' -> { + dial = (dial - change + 100) % 100; + } + case 'R' -> { + dial = (dial + change) % 100; + } + } + if (dial == 0) { + password += 1; + } + } + System.out.println(password); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + // this is the inefficient way to do it, but it's also accurate! + private static void part2() { + try (Scanner scanner = new Scanner(new FileReader("resources/inputs/01/input")).useDelimiter("\\R")) { + int dial = 50; + int password = 0; + while (scanner.hasNext()) { + String instruction = scanner.next(); + int change = Integer.parseInt(instruction.substring(1)); + switch (instruction.charAt(0)) { + case 'L' -> { + for (int i = 0; i < change; i++) { + dial = (dial - 101) % 100; + if (dial == 0) { + password += 1; + } + } + } + case 'R' -> { + for (int i = 0; i < change; i++) { + dial = (dial + 1) % 100; + if (dial == 0) { + password += 1; + } + } + } + } + } + System.out.println(password); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/today.sh b/today.sh index 0a8f2b9..fea9b34 100755 --- a/today.sh +++ b/today.sh @@ -1,6 +1,6 @@ #!/bin/bash -__year=2024 +__year=2025 __day="${1}" if [ "${__day}" == "" ]; then @@ -8,15 +8,15 @@ if [ "${__day}" == "" ]; then fi padded=$(printf "%02g" ${__day}) -mkdir -p ${padded}/inputs -touch ${padded}/inputs/testinput -if [ ! -f "${padded}/inputs/input" ]; then +mkdir -p src/resources/inputs/${padded} +touch src/resources/inputs/${padded}/testinput +if [ ! -f "src/resources/inputs/${padded}/input" ]; then curl -s \ -A "https://git.yetaga.in/alazyreader/AdventOfCode${__year}/" \ -H "Cookie: session=`cat .cookie`" https://adventofcode.com/${__year}/day/${__day##0}/input > "src/resources/inputs/${padded}/input" fi if [ ! -f "src/Day${padded}.java" ]; then cp main.java.tmpl src/Day${padded}.java - gsed -i '' "s/00/${padded}/g" src/Day${padded}.java + sed -i '' "s/00/${padded}/g" src/Day${padded}.java fi open "https://adventofcode.com/${__year}/day/${__day##0}"