8000 vine: overhaul control flow by tjjfvi · Pull Request #314 · VineLang/vine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

vine: overhaul control flow #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
25 changes: 15 additions & 10 deletions tests/programs/aoc_2024/day_02.vi
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ pub fn main(&io: &IO) {

for line in lines.into_iter() {
let report = line.split(" ").map(fn* (x) { N32::parse(x).unwrap() });
if is_safe(report) {
safe_count += 1;
dampened_safe_count += 1;
} else if problem_dampener(report) {
dampened_safe_count += 1;
when {
is_safe(report) {
safe_count += 1;
dampened_safe_count += 1;
}
problem_dampener(report) {
dampened_safe_count += 1;
}
}
}

F438 Expand All @@ -32,26 +35,28 @@ fn is_safe(report: List[N32]) -> Bool {
level - last
};
if !(1 <= diff <= 3) {
return false;
break false;
}
if order is Some(order) && order != (last > level) {
return false;
break false;
}
order = Some(last > level);
}
last = Some(level);
} else {
true
}
true
}

fn problem_dampener(report: List[N32]) -> Bool {
let prefix = [];
let suffix = report;
while suffix.pop_front() is Some(level) {
if is_safe(prefix ++ suffix) {
return true;
break true;
}
prefix ++= [level];
} else {
false
}
false
}
50 changes: 27 additions & 23 deletions tests/programs/aoc_2024/day_04.vi
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,33 @@ pub fn main(&io: &IO) {
diamond(
lines,
fn* (char: Char, Neighbors[Char](nw, n, ne, w, e, sw, s, se)) {
if char == 'X' {
nw.send('X');
n.send('X');
ne.send('X');
w.send('X');
e.send('X');
sw.send('X');
s.send('X');
se.send('X');
} else if char == 'M' || char == 'A' {
let recv = if char == 'M' {
'X'
} else {
'M'
};
relay(nw, se, recv, char);
relay(n, s, recv, char);
relay(ne, sw, recv, char);
relay(w, e, recv, char);
} else {
matches += check(nw, 'A') + check(ne, 'A') + check(sw, 'A') + check(se, 'A');
let x = check(n, 'A') + check(s, 'A') + check(w, 'A') + check(e, 'A');
matches += x;
when {
char == 'X' {
nw.send('X');
n.send('X');
ne.send('X');
w.send('X');
e.send('X');
sw.send('X');
s.send('X');
se.send('X');
}
char == 'M' || char == 'A' {
let recv = if char == 'M' {
'X'
} else {
'M'
};
relay(nw, se, recv, char);
relay(n, s, recv, char);
relay(ne, sw, recv, char);
relay(w, e, recv, char);
}
_ {
matches += check(nw, 'A') + check(ne, 'A') + check(sw, 'A') + check(se, 'A');
let x = check(n, 'A') + check(s, 'A') + check(w, 'A') + check(e, 'A');
matches += x;
}
}
},
'.',
Expand Down
10 changes: 6 additions & 4 deletions tests/programs/aoc_2024/day_06.vi
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ fn walk(&grid: &Array[Array[Char]], pos: (N32, N32)) -> (Bool, N32) {
let distinct = 1;
let repeat = 0;

while repeat < 3 * distinct {
let looped = while repeat < 3 * distinct {
let new_pos = pos + dir;
let (x, y) = new_pos;
if x >= width || y >= height {
return (false, distinct);
break false;
}
let &cell = (*grid.at(y).unwrap()).at(x).unwrap();
if cell == '#' {
Expand All @@ -91,7 +91,9 @@ fn walk(&grid: &Array[Array[Char]], pos: (N32, N32)) -> (Bool, N32) {
}
cell = 'X';
pos = new_pos;
}
} else {
true
};

(true, distinct)
(looped, distinct)
}
6 changes: 3 additions & 3 deletions tests/programs/aoc_2024/day_07.vi
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ pub fn main(&io: &IO) {
results = add ++ mul;
results2 = add2 ++ mul2 ++ concat;
}
do.search {
do {
for num in results.into_iter() {
if num == goal {
part1 += goal;
part2 += goal;
break.search;
break.do;
}
}
for num in results2.into_iter() {
if num == goal {
part2 += goal;
break.search;
break.do;
}
}
}
Expand Down
29 changes: 19 additions & 10 deletions tests/programs/aoc_2024/day_14.vi
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ pub fn main(&io: &IO) {
for (px, py, vx, vy) in robots.into_iter() {
let x = (px + vx * 100) % width;
let y = (py + vy * 100) % height;
if x < width / 2 {
if y < height / 2 {
a += 1;
} else if y > height / 2 {
b += 1;
when {
x < width / 2 {
when {
y < height / 2 {
a += 1;
}
y > height / 2 {
b += 1;
}
}
}
} else if x > width / 2 {
if y < height / 2 {
c += 1;
} else if y > height / 2 {
d += 1;
x > width / 2 {
when {
y < height / 2 {
c += 1;
}
y > height / 2 {
d += 1;
}
}
}
}
}
Expand Down
59 changes: 30 additions & 29 deletions tests/programs/aoc_2024/day_15.vi
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ pub fn main(&io: &IO) {
while io.read_line() is Some(line) {
for char in line!.into_iter() {
instructions ++= [char];
let dir = if char == '<' {
(-(1), 0)
} else if char == '>' {
(1, 0)
} else if char == '^' {
(0, -(1))
} else {
(0, 1)
let dir = when {
char == '<' { (-(1), 0) }
char == '>' { (1, 0) }
char == '^' { (0, -(1)) }
_ { (0, 1) }
};
let target_pos = robot + dir;
while *get(target_pos) == 'O' {
Expand Down Expand Up @@ -117,6 +114,7 @@ pub fn main(&io: &IO) {
char = carry;
}
carry = old;
continue;
}
} else {
let dir_y = if char == '^' {
Expand All @@ -136,29 +134,32 @@ pub fn main(&io: &IO) {
let new_front = [];
while front.pop_front() is Some(pos_x, carry) {
let &char = get((pos_x, pos_y));
if char == '#' {
~success = false;
new_front = [];
break;
}
if char == '[' {
new_front.push_back((pos_x, '['));
new_front.push_back((pos_x + 1, ']'));
let carry = '.';
if front.len() != 0 && front.at(0) is Some(&(p, c)) && p == pos_x + 1 {
front.pop_front();
carry = c;
when {
char == '#' {
~success = false;
new_front = [];
break;
}
let &char = get((pos_x + 1, pos_y));
if ~success {
char = carry;
char == '[' {
new_front.push_back((pos_x, '['));
new_front.push_back((pos_x + 1, ']'));
let carry = '.';
if front.len() != 0 && front.at(0) is Some(&(p, c)) && p == pos_x + 1 {
front.pop_front();
carry = c;
}
let &char = get((pos_x + 1, pos_y));
if ~success {
char = carry;
}
}
} else if char == ']' {
new_front.push_back((pos_x - 1, '['));
new_front.push_back((pos_x, ']'));
let &char = get((pos_x - 1, pos_y));
if ~success {
char = '.';
char == ']' {
new_front.push_back((pos_x - 1, '['));
new_front.push_back((pos_x, ']'));
let &char = get((pos_x - 1, pos_y));
if ~success {
char = '.';
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions tests/programs/aoc_2024/day_16.vi
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ pub fn main(&io: &IO) {
while io.read_line() is Some(line) {
let row = [];
for char in line!.into_iter() {
if char == 'S' {
start = (row.len(), grid.len());
} else if char == 'E' {
end = (row.len(), grid.len());
when {
char == 'S' {
start = (row.len(), grid.len());
}
char == 'E' {
end = (row.len(), grid.len());
}
}
row.push_back((
char != '#',
Expand Down
58 changes: 32 additions & 26 deletions tests/programs/aoc_2024/day_17.vi
A92E
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,41 @@ pub fn main(&io: &IO) {
let &opcode = program.at(ip).unwrap();
let &operand = program.at(ip + 1).unwrap();
let fn combo() {
if operand == 4 {
a
} else if operand == 5 {
b
} else if operand == 6 {
c
} else {
operand
when {
operand == 4 { a }
operand == 5 { b }
operand == 6 { c }
_ { operand }
}
}
if opcode == 0 {
a >>= combo();
} else if opcode == 1 {
b ^= operand;
} else if opcode == 2 {
b = combo() & 7;
} else if opcode == 3 {
if a != 0 {
ip = operand - 2;
when {
opcode == 0 {
a >>= combo();
}
opcode == 1 {
b ^= operand;
}
opcode == 2 {
b = combo() & 7;
}
opcode == 3 {
if a != 0 {
ip = operand - 2;
}
}
opcode == 4 {
b ^= c;
}
opcode == 5 {
let n = combo() & 7;
output ++= [n];
}
opcode == 6 {
b = a >> combo();
}
opcode == 7 {
c = a >> combo();
}
} else if opcode == 4 {
b ^= c;
} else if opcode == 5 {
let n = combo() & 7;
output ++= [n];
} else if opcode == 6 {
b = a >> combo();
} else if opcode == 7 {
c = a >> combo();
}
ip += 2;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/programs/aoc_2024/day_20.vi
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ pub fn main(&io: &IO) {
while io.read_line() is Some(line) {
let row = [];
for char in line!.into_iter() {
if char == 'S' {
start = (row.len(), grid.len())
} else if char == 'E' {
end = (row.len(), grid.len())
when {
char == 'S' { start = (row.len(), grid.len()) }
char == 'E' { end = (row.len(), grid.len()) }
}
row.push_back(char != '#');
}
Expand Down
Loading
0