-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Raghu Kokku
committed
Nov 8, 2024
1 parent
8452c26
commit b08fdb2
Showing
13 changed files
with
674 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package adventofcode; | ||
|
||
import java.util.List; | ||
|
||
public class CampCleanup { | ||
public static void main(String[] args) { | ||
List<String> fileContents = CodeUtility.readFile("/Users/raghu.kokku/code/AdventOfCode/CampCleanup.txt"); | ||
|
||
int count =0; | ||
int overlapCount = 0; | ||
|
||
|
||
for (String line : fileContents ) { | ||
if (isPair(line)) { | ||
count++; | ||
} | ||
if (doesOverlap(line)) { | ||
overlapCount++; | ||
} | ||
} | ||
|
||
System.out.printf("Count = %d\n", count); | ||
System.out.printf("Count of overlap = %d", overlapCount); | ||
|
||
} | ||
|
||
private static boolean isPair(String line) { | ||
String[] pairs = line.split(","); | ||
|
||
String[] firstPair = pairs[0].split("-"); | ||
String[] secondPair = pairs[1].split("-"); | ||
|
||
int firstNumberInPair1 = Integer.valueOf(firstPair[0]); | ||
int secondNumberInPair1 = Integer.valueOf(firstPair[1]); | ||
int firstNumberInPair2 = Integer.valueOf(secondPair[0]); | ||
int secondNumberInPair2 = Integer.valueOf(secondPair[1]); | ||
|
||
return ((firstNumberInPair1 <= firstNumberInPair2 && secondNumberInPair1 >= secondNumberInPair2) || | ||
((firstNumberInPair1 >= firstNumberInPair2 && secondNumberInPair1 <= secondNumberInPair2))); | ||
} | ||
|
||
private static boolean doesOverlap(String line) { | ||
String[] pairs = line.split(","); | ||
|
||
String[] firstPair = pairs[0].split("-"); | ||
String[] secondPair = pairs[1].split("-"); | ||
|
||
int firstNumberInPair1 = Integer.valueOf(firstPair[0]); | ||
int secondNumberInPair1 = Integer.valueOf(firstPair[1]); | ||
int firstNumberInPair2 = Integer.valueOf(secondPair[0]); | ||
int secondNumberInPair2 = Integer.valueOf(secondPair[1]); | ||
|
||
return (firstNumberInPair2 >= firstNumberInPair1 && firstNumberInPair2 <= secondNumberInPair1) || | ||
(firstNumberInPair1 >= firstNumberInPair2 && firstNumberInPair1 <= secondNumberInPair2) || | ||
(secondNumberInPair1 == secondNumberInPair2); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package adventofcode; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
public class CodeUtility { | ||
|
||
protected static List<String> readFile(String filePath) { | ||
try { | ||
return Files.readAllLines(Paths.get(filePath)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package adventofcode; | ||
|
||
import java.util.LinkedList; | ||
|
||
class Graph { | ||
int vertex; | ||
LinkedList<String> list[]; | ||
|
||
public Graph (int vertex) { | ||
this.vertex = vertex; | ||
list = new LinkedList[vertex]; | ||
|
||
for(int i=0; i<vertex; i++) { | ||
list[i] = new LinkedList<>(); | ||
} | ||
} | ||
|
||
public void addEdge(String source, String destination) { | ||
list[source].add(destination); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
public class Commands { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package adventofcode; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.Collections; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
/* | ||
* 1) Read all lines from an input file. | ||
* 2) Iterate through each line and sum the values until you encounter new line | ||
* 3) Calculate the max as we sum the values for each elf | ||
*/ | ||
|
||
public class ElfCalorieCounter { | ||
|
||
public static void main(String[] args) { | ||
|
||
final String filePath = "/Users/raghu.kokku/code/AdventOfCode/ElfCalorieCounter.txt"; | ||
|
||
int maxCalories = Integer.MIN_VALUE; | ||
|
||
try { | ||
List<String> allInputLines = Files.readAllLines(Paths.get(filePath)); | ||
List<Integer> listOfItems = new ArrayList<>(); | ||
Set<Integer> maxCaloriesOfEachElf = new HashSet<>(); | ||
|
||
for (String line : allInputLines) { | ||
|
||
if(StringUtils.isBlank(line)) { | ||
maxCaloriesOfEachElf.add(caloriesForEachElf(listOfItems)); | ||
maxCalories = Math.max(maxCalories, caloriesForEachElf(listOfItems)); | ||
listOfItems.clear(); | ||
} else { | ||
listOfItems.add(Integer.valueOf(line)); | ||
} | ||
} | ||
|
||
System.out.printf("Max calories = %d\n", maxCalories); | ||
|
||
System.out.printf("Max calories of first three = %d", sumOfTopThree(maxCaloriesOfEachElf)); | ||
|
||
|
||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static int caloriesForEachElf(List<Integer> calories) { | ||
return calories.stream().mapToInt(c -> c).sum(); | ||
} | ||
|
||
private static int sumOfTopThree(Set<Integer> maxCalories) { | ||
|
||
List<Integer> listOfCalories = new ArrayList<>(maxCalories); | ||
|
||
Collections.sort(listOfCalories, Collections.reverseOrder()); | ||
|
||
return (listOfCalories.get(0) + listOfCalories.get(1) + listOfCalories.get(2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package adventofcode; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class GrovePositioningSystem { | ||
public static void main(String[] args) { | ||
List<String> fileContents = CodeUtility.readFile("/Users/raghu.kokku/code/AdventOfCode/gps.txt"); | ||
List<Integer> fileContentsInInts = fileContents.stream().map(Integer::parseInt).collect(Collectors.toList()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package adventofcode; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MonkeyMath { | ||
|
||
private static List<String> operators = List.of("+", "-", "/", "*"); | ||
private static List<String> fileContents = CodeUtility.readFile("/Users/raghu.kokku/code/AdventOfCode/monkey-yells1.txt"); | ||
private static Map<String, String> dataMap = dataMap(fileContents); | ||
|
||
public static void main(String[] args) { | ||
|
||
int sum =0; | ||
|
||
for (String value : dataMap.values()) { | ||
|
||
//sum = monkeyMath(value, sum); | ||
|
||
} | ||
|
||
System.out.println("Sum = " + sum); | ||
|
||
} | ||
|
||
|
||
|
||
private static Map<String, String> dataMap(List<String> fileContents) { | ||
Map<String, String> dataMap = new HashMap<>(); | ||
|
||
for(String line : fileContents) { | ||
String[] keyValues = line.split(":"); | ||
|
||
String key = keyValues[0].trim(); | ||
String value = keyValues[1].trim(); | ||
|
||
dataMap.put(key, value); | ||
} | ||
|
||
return dataMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package adventofcode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package adventofcode; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class RockPaperScissors { | ||
public static void main(String[] args) { | ||
|
||
List<String> myOptions = List.of("X", "Y", "Z"); | ||
|
||
Map<String, Integer> myMap = constructMap(new HashMap<>(), myOptions); | ||
|
||
List<String> winCombinations = List.of("AY", "BZ", "CX"); | ||
List<String> drawCombinations = List.of("AX", "BY", "CZ"); | ||
List<String> loseCombinations = List.of("AZ", "BX", "CY"); | ||
|
||
int totalScore = 0; | ||
|
||
for (String line : readInputFile()) { | ||
|
||
String opponent = line.split(" ")[0]; | ||
String player = line.split(" ")[1]; | ||
|
||
String combination = StringUtils.join(opponent, player); | ||
|
||
if (winCombinations.contains(combination)) { | ||
totalScore += myMap.get(player) + 6; | ||
} else if (drawCombinations.contains(combination)) { | ||
totalScore += myMap.get(player) + 3; | ||
} else if (loseCombinations.contains(combination)) { | ||
totalScore += myMap.get(player); | ||
} | ||
} | ||
|
||
System.out.printf("Total score = %d\n", totalScore); | ||
System.out.printf("Total score = %d", partTwo(myMap, winCombinations, drawCombinations, loseCombinations)); | ||
|
||
} | ||
|
||
private static Map<String, Integer> constructMap(Map<String, Integer> playerMap, List<String> listOfOptions) { | ||
int i = 1; | ||
for(String c : listOfOptions) { | ||
playerMap.put(c, i++); | ||
} | ||
return playerMap; | ||
} | ||
|
||
private static List<String> readInputFile() { | ||
try { | ||
return Files.readAllLines(Paths.get("/Users/raghu.kokku/code/AdventOfCode/RockPaperScissors.txt")); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static int partTwo(Map<String, Integer> compareMap, List<String> winCombinations, | ||
List<String> drawCombinations, List<String> loseCombinations) { | ||
int score = 0; | ||
|
||
for (String line : readInputFile()) { | ||
String opponent = line.split(" ")[0]; | ||
String player = line.split(" ")[1]; | ||
|
||
if (player.equals("Y")) { | ||
List<String> result = drawCombinations.stream().filter(s -> s.startsWith(opponent)).collect(Collectors.toList()); | ||
char[] chars = result.get(0).toCharArray(); | ||
score += compareMap.get(String.valueOf(chars[1])) + 3; | ||
} else if (player.equals("X")) { | ||
List<String> result = loseCombinations.stream().filter(s -> s.startsWith(opponent)).collect(Collectors.toList()); | ||
char[] chars = result.get(0).toCharArray(); | ||
score += compareMap.get(String.valueOf(chars[1])) + 0; | ||
} else if (player.equals("Z")) { | ||
List<String> result = winCombinations.stream().filter(s -> s.startsWith(opponent)).collect(Collectors.toList()); | ||
char[] chars = result.get(0).toCharArray(); | ||
score += compareMap.get(String.valueOf(chars[1])) + 6; | ||
} | ||
} | ||
|
||
return score; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.