diff options
| author | Nat Lasseter <user@4574.co.uk> | 2023-12-04 09:30:35 +0000 | 
|---|---|---|
| committer | Nat Lasseter <user@4574.co.uk> | 2023-12-04 09:30:35 +0000 | 
| commit | 793c779e687f3ecf2aa4365dfc35a71b326da29d (patch) | |
| tree | 19a12958196e4b43f03d1a06febc7c34eccf129f /day03 | |
| parent | 4c523d9e3494d75a05894643555f95468c91fe56 (diff) | |
Day 03
Diffstat (limited to 'day03')
| -rw-r--r-- | day03/day03.java | 110 | 
1 files changed, 78 insertions, 32 deletions
| diff --git a/day03/day03.java b/day03/day03.java index 4d4caa9..b9f8ecd 100644 --- a/day03/day03.java +++ b/day03/day03.java @@ -4,36 +4,6 @@ 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"); @@ -82,6 +52,52 @@ public class day03 {      return Integer.toString(tot);    } +  public static String part2(RandomAccessFile input) throws IOException { +    String line; +    int row = 0; +    ArrayList<Number> numbers = new ArrayList<>(); +    ArrayList<Symbol> symbols = new ArrayList<>(); + +    while ((line = input.readLine()) != null) { +      getNumbers(numbers, row, line); +      getSymbols(symbols, row, line); +      row += 1; +    } + +    int tot = 0; +    for (Symbol sym : symbols) { +      if (!sym.symbol.equals("*")) { +        continue; +      } + +      ArrayList<Number> neighs = new ArrayList<>(); + +      for (Number num: numbers) { +        if (num.contains(sym.row - 1, sym.col - 1) || +            num.contains(sym.row - 1, sym.col    ) || +            num.contains(sym.row - 1, sym.col + 1) || +            num.contains(sym.row    , sym.col - 1) || +            num.contains(sym.row    , sym.col + 1) || +            num.contains(sym.row + 1, sym.col - 1) || +            num.contains(sym.row + 1, sym.col    ) || +            num.contains(sym.row + 1, sym.col + 1)) { +          if (!neighs.contains(num)) { +            neighs.add(num); +          } +        } +      } + +      if (!(neighs.size() == 2)) { +        continue; +      } + +      int ratio = neighs.get(0).value * neighs.get(1).value; +      tot += ratio; +    } + +    return Integer.toString(tot); +  } +    private static void getNumbers(ArrayList<Number> numbers, int row, String line) {      Pattern pat = Pattern.compile("\\d+");      Matcher mat = pat.matcher(line); @@ -110,8 +126,38 @@ public class day03 {        );      }    } +} -  public static String part2(RandomAccessFile input) throws IOException { -    return "WIP"; +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 boolean contains(int row, int col) { +    return (row == this.row) && (this.colstart <= col && col <= this.colend); +  } + +  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;    }  } | 
