This repository was archived by the owner on Aug 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.java
More file actions
60 lines (53 loc) · 2.19 KB
/
Copy pathDay1.java
File metadata and controls
60 lines (53 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Day1 {
public static void main(String[] args) throws FileNotFoundException {
// RULES:
// Every elf keeps a log of how many calories they are carrying in a file
// Every line is a food item, elves separate themselves with blank newlines
// Example:
// 100
// 200
//
// 540
//
// The first elf has 300 calories, the second has 540.
// Part 1 Objective: Find how many calories the elf with the most calories has
// Part 2 Objective: Find how many calories the top 3 elves with the most calories have
Scanner calorieLog = new Scanner(new FileReader("inputs/input1.txt"));
int firstCalories = 0;
int firstElf = 0;
int secondCalories = 0;
int secondElf = 0;
int thirdCalories = 0;
int thirdElf = 0;
int elf = 0;
while (calorieLog.hasNextLine()) {
elf++;
int calories = 0;
while (calorieLog.hasNextLine()) {
String line = calorieLog.nextLine();
if (line.isBlank()) break;
int number = Integer.parseInt(line);
if (number != 0) calories += number;
}
if (calories > firstCalories) {
firstCalories = calories;
firstElf = elf;
} else if (calories > secondCalories) {
secondCalories = calories;
secondElf = elf;
} else if (calories > thirdCalories) {
thirdCalories = calories;
thirdElf = elf;
}
System.out.println("Elf " + elf + " has " + calories + " calories.");
}
System.out.println("Calorie rankings:");
System.out.println("1. Elf " + firstElf + " with " + firstCalories + " calories.");
System.out.println("2. Elf " + secondElf + " with " + secondCalories + " calories.");
System.out.println("3. Elf " + thirdElf + " with " + thirdCalories + " calories.");
System.out.println("Sum of top 3: " + (firstCalories + secondCalories + thirdCalories));
}
}