Skip to content

Commit 1b4c937

Browse files
committed
Add gen.sh to create Rust stub
1 parent 382668e commit 1b4c937

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

native/aoc/gen.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/zsh
2+
3+
FULL_YEAR=$1
4+
YEAR=${1:(-2)}
5+
DAY=$(printf '%02d' $2)
6+
SRC_FILE=./src/year_${FULL_YEAR}/day_${YEAR}_${DAY}.rs
7+
8+
if [[ ! -e $SRC_FILE ]]; then
9+
touch "$SRC_FILE"
10+
cat << EOF > "$SRC_FILE"
11+
pub fn solve_${YEAR}_${DAY}(raw_input: String) -> (i32, i32) {
12+
let input = process(&raw_input);
13+
(part_1(&input), part_2(&input))
14+
}
15+
16+
fn process(raw_input: &str) -> Vec<&str> {
17+
raw_input
18+
.trim()
19+
.split('\n')
20+
.collect()
21+
}
22+
23+
fn part_1(input: &[&str]) -> i32 {
24+
input.len() as i32
25+
}
26+
27+
fn part_2(input: &[&str]) -> i32 {
28+
input.len() as i32
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use crate::util::io_helpers::read_input_from_resources;
34+
use super::*;
35+
36+
const YEAR: i16 = $1;
37+
const DAY: i8 = $2;
38+
39+
#[test]
40+
fn test_process() {
41+
let given = "hello\nthere";
42+
let expected = vec!["hello", "there"];
43+
44+
assert_eq!(process(given), expected);
45+
}
46+
47+
#[test]
48+
fn test_solution() {
49+
let given = read_input_from_resources(YEAR, DAY, false);
50+
let expected = (3, 3);
51+
52+
assert_eq!(solve_${YEAR}_${DAY}(given), expected);
53+
}
54+
}
55+
EOF
56+
else
57+
echo "Source ${SRC_FILE} already exists"
58+
fi

0 commit comments

Comments
 (0)