Advent of Code 2021: Day 9 
Tags: adventofcode, java
Published: 2021-12-23 11:44:12

## Puzzle
--- Day 9: Smoke Basin ---

These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.

If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).

Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:

2199943210
3987894921
9856789892
8767896789
9899965678
Each number corresponds to the height of a particular location, where 9 is the highest and 0 is the lowest a location can be.

Your first goal is to find the low points - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)

In the above example, there are four low points, all highlighted: two are in the first row (a 1 and a 0), one is in the third row (a 5), and one is in the bottom row (also a 5). All other locations on the heightmap have some lower adjacent location, and so are not low points.

The risk level of a low point is 1 plus its height. In the above example, the risk levels of the low points are 2, 1, 6, and 6. The sum of the risk levels of all low points in the heightmap is therefore 15.

Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?*

## My solution

```java
package main;

import java.io.File;
import java.util.*;

public class Puzzle {
  public static void main(String[] args) throws Exception {
    File input = new File("/Users/files/input.txt");
    Scanner scanner = new Scanner(input);

    List<List<Integer>> values = new ArrayList<List<Integer>>();
    while (scanner.hasNextLine()) {
      String inputLine = scanner.nextLine();
      String[] string = inputLine.split("");
      List<String> str = new ArrayList<String>();
      str = Arrays.asList(string);
      List<Integer> numbers = new ArrayList<>();
      for (int i = 0; i < str.size(); i++) {
        numbers.add(Integer.parseInt(str.get(i)));
      }
      ;
      values.add(numbers);
    }

    List<Integer> lowestValues = new ArrayList<>();

    for (int i = 0; i < values.size(); i++) {
      for (int j = 0; j < values.get(i).size(); j++) {

        if (i == 0 && j == 0) {
          if (values.get(i).get(j) < values.get(i).get(j + 1)
              && values.get(i).get(j) < values.get(i + 1).get(j)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if (i == 0 && j == values.get(i).size() - 1) {
          if (values.get(i).get(j) < values.get(i).get(j - 1)
              && values.get(i).get(j) < values.get(i + 1).get(j)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if (i == 0 && (j < values.get(i).size() - 1 && j > 0)) {
          if (values.get(i).get(j) < values.get(i).get(j - 1)
              && values.get(i).get(j) < values.get(i).get(j + 1)
              && values.get(i).get(j) < values.get(i + 1).get(j)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if (i == values.size() - 1 && j == 0) {
          if (values.get(i).get(j) < values.get(i - 1).get(j)
              && values.get(i).get(j) < values.get(i).get(j + 1)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if ((i == values.size() - 1) && (j == values.get(i).size() - 1)) {
          if (values.get(i).get(j) < values.get(i - 1).get(j)
              && values.get(i).get(j) < values.get(i).get(j - 1)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if (i == values.size() - 1 && (j < values.get(i).size() - 1 && j > 0)) {
          if (values.get(i).get(j) < values.get(i).get(j - 1)
              && values.get(i).get(j) < values.get(i).get(j + 1)
              && values.get(i).get(j) < values.get(i - 1).get(j)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if (i > 0 && i < values.size() - 1 && j == 0) {
          if (values.get(i).get(j) < values.get(i - 1).get(j)
              && values.get(i).get(j) < values.get(i + 1).get(j)
              && values.get(i).get(j) < values.get(i).get(j + 1)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else if ((i > 0 && i < values.size() - 1) && j == values.get(i).size() - 1) {
          if (values.get(i).get(j) < values.get(i - 1).get(j)
              && values.get(i).get(j) < values.get(i + 1).get(j)
              && values.get(i).get(j) < values.get(i).get(j - 1)) {
            lowestValues.add(values.get(i).get(j));
          }
        } else {

          if (values.get(i).get(j) < values.get(i - 1).get(j)
              && values.get(i).get(j) < values.get(i + 1).get(j)
              && values.get(i).get(j) < values.get(i).get(j - 1)
              && values.get(i).get(j) < values.get(i).get(j + 1)) {
            lowestValues.add(values.get(i).get(j));
          }
        }
      }
    }
    for (int k = 0; k < lowestValues.size(); k++) {
      lowestValues.set(k, lowestValues.get(k) + 1);
    }
    int sum = lowestValues.stream().mapToInt(Integer::intValue).sum();

    System.out.println(sum);
  }
}

```
_*Source: https://adventofcode.com/2021/day/9_