From 608fe141f4eed2db826f4eb386f0a9218627f3ee Mon Sep 17 00:00:00 2001 From: pin2t Date: Sat, 30 Jul 2022 21:57:47 +0400 Subject: [PATCH] . --- d10.java | 3 +-- d11.java | 8 +++----- d12.java | 2 +- d14.java | 6 ++++-- d18.java | 6 ++---- d19.java | 2 +- d22.java | 2 +- d23.java | 10 +++++----- d9.java | 3 +-- 9 files changed, 19 insertions(+), 23 deletions(-) diff --git a/d10.java b/d10.java index 9ea6b98..722a4de 100644 --- a/d10.java +++ b/d10.java @@ -1,6 +1,5 @@ import java.io.*; import java.util.*; -import java.util.stream.Collectors; public class d10 { public static void main(String[] args) { @@ -10,7 +9,7 @@ public static void main(String[] args) { var incScore = new HashMap() {{put("(", 1L); put("[", 2L); put("{", 3L); put("<", 4L);}}; var error = 0L; var incompletes = new ArrayList(); - for (String line : reader.lines().collect(Collectors.toList())) { + for (String line : reader.lines().toList()) { var i = 0; var stack = new LinkedList(); while (i < line.length()) { diff --git a/d11.java b/d11.java index 7dd8944..1f331a1 100644 --- a/d11.java +++ b/d11.java @@ -1,6 +1,6 @@ import java.io.BufferedReader; import java.io.InputStreamReader; -import java.util.stream.Collectors; +import java.util.Arrays; import static java.lang.System.out; @@ -33,11 +33,9 @@ static class Board { Board(BufferedReader reader) { this.energy = new int[CAPACITY * CAPACITY]; - for (int i = 0; i < this.energy.length; i++) { - this.energy[i] = -1000; - } + Arrays.fill(this.energy, -1000); int i = 1; - for (String line : reader.lines().collect(Collectors.toList())) { + for (String line : reader.lines().toList()) { for (int j = 1; j <= line.length(); j++) { this.energy[i * CAPACITY + j] = line.charAt(j - 1) - '0'; } diff --git a/d12.java b/d12.java index 60bfb5b..2642dc8 100644 --- a/d12.java +++ b/d12.java @@ -63,7 +63,7 @@ static class Edge { final String from, to; Edge(String s) { - var pair = s.split("\\-"); + var pair = s.split("-"); this.from = pair[0]; this.to = pair[1]; } diff --git a/d14.java b/d14.java index c4092db..8aa73e2 100644 --- a/d14.java +++ b/d14.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; +import java.util.function.Supplier; import static java.lang.System.out; @@ -22,6 +23,7 @@ public static void main(String[] args) throws IOException { line = reader.readLine(); rules.put(line.substring(0, 2), line.charAt(6)); } + Supplier difference = () -> counts.values().stream().max(Long::compareTo).get() - counts.values().stream().min(Long::compareTo).get(); for (int step = 1; step <= 40; step++) { var newpairs = new HashMap(); for (var pair : pairs.entrySet()) { @@ -32,10 +34,10 @@ public static void main(String[] args) throws IOException { } } if (step == 10) { - out.println(counts.values().stream().max(Long::compareTo).get() - counts.values().stream().min(Long::compareTo).get()); + out.println(difference.get()); } pairs = newpairs; } - out.println(counts.values().stream().max(Long::compareTo).get() - counts.values().stream().min(Long::compareTo).get()); + out.println(difference.get()); } } diff --git a/d18.java b/d18.java index 41fbe23..850670e 100644 --- a/d18.java +++ b/d18.java @@ -3,7 +3,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; import static java.lang.System.out; @@ -12,7 +11,7 @@ public class d18 { public static void main(String[] args) { var reader = new BufferedReader(new InputStreamReader(System.in)); - var numbers = reader.lines().collect(Collectors.toList()); + var numbers = reader.lines().toList(); var result = numbers.get(0); for (var i = 1; i < numbers.size(); i++) { result = sum(result, numbers.get(i)); @@ -75,8 +74,7 @@ static String explode(String n, int pos, Matcher matcher) { static String split(String n, int pos) { var regular = peekRegular(n, pos); - var result = n.substring(0, pos) + "[" + regular / 2 + "," + (regular / 2 + regular % 2) + "]" + n.substring(pos + 2); - return result; + return n.substring(0, pos) + "[" + regular / 2 + "," + (regular / 2 + regular % 2) + "]" + n.substring(pos + 2); } static boolean isdigit(String s, int pos) { return s.charAt(pos) >= '0' && s.charAt(pos) <= '9'; } diff --git a/d19.java b/d19.java index 602789e..cbfa432 100644 --- a/d19.java +++ b/d19.java @@ -64,7 +64,7 @@ static Optional matchAll(Set unique, BeaconsScanner scanner) static Optional match(Set beacons, BeaconsScanner scanner) { for (Position beacon : beacons) { for (Position p : scanner.beacons) { - var mapped = scanner.beacons.stream().map(b -> b.move(beacon.x - p.x, beacon.y - p.y, beacon.z - p.z)).collect(Collectors.toList()); + var mapped = scanner.beacons.stream().map(b -> b.move(beacon.x - p.x, beacon.y - p.y, beacon.z - p.z)).toList(); if (mapped.stream().filter(beacons::contains).count() >= 12) { beacons.addAll(mapped); return Optional.of(new Position(beacon.x - p.x, beacon.y - p.y, beacon.z - p.z)); diff --git a/d22.java b/d22.java index 698898d..f6c1308 100644 --- a/d22.java +++ b/d22.java @@ -65,7 +65,7 @@ static class Cuboid { Cuboid(String s) { var m = commandPtn.matcher(s); - m.matches(); + if (!m.matches()) throw new RuntimeException("invalid cuboid format " + s); this.x1 = Integer.parseInt(m.group(1)); this.x2 = Integer.parseInt(m.group(2)); this.y1 = Integer.parseInt(m.group(3)); diff --git a/d23.java b/d23.java index 37354a9..30c3816 100644 --- a/d23.java +++ b/d23.java @@ -99,14 +99,14 @@ List moves() { for (Amphipod a : amphipods) { // if amphipod in the hallway and can step into its destination - step in if (a.pos() != 2 && a.pos() != 4 && a.pos() != 6 && a.pos() != 8) { - if (!amphipods.stream().anyMatch(aa -> { + if (amphipods.stream().noneMatch(aa -> { if (a.destination() > a.pos()) { return aa.depth() == 0 && aa.pos() > a.pos() && aa.pos() <= a.destination(); } else { return aa.depth() == 0 && aa.pos() >= a.destination() && aa.pos() < a.pos(); } }) && - !amphipods.stream().anyMatch(aa -> aa.pos() == a.destination() && ((aa.type() - 'A') * 2 + 2) != a.destination())) { + amphipods.stream().noneMatch(aa -> aa.pos() == a.destination() && ((aa.type() - 'A') * 2 + 2) != a.destination())) { result.add(this.move(a, a.destination())); } } else if (a.pos() != a.destination() || @@ -116,14 +116,14 @@ List moves() { // conditions: // 1. no other amphipods between amphipod.pos and the dest in hallway // 2. no other amphipods in the amphipod's room with lower depth - if (!amphipods.stream().anyMatch(aa -> { + if (amphipods.stream().noneMatch(aa -> { if (dest > a.pos()) { return aa.pos() > a.pos() && aa.pos() <= dest && aa.depth() == 0; } else { return aa.pos() >= dest && aa.depth() == 0 && aa.pos() < a.pos(); } }) && - !amphipods.stream().anyMatch(aa -> aa.pos() == a.pos() && aa.depth() < a.depth())) { + amphipods.stream().noneMatch(aa -> aa.pos() == a.pos() && aa.depth() < a.depth())) { result.add(this.move(a, dest)); } } @@ -133,7 +133,7 @@ List moves() { } boolean isFinal() { - return !amphipods.stream().anyMatch(it -> it.pos() != ((it.type() - 'A') * 2 + 2)); + return amphipods.stream().noneMatch(it -> it.pos() != ((it.type() - 'A') * 2 + 2)); } public boolean equals(Object other) { diff --git a/d9.java b/d9.java index 179d5a5..41d430a 100644 --- a/d9.java +++ b/d9.java @@ -1,7 +1,6 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; -import java.util.stream.Collectors; import static java.lang.System.out; @@ -10,7 +9,7 @@ public static void main(String[] args) { var reader = new BufferedReader(new InputStreamReader(System.in)); var field = new Field(reader); out.println(field.risk()); - var sizes = field.basins().stream().sorted().collect(Collectors.toList()); + var sizes = field.basins().stream().sorted().toList(); out.println(sizes.get(sizes.size() - 1) * sizes.get(sizes.size() - 2) * sizes.get(sizes.size() - 3)); }