Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 460ef57

Browse files
authored
Merge pull request #9 from ProgSoc/unclosed-brackets-fix
Allow for possibility of no unclosed braces in unclosed-brackets
2 parents 670d4b2 + e69e528 commit 460ef57

File tree

2 files changed

+74
-29
lines changed

2 files changed

+74
-29
lines changed

competition/unclosed-brackets/prob.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,38 @@ difficulty = 1
1313

1414
# 📄 Unclosed Braces
1515

16-
Given a snippet of C with unclosed curly braces (`{}`), determine the index of the first unclosed opening curly brace.
16+
You've been given a piece of spaghetti code written in C,
17+
which was probably a failed attempt at some code golfing.
18+
However, your manager isn't sure if this code is usable at all.
19+
They suspect that there are unclosed braces in this program,
20+
and they refuse to use a proper IDE to check.
1721

18-
## Output Format
19-
Your output should be the index (with the first character being index 0) of the first unclosed opening curly brace.
22+
Your task is to take this code snippet and determine the index
23+
of the *first* unclosed opening curly brace.
24+
If there are no unclosed curly braces, then you can just tell your manager there are none.
25+
26+
Note that newline characters (`\n`), carriage returns (`\r`) and other invisible ASCII characters
27+
are counted as a character.
28+
29+
## Input
30+
31+
Your input for this question is a code snippet which may or may not have an unclosed curly brace.
32+
33+
## Output
34+
35+
Your output for this question is either:
36+
* the index of the first unclosed opening curly brace, counting such that the very first character is at index 0, or
37+
* the word `none`, if there are no such unclosed curly braces.
38+
39+
## Example
40+
41+
Consider the following code snippet.
42+
43+
```c
44+
#include <stdio.h>
45+
void f(){{printf("%d",1);}
46+
int main(){f();}
47+
```
48+
49+
The first unclosed curly brace is on the second line, just before the `printf` call.
50+
The answer for this should be `27` (there is only a single `\n` for each newline).

competition/unclosed-brackets/src/main.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ use rand_chacha::ChaChaRng;
1010

1111
fn expression(rng: &mut impl Rng, depth: usize) -> String {
1212
if depth > 20 {
13-
return format!("{}", rng.gen_range(1..=100));
13+
return format!("{}", rng.random_range(1..=100));
1414
}
1515

16-
match rng.gen_range(0..6) {
16+
match rng.random_range(0..6) {
1717
1 => {
1818
format!(
1919
"{}{}{}",
2020
expression(rng, depth + 1),
21-
match rng.gen_range(0..4) {
21+
match rng.random_range(0..4) {
2222
0 => '+',
2323
1 => '-',
2424
2 => '*',
@@ -31,14 +31,14 @@ fn expression(rng: &mut impl Rng, depth: usize) -> String {
3131
format!("{{{}}}", expression(rng, depth + 1))
3232
}
3333
3 => {
34-
let function = match rng.gen_range(0..3) {
34+
let function = match rng.random_range(0..3) {
3535
0 => "sin",
3636
1 => "cos",
3737
_ => "tan",
3838
};
3939
format!("{}({})", function, expression(rng, depth + 1))
4040
}
41-
_ => format!("{}", rng.gen_range(1..=100)),
41+
_ => format!("{}", rng.random_range(1..=100)),
4242
}
4343
}
4444

@@ -47,11 +47,11 @@ fn statement(rng: &mut impl Rng, depth: usize) -> String {
4747
return format!("return {}; ", expression(rng, depth + 1));
4848
}
4949

50-
if rng.gen_bool(0.01) && depth > 5 {
50+
if rng.random_bool(0.01) && depth > 5 {
5151
return format!("{{{}", statement(rng, depth + 1));
5252
}
5353

54-
match rng.gen_range(0..10) {
54+
match rng.random_range(0..10) {
5555
0 => format!("int x={};", expression(rng, depth + 1)),
5656
1 => format!("printf(\"%d\",{});", expression(rng, depth + 1)),
5757
2 => format!(
@@ -67,7 +67,7 @@ fn statement(rng: &mut impl Rng, depth: usize) -> String {
6767
),
6868
4 => format!(
6969
"for(int i=0;i<{};i++){{{}}}",
70-
rng.gen_range(1..=10),
70+
rng.random_range(1..=10),
7171
statement(rng, depth + 1)
7272
),
7373
5 => format!(
@@ -80,24 +80,24 @@ fn statement(rng: &mut impl Rng, depth: usize) -> String {
8080
}
8181

8282
fn function(rng: &mut impl Rng, depth: usize) -> String {
83-
let body = (0..rng.gen_range(10..50))
83+
let body = (0..rng.random_range(10..50))
8484
.map(|_| statement(rng, depth + 1))
8585
.collect::<Vec<_>>()
8686
.join("");
8787

88-
format!("void func_{}(){{{}}}", rng.gen_range(1..1000), body)
88+
format!("void func_{}(){{{}}}", rng.random_range(1..1000), body)
8989
}
9090

9191
fn program(rng: &mut impl Rng) -> String {
92-
let functions = (0..rng.gen_range(100..200))
92+
let functions = (0..rng.random_range(100..200))
9393
.map(|_| function(rng, 0))
9494
.collect::<Vec<_>>()
9595
.join("");
9696

9797
format!(
9898
"#include <stdio.h>\n{}\nint main(){{func_{}();return 0;}}",
9999
functions,
100-
rng.gen_range(1..=1000)
100+
rng.random_range(1..=1000)
101101
)
102102
}
103103

@@ -119,12 +119,7 @@ fn run(seed: u64, method: &str) {
119119
let mut rng = ChaChaRng::seed_from_u64(seed);
120120

121121
let src = program(&mut rng);
122-
123-
let Some(solution) = solve(&src) else {
124-
// Try again
125-
run(seed.wrapping_add(1), method);
126-
return;
127-
};
122+
let solution = solve(&src);
128123

129124
match method {
130125
"generate" => {
@@ -134,18 +129,37 @@ fn run(seed: u64, method: &str) {
134129
// Reads from user input.
135130
let mut buffer = String::new();
136131
let _ = stdin().read_line(&mut buffer);
137-
let Ok(input) = buffer.trim().parse::<usize>() else {
138-
eprintln!("Invalid input, expected a positive integer.");
139-
exit(1);
140-
};
141-
142-
if input != solution {
143-
eprintln!("Incorrect solution");
132+
let provided_answer = buffer.trim();
133+
134+
if provided_answer == "none" {
135+
match solution {
136+
None => {
137+
exit(0);
138+
}
139+
Some(_) => {
140+
eprintln!("Incorrect answer. There is an unclosed bracket.");
141+
exit(1);
142+
}
143+
}
144+
} else if let Some(solution_index) = solution {
145+
let Ok(input) = provided_answer.parse::<usize>() else {
146+
eprintln!("Invalid input, Expected either 'none' or a positive integer.");
147+
exit(1);
148+
};
149+
150+
if input == solution_index {
151+
exit(0);
152+
} else {
153+
eprintln!("Incorrect solution");
154+
exit(1);
155+
}
156+
} else {
157+
eprintln!("Invalid input. Expected either 'none' or a positive integer.");
144158
exit(1);
145159
}
146160
}
147161
"solution" => {
148-
println!("{}", solution);
162+
println!("{:?}", solution);
149163
}
150164
_ => panic!(),
151165
}

0 commit comments

Comments
 (0)