From 4c523d9e3494d75a05894643555f95468c91fe56 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Sun, 3 Dec 2023 22:18:17 +0000 Subject: Day 03 part 1 --- day03/day03.java | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 day03/day03.java (limited to 'day03/day03.java') diff --git a/day03/day03.java b/day03/day03.java new file mode 100644 index 0000000..4d4caa9 --- /dev/null +++ b/day03/day03.java @@ -0,0 +1,117 @@ +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +class Number { + int row, colstart, colend, value; + + public Number(int row, int colstart, int colend, int value) { + this.row = row; + this.colstart = colstart; + this.colend = colend; + this.value = value; + } + + public String toString() { + return "[" + this.row + ", " + this.colstart + "-" + this.colend + "]: " + this.value; + } +} + +class Symbol { + int row, col; + String symbol; + + public Symbol(int row, int col, String symbol) { + this.row = row; + this.col = col; + this.symbol = symbol; + } + + public String toString() { + return "[" + this.row + ", " + this.col + "]: " + this.symbol; + } +} + +public class day03 { + public static void main(String args[]) throws IOException { + RandomAccessFile input = new RandomAccessFile("input", "r"); + System.out.println("Day 01 Part 1: " + part1(input)); + input.seek(0); + System.out.println("Day 01 Part 2: " + part2(input)); + input.close(); + } + + public static String part1(RandomAccessFile input) throws IOException { + String line; + int row = 0; + ArrayList numbers = new ArrayList<>(); + ArrayList symbols = new ArrayList<>(); + + while ((line = input.readLine()) != null) { + getNumbers(numbers, row, line); + getSymbols(symbols, row, line); + row += 1; + } + + int tot = 0; + for (Number num: numbers) { + boolean part = false; + for (Symbol sym: symbols) { + for (int col = num.colstart - 1; col <= num.colend + 1; col++) { + if (sym.row == num.row - 1 && sym.col == col) { + part = true; + } + if (sym.row == num.row + 1 && sym.col == col) { + part = true; + } + } + if (sym.row == num.row && sym.col == num.colstart - 1) { + part = true; + } + if (sym.row == num.row && sym.col == num.colend + 1) { + part = true; + } + } + if (part) { + tot += num.value; + } + } + + return Integer.toString(tot); + } + + private static void getNumbers(ArrayList numbers, int row, String line) { + Pattern pat = Pattern.compile("\\d+"); + Matcher mat = pat.matcher(line); + while (mat.find()) { + numbers.add( + new Number( + row, + mat.start(), + mat.end() - 1, + Integer.parseInt(mat.group()) + ) + ); + } + } + + private static void getSymbols(ArrayList symbols, int row, String line) { + Pattern pat = Pattern.compile("[^0-9.]"); + Matcher mat = pat.matcher(line); + while (mat.find()) { + symbols.add( + new Symbol( + row, + mat.start(), + mat.group() + ) + ); + } + } + + public static String part2(RandomAccessFile input) throws IOException { + return "WIP"; + } +} -- cgit v1.2.1