Skip to content

Commit

Permalink
day 3 java
Browse files Browse the repository at this point in the history
  • Loading branch information
pin2t committed Dec 10, 2024
1 parent 0077023 commit 8ba54fe
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions day03.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.*;
import java.util.*;
import java.util.regex.*;

import static java.lang.Integer.parseInt;
import static java.lang.System.in;
import static java.lang.System.out;

public class day03 {
public static void main(String[] args) {
var cmd = Pattern.compile("mul\\((\\d+),(\\d+)\\)|do\\(\\)|don't\\(\\)");
var muls = new int[]{0, 0};
var _do = new boolean[]{true};
new BufferedReader(new InputStreamReader(in)).lines().forEach(line -> {
new Scanner(line).findAll(cmd).forEach(match -> {
if (match.group(0).startsWith("mul")) {
var n1 = parseInt(match.group(1));
var n2 = parseInt(match.group(2));
muls[0] += n1 * n2;
if (_do[0]) { muls[1] += n1 * n2; }
} else {
_do[0] = "do()".equals(match.group(0));
}
});
});
out.printf("%d %d\n", muls[0], muls[1]);
}
}

0 comments on commit 8ba54fe

Please sign in to comment.