Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
pin2t committed Jul 30, 2022
1 parent 94af16e commit 608fe14
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 23 deletions.
3 changes: 1 addition & 2 deletions d10.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class d10 {
public static void main(String[] args) {
Expand All @@ -10,7 +9,7 @@ public static void main(String[] args) {
var incScore = new HashMap<String, Long>() {{put("(", 1L); put("[", 2L); put("{", 3L); put("<", 4L);}};
var error = 0L;
var incompletes = new ArrayList<Long>();
for (String line : reader.lines().collect(Collectors.toList())) {
for (String line : reader.lines().toList()) {
var i = 0;
var stack = new LinkedList<String>();
while (i < line.length()) {
Expand Down
8 changes: 3 additions & 5 deletions d11.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion d12.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
6 changes: 4 additions & 2 deletions d14.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Long> 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<String, Long>();
for (var pair : pairs.entrySet()) {
Expand All @@ -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());
}
}
6 changes: 2 additions & 4 deletions d18.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand Down Expand Up @@ -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'; }
Expand Down
2 changes: 1 addition & 1 deletion d19.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static Optional<Position> matchAll(Set<Position> unique, BeaconsScanner scanner)
static Optional<Position> match(Set<Position> 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));
Expand Down
2 changes: 1 addition & 1 deletion d22.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions d23.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ List<State> 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() ||
Expand All @@ -116,14 +116,14 @@ List<State> 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));
}
}
Expand All @@ -133,7 +133,7 @@ List<State> 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) {
Expand Down
3 changes: 1 addition & 2 deletions d9.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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));
}

Expand Down

0 comments on commit 608fe14

Please sign in to comment.