-
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
Showing
3 changed files
with
79 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
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,26 @@ | ||
import java.util.*; | ||
|
||
import static java.lang.Integer.parseInt; | ||
import static java.lang.Math.abs; | ||
import static java.lang.System.in; | ||
import static java.lang.System.out; | ||
|
||
public class day01 { | ||
public static void main(String[] args) { | ||
var left = new ArrayList<Integer>(); | ||
var right = new ArrayList<Integer>(); | ||
new Scanner(in).findAll("(\\d+)\\s+(\\d+)").forEach(pair -> { | ||
left.add(parseInt(pair.group(1))); | ||
right.add(parseInt(pair.group(2))); | ||
}); | ||
left.sort(Integer::compareTo); | ||
right.sort(Integer::compareTo); | ||
int total = 0, similarity = 0; | ||
for (int i = 0; i < left.size(); i++) { | ||
int l = left.get(i); | ||
total += abs(l - right.get(i)); | ||
similarity += (int) (l * right.stream().filter(n -> n.equals(l)).count()); | ||
} | ||
out.println(total + " " + similarity); | ||
} | ||
} |
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,49 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
import static java.lang.System.in; | ||
import static java.lang.System.out; | ||
|
||
public class day02 { | ||
public static void main(String[] args) { | ||
var safes = new int[]{0, 0}; | ||
new BufferedReader(new InputStreamReader(in)).lines().forEach(line -> { | ||
var report = new Report(line); | ||
if (report.safe()) { | ||
safes[0]++; | ||
safes[1]++; | ||
} else if (report.canBeSafe()) { | ||
safes[1]++; | ||
} | ||
}); | ||
out.println(safes[0] + " " + safes[1]); | ||
} | ||
|
||
static class Report extends ArrayList<Integer> { | ||
Report(String input) { | ||
super(Arrays.stream(input.split("\\s")).map(Integer::valueOf).toList()); | ||
} | ||
|
||
Report(List<Integer> source, int except) { | ||
super(source); | ||
remove(except); | ||
} | ||
|
||
boolean safe() { | ||
boolean increasing = getFirst().compareTo(getLast()) < 0; | ||
for (int i = 1; i < size(); i++) { | ||
if (increasing && (get(i) - get(i - 1) <= 0 || get(i) - get(i - 1) > 3) || | ||
!increasing && (get(i) - get(i - 1) >= 0 || get(i - 1) - get(i) > 3)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
boolean canBeSafe() { | ||
for (int i = 0; i < size(); i++) | ||
if (new Report(this, i).safe()) return true; | ||
return false; | ||
} | ||
} | ||
} |