add Solution example
This commit is contained in:
parent
f1976baf57
commit
0c41658958
10
c/execute.c
10
c/execute.c
@ -682,8 +682,8 @@ void op_getfield(struct vm * vm, uint32_t index)
|
||||
&class_entry,
|
||||
&field_entry,
|
||||
&field_descriptor_constant);
|
||||
// could be an array
|
||||
assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
|
||||
//assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
|
||||
uint32_t field_index = field_entry->field_info - class_entry->class_file->fields;
|
||||
printf("putfield field_index %d\n", field_index);
|
||||
@ -736,7 +736,7 @@ void op_getstatic(struct vm * vm, uint32_t index)
|
||||
if (!vm_initialize_class(vm, class_entry))
|
||||
return;
|
||||
|
||||
assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
//assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
|
||||
switch (field_descriptor_constant->utf8.bytes[0]) {
|
||||
case 'B': [[fallthrough]];
|
||||
@ -1722,7 +1722,7 @@ void op_putfield(struct vm * vm, uint32_t index)
|
||||
type or an array type, then the value must be a value of the field descriptor
|
||||
type. */
|
||||
|
||||
assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
//assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
|
||||
uint32_t field_index = field_entry->field_info - class_entry->class_file->fields;
|
||||
printf("putfield field_index %d\n", field_index);
|
||||
@ -1784,7 +1784,7 @@ void op_putstatic(struct vm * vm, uint32_t index)
|
||||
if (!vm_initialize_class(vm, class_entry))
|
||||
return;
|
||||
|
||||
assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
//assert(field_descriptor_constant->utf8.length == 1 || field_descriptor_constant->utf8.length == 2);
|
||||
|
||||
switch (field_descriptor_constant->utf8.bytes[0]) {
|
||||
case 'B': [[fallthrough]];
|
||||
|
218
p/Solution.java
Normal file
218
p/Solution.java
Normal file
@ -0,0 +1,218 @@
|
||||
package p;
|
||||
|
||||
class Rule {
|
||||
public final int left;
|
||||
public final int right;
|
||||
|
||||
public Rule(int left, int right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
};
|
||||
|
||||
class Array {
|
||||
public int[] num;
|
||||
public int length;
|
||||
|
||||
public Array(int size) {
|
||||
this.num = new int[size];
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
public void append(int n) {
|
||||
num[length] = n;
|
||||
length += 1;
|
||||
}
|
||||
};
|
||||
|
||||
class InputParser {
|
||||
byte[] buf;
|
||||
int ix;
|
||||
|
||||
public InputParser(byte[] buf) {
|
||||
this.buf = buf;
|
||||
this.ix = 0;
|
||||
}
|
||||
|
||||
static int parse_base10_digit(byte c)
|
||||
{
|
||||
switch (c) {
|
||||
case '0': return 0;
|
||||
case '1': return 1;
|
||||
case '2': return 2;
|
||||
case '3': return 3;
|
||||
case '4': return 4;
|
||||
case '5': return 5;
|
||||
case '6': return 6;
|
||||
case '7': return 7;
|
||||
case '8': return 8;
|
||||
case '9': return 9;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int parse_base10() {
|
||||
int n = 0;
|
||||
|
||||
while (true) {
|
||||
int digit = parse_base10_digit(buf[ix]);
|
||||
if (digit == -1)
|
||||
break;
|
||||
|
||||
n *= 10;
|
||||
n += digit;
|
||||
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void parse_consume(char c) {
|
||||
if (buf[ix] != (byte)c) {
|
||||
// error
|
||||
while (true);
|
||||
}
|
||||
ix += 1;
|
||||
}
|
||||
|
||||
public void parse_skip(char c) {
|
||||
while (ix < buf.length && buf[ix] == (byte)c) {
|
||||
ix += 1;
|
||||
}
|
||||
}
|
||||
|
||||
boolean parse_match(char c) {
|
||||
if (buf[ix] == (byte)c) {
|
||||
parse_skip(c);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean end_of_rules() {
|
||||
return buf[ix] == '\n';
|
||||
}
|
||||
|
||||
public Rule parse_rule() {
|
||||
int left = parse_base10();
|
||||
parse_consume('|');
|
||||
int right = parse_base10();
|
||||
parse_consume('\n');
|
||||
|
||||
return new Rule(left, right);
|
||||
}
|
||||
|
||||
public boolean end_of_updates() {
|
||||
return ix >= buf.length;
|
||||
}
|
||||
|
||||
public Array parse_update() {
|
||||
Array update = new Array(10);
|
||||
|
||||
while (true) {
|
||||
int n = parse_base10();
|
||||
parse_skip(',');
|
||||
|
||||
update.append(n);
|
||||
|
||||
if (parse_match('\n'))
|
||||
return update;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
static String input = """
|
||||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
||||
""";
|
||||
|
||||
static boolean rule_match(Rule rule, int a, int b)
|
||||
{
|
||||
return rule.left == a && rule.right == b;
|
||||
}
|
||||
|
||||
|
||||
static boolean sort_numbers(Rule[] rules, int rules_length, Array update) {
|
||||
boolean at_least_one_swap = false;
|
||||
while (true) {
|
||||
boolean swapped = false;
|
||||
for (int i = 0; i < update.length - 1; i++) {
|
||||
int a = update.num[i + 0];
|
||||
int b = update.num[i + 1];
|
||||
|
||||
for (int j = 0; j < rules_length; j++) {
|
||||
if (rule_match(rules[j], b, a)) {
|
||||
swapped = true;
|
||||
at_least_one_swap = true;
|
||||
update.num[i + 0] = b;
|
||||
update.num[i + 1] = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!swapped)
|
||||
break;
|
||||
}
|
||||
|
||||
return at_least_one_swap;
|
||||
}
|
||||
|
||||
static int solve(boolean correctly_ordered) {
|
||||
InputParser parser = new InputParser(input.getBytes());
|
||||
|
||||
Rule rules[] = new Rule[100];
|
||||
int rules_length = 0;
|
||||
while (!(parser.end_of_rules())) {
|
||||
Rule rule = parser.parse_rule();
|
||||
rules[rules_length] = rule;
|
||||
rules_length += 1;
|
||||
}
|
||||
|
||||
parser.parse_skip('\n');
|
||||
|
||||
int sum = 0;
|
||||
while (!(parser.end_of_updates())) {
|
||||
Array update = parser.parse_update();
|
||||
|
||||
boolean at_least_one_swap = sort_numbers(rules, rules_length, update);
|
||||
if (correctly_ordered ^ at_least_one_swap) {
|
||||
sum += update.num[update.length / 2];
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
solve(true); // part1
|
||||
solve(false); // part2
|
||||
}
|
||||
}
|
@ -1,6 +1,12 @@
|
||||
package p;
|
||||
|
||||
class TestString {
|
||||
static final String s = """
|
||||
text
|
||||
text
|
||||
text
|
||||
""";
|
||||
|
||||
static int test() {
|
||||
String s = new String("asdf");
|
||||
byte[] b = s.getBytes();
|
||||
@ -11,7 +17,6 @@ class TestString {
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
public static void main() {
|
||||
test();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user