Skip to content

Instantly share code, notes, and snippets.

@msx80
Created December 2, 2020 08:16
Show Gist options
  • Save msx80/4e2bd83483fef19c75873e69750fdbcb to your computer and use it in GitHub Desktop.
Save msx80/4e2bd83483fef19c75873e69750fdbcb to your computer and use it in GitHub Desktop.
package aoc;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Day2 {
static class Line {
int min; int max; int ch; String pwd;
public Line(String l) {
String toks[] = l.split("-| |:");
min = Integer.parseInt(toks[0]);
max = Integer.parseInt(toks[1]);
ch = toks[2].charAt(0);
pwd = toks[4];
}
public boolean validate1() {
long count = pwd.chars().filter(i -> i == ch).count();
return count >= min && count <=max;
}
public boolean validate2() {
return (pwd.charAt(min-1) == ch) ^ (pwd.charAt(max-1) == ch);
}
};
public static void main(String[] args) throws IOException {
var records = Files.readAllLines(Paths.get("input2.txt"))
.stream()
.map(Line::new)
.toArray(Line[]::new);
System.out.println(Stream.of(records).filter(s->s.validate1()).count());
System.out.println(Stream.of(records).filter(s->s.validate2()).count());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment