private c_snailfish_number( c_snailfish_number p, string input) { parent = p; if (input[0] == '[') { type = e_snailfish_number_type.pair; int left_start_index = 1; int left_end_index; left = parse_pair_element(input, left_start_index, out left_end_index); int right_start_index = left_end_index + 2; int right_end_index; right = parse_pair_element(input, right_start_index, out right_end_index); } else { type = e_snailfish_number_type.raw_number; value = UInt64.Parse(input); } }
private c_snailfish_number( UInt64 v, c_snailfish_number p) { type = e_snailfish_number_type.raw_number; parent = p; value = v; }
private c_snailfish_number( c_snailfish_number l, c_snailfish_number r, c_snailfish_number p) { type = e_snailfish_number_type.pair; parent = p; left = l.clone(); left.parent = this; right = r.clone(); right.parent = this; }
private bool try_explode( bool pretty, int num_parents) { bool result = false; if (type == e_snailfish_number_type.pair) { result = left.try_explode(pretty, num_parents + 1); if (!result && num_parents >= 4) { if (pretty) { Console.WriteLine("exploding:"); display(pretty); Console.WriteLine(); } explode_left_value(pretty); explode_right_value(pretty); type = e_snailfish_number_type.raw_number; left = null; right = null; value = 0; if (pretty) { Console.WriteLine("into:"); display(pretty); Console.WriteLine(); } result = true; } if (!result) { result = right.try_explode(pretty, num_parents + 1); } } return(result); }
public bool try_split( bool pretty) { bool result = false; if (type == e_snailfish_number_type.pair) { result = left.try_split(pretty); if (!result) { result = right.try_split(pretty); } } else if (type == e_snailfish_number_type.raw_number && value >= 10) { if (pretty) { Console.WriteLine("splitting:"); display(pretty); Console.WriteLine(); } type = e_snailfish_number_type.pair; left = new c_snailfish_number(value / 2, this); right = new c_snailfish_number(value / 2 + value % 2, this); value = 0; if (pretty) { Console.WriteLine("into:"); display(pretty); Console.WriteLine(); } result = true; } return(result); }