-
Notifications
You must be signed in to change notification settings - Fork 1
solving problem of day1 part2 and day2 part1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String> lines = Files.lines(Path.of("src/main/resources/day1.txt")); | ||
| // Stream<String> lines = Files.lines(Path.of(Day1.class.getClassLoader().getResource("day1.txt").toURI())); | ||
| // lines.forEach(System.out::println); | ||
| Stream<String> lines = Files.lines(Path.of("day1.txt")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this is done? |
||
| //Stream<String> lines = Files.lines(Path.of(Day1.class.getClassLoader().getResource("day1.txt").toURI())); | ||
| //lines.forEach(System.out::println); | ||
|
|
||
| List<Long> 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)); | ||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this part 2? then use a better class name: Day1P2 or Day1Part2 etc. |
||
| public static void main(String args[]) throws IOException{ | ||
| Stream<String> lines = Files.lines(Path.of("day1.txt")); | ||
| //lines.forEach(System.out::println); // to print input. | ||
| int sum =0; | ||
| List<Long> 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<Long>(); | ||
| sum += calories.get(i); | ||
| } | ||
| System.out.println(sum); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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<String> 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); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why have you commented this?