-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d499bf
commit d8935de
Showing
92 changed files
with
2,447 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
VALUE_TYPE = "VALUE" | ||
REFERENCE_TYPE = "REFERENCE" | ||
EMPTY_TYPE = "EMPTY" | ||
|
||
VALUE_PATTERN = /^-?[0-9]+$/ | ||
REFERENCE_PATTERN = /^\$[0-9]+$/ | ||
|
||
def get_arg_type(arg) | ||
return VALUE_TYPE if VALUE_PATTERN.match(arg) | ||
return REFERENCE_TYPE if REFERENCE_PATTERN.match(arg) | ||
|
||
return EMPTY_TYPE | ||
end | ||
|
||
def value(arg_type, arg, formulas, values) | ||
if arg_type == REFERENCE_TYPE | ||
index = arg[1..-1].to_i | ||
parse_formula(index, formulas, values) unless values.key?(index) | ||
return values[index] | ||
end | ||
return arg.to_i | ||
end | ||
|
||
def parse_formula(i, formulas, values) | ||
formula = formulas[i] | ||
operation, arg1, arg2 = formula.split | ||
arg_type1 = get_arg_type(arg1) | ||
arg_type2 = get_arg_type(arg2) | ||
|
||
if operation == "VALUE" | ||
values[i] = value(arg_type1, arg1, formulas, values) | ||
else | ||
value1 = value(arg_type1, arg1, formulas, values) | ||
value2 = value(arg_type2, arg2, formulas, values) | ||
if operation == "ADD" | ||
values[i] = value1 + value2 | ||
elsif operation == "SUB" | ||
values[i] = value1 - value2 | ||
else | ||
values[i] = value1 * value2 | ||
end | ||
end | ||
end | ||
|
||
formulas = [] | ||
values = {} | ||
|
||
# read input formulas | ||
nb_cells = gets.to_i | ||
nb_cells.times do | ||
formulas << gets.chomp | ||
end | ||
|
||
# parse formulas | ||
nb_cells.times do |i| | ||
parse_formula(i, formulas, values) | ||
end | ||
|
||
# output values | ||
nb_cells.times do |i| | ||
puts values[i] | ||
end |
33 changes: 33 additions & 0 deletions
33
community-puzzles/ancestors-and-descendants/ancestors_and_descendants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def print_tree(tree): | ||
output = "" | ||
if not tree: | ||
return | ||
i = 0 | ||
while i < len(tree) - 1: | ||
output += tree[i] + " > " | ||
i += 1 | ||
output += tree[i] | ||
print(output) | ||
|
||
|
||
nb_people = int(input()) | ||
tree = [] # partial family tree | ||
|
||
for _ in range(nb_people): | ||
line = input().strip() | ||
name = line.replace(".", "").rstrip() | ||
name_level = len(line) - len(name) | ||
tree_level = len(tree) | ||
|
||
if name_level == 0: | ||
if tree: | ||
print_tree(tree) | ||
tree.clear() | ||
elif name_level < tree_level: | ||
print_tree(tree) | ||
while name_level < len(tree): | ||
tree.pop() | ||
|
||
tree.append(name) | ||
|
||
print_tree(tree) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
|
||
class Solution | ||
{ | ||
static int[] registers; | ||
static string[] instructions; | ||
static int curLine = 0; | ||
const string names = "abcd"; | ||
|
||
static void Main(string[] args) | ||
{ | ||
const int NB_REGISTERS = 4; | ||
var inStr = Console.ReadLine(); | ||
var inArr = inStr.Split(' '); | ||
registers = new int[NB_REGISTERS]; | ||
for (int i = 0; i < NB_REGISTERS; ++i) | ||
registers[i] = int.Parse(inArr[i]); | ||
|
||
int nbLines = int.Parse(Console.ReadLine()); | ||
instructions = new string[nbLines]; | ||
for (int i = 0; i < nbLines; ++i) | ||
instructions[i] = Console.ReadLine(); | ||
|
||
do | ||
{ | ||
string line = instructions[curLine]; | ||
interpret(line); | ||
Console.Error.WriteLine(curLine + " " + line); | ||
// printRegisters(); | ||
} while (curLine != nbLines); | ||
|
||
string output = ""; | ||
foreach (int register in registers) | ||
output += register.ToString() + " "; | ||
|
||
Console.WriteLine(output.Trim()); | ||
} | ||
|
||
static void interpret(string line) | ||
{ | ||
string[] instruction = line.Split(' '); | ||
switch (instruction[0]) | ||
{ | ||
case "MOV": move(instruction); ++curLine; break; | ||
case "ADD": add(instruction); ++curLine; break; | ||
case "SUB": sub(instruction); ++curLine; break; | ||
case "JNE": jumpNotEqual(instruction); break; | ||
} | ||
} | ||
|
||
static void move(string[] instruction) | ||
{ | ||
int dest = index(instruction[1]); | ||
string value = instruction[2]; | ||
if (names.Contains(value)) | ||
{ | ||
int source = index(value); | ||
registers[dest] = registers[source]; | ||
} | ||
else | ||
{ | ||
registers[dest] = int.Parse(value); | ||
} | ||
} | ||
|
||
static void operation(string[] instruction, int sign) | ||
{ | ||
int result; | ||
|
||
int dest = index(instruction[1]); | ||
string value = instruction[2]; | ||
if (names.Contains(value)) | ||
{ | ||
int source = index(value); | ||
result = registers[source]; | ||
} | ||
else | ||
{ | ||
result = int.Parse(value); | ||
} | ||
|
||
value = instruction[3]; | ||
if (names.Contains(value)) | ||
{ | ||
int source = index(value); | ||
result += sign * registers[source]; | ||
} | ||
else | ||
{ | ||
result += sign * int.Parse(value); | ||
} | ||
|
||
registers[dest] = result; | ||
} | ||
|
||
static void add(string[] instruction) | ||
{ | ||
operation(instruction, 1); | ||
} | ||
|
||
static void sub(string[] instruction) | ||
{ | ||
operation(instruction, -1); | ||
} | ||
|
||
static void jumpNotEqual(string[] instruction) | ||
{ | ||
int operand1 = registers[index(instruction[2])]; | ||
int operand2; | ||
string value2 = instruction[3]; | ||
if (names.Contains(value2)) | ||
operand2 = registers[index(value2)]; | ||
else | ||
operand2 = int.Parse(value2); | ||
|
||
if (operand1 != operand2) | ||
curLine = int.Parse(instruction[1]); | ||
else | ||
++curLine; | ||
} | ||
|
||
static int index(string register) | ||
{ | ||
return char.ToLower(register[0]) - 'a'; | ||
} | ||
|
||
static void printRegisters() | ||
{ | ||
string output = ""; | ||
foreach (int register in registers) | ||
output += register.ToString() + " "; | ||
|
||
Console.Error.WriteLine(output.Trim()); | ||
} | ||
} |
Oops, something went wrong.