diff --git a/src/main/java/com/aoc/days/Day1.java b/src/main/java/com/aoc/days/Day1.java index acfd57f..89a6e88 100644 --- a/src/main/java/com/aoc/days/Day1.java +++ b/src/main/java/com/aoc/days/Day1.java @@ -1,4 +1,4 @@ -package com.aoc.days; +//package com.aoc.days; import java.io.IOException; //import java.net.URISyntaxException; @@ -12,9 +12,9 @@ public class Day1 { public static void main(String[] args) throws IOException { //}, URISyntaxException { - Stream lines = Files.lines(Path.of("src/main/resources/day1.txt")); -// Stream lines = Files.lines(Path.of(Day1.class.getClassLoader().getResource("day1.txt").toURI())); -// lines.forEach(System.out::println); + Stream lines = Files.lines(Path.of("day1.txt")); + //Stream lines = Files.lines(Path.of(Day1.class.getClassLoader().getResource("day1.txt").toURI())); + //lines.forEach(System.out::println); List calories = new ArrayList<>(); AtomicLong tempCalorie = new AtomicLong(); @@ -32,4 +32,4 @@ public static void main(String[] args) throws IOException { //}, URISyntaxExcept System.out.println(calories.stream().max(Long::compare)); } -} +} \ No newline at end of file diff --git a/src/main/java/com/aoc/days/Day1a.java b/src/main/java/com/aoc/days/Day1a.java new file mode 100644 index 0000000..5fcd48f --- /dev/null +++ b/src/main/java/com/aoc/days/Day1a.java @@ -0,0 +1,39 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Stream; + +public class Day1a { + public static void main(String args[]) throws IOException{ + Stream lines = Files.lines(Path.of("day1.txt")); + //lines.forEach(System.out::println); // to print input. + int sum =0; + List calories = new ArrayList<>(); // so that not need to define size in list + AtomicLong tempCalories = new AtomicLong(); + tempCalories.set(0); + lines.forEach(line -> { + if(line.isBlank()) { + calories.add(tempCalories.get()); + tempCalories.set(0); + } + else { + int n = Integer.parseInt(line); + tempCalories.set(n+tempCalories.get()); + } + }); + System.out.println(calories.stream().max(Long::compare)); + Collections.sort(calories); + System.out.println(calories); + System.out.println(calories.size()); + for(int i=calories.size();i<238;i--) { + //calories = new List(); + sum += calories.get(i); + } + System.out.println(sum); + + } +} diff --git a/src/main/java/com/aoc/days/Day2.java b/src/main/java/com/aoc/days/Day2.java new file mode 100644 index 0000000..4299738 --- /dev/null +++ b/src/main/java/com/aoc/days/Day2.java @@ -0,0 +1,99 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Stream; + +// rock paper siccisor + // a = x = rock = 1 + //b = y = papr = 2 win = 6 and loosse = 0 + //c = z = scissior = 3 draw =3 + + public class Day2 { + static int score1=0; static int score2 = 0; + + static boolean win(char c,char d) { + if(c==('A')) { + return (d==('Z')); + } + else if (c==('B')) { + return d==('X'); + } + else { + return d==('Y'); + } + } + + static boolean draw(char c, char d) { + if(c==('A')) { + return (d==('X')); + } + else if (c==('B')) { + return d==('Y'); + } + else { + return d==('Z'); + } + } + + + static public void main(String args[]) throws IOException { + Stream lines = Files.lines(Path.of("day2.txt")); + + lines.forEach(line -> { + if (line.length() == 3) { + char p1 = line.charAt(0); + char p2 = line.charAt(2); + if(win(p1,p2)) { + //System.out.println("Player 1 win"); + if(p1=='A' && p2=='Z') { + score1+=7; + score2+=3; + } + else if(p1=='B' && p2=='X') { + score1+=8; + score2+=1; + } + else { + score1+=9; + score2+=2; + } + + } + else if(draw(p1, p2)) { + //System.out.println("Draw"); + score1+=3; + score2+=3; + + } + else { + //System.out.println("player 2 win"); + if(p1=='C' && p2=='X'){ + score1+=3; + score2+=7; + } + else if(p1=='A' && p2=='Y'){ + score1+=1; + score2+=8; + } + else { + score1+=2; + score2+=9; + } + } + + + } else { + System.out.println("Not a valid input!"); + } + }); + System.out.println(score1+" "+score2); + } + } + + + + +