DEV Community

Cover image for Advent of Code 2021 - Day 1
Petra
Petra

Posted on

Advent of Code 2021 - Day 1

Hi all!

This is my solution of the first puzzle of Advent of Code on Day 1, in Java.

package main;

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

public class Puzzle {

    public static void main(String[] {
        try {
            File input = new File("/Users/files/input.txt");
            Scanner scanner = new Scanner(input);
            List<Integer> numbers = new ArrayList<>();

            while (scanner.hasNextLine())
                numbers.add(Integer.parseInt(scanner.nextLine()));


            int count = 0;

            for (int i = 1; i < numbers.size(); i++) {
                if (numbers.get(i) > numbers.get(i - 1)) {
                    count++;
                }
            }

            System.out.println(count);
        } catch (Exception e) {
            System.out.println("Exception!");
        }

    }

}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)