mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
start AOC 2022
This commit is contained in:
parent
fd45c3bfb1
commit
c58ac4586a
11
.github/workflows/rust.yml
vendored
11
.github/workflows/rust.yml
vendored
@ -31,3 +31,14 @@ jobs:
|
||||
run: cd 2021 && cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cd 2021 && cargo test --verbose
|
||||
|
||||
tests_2022:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Build
|
||||
run: cd 2022 && cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cd 2022 && cargo test --verbose
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@
|
||||
|
||||
#/target
|
||||
*/Cargo.lock
|
||||
.DS_Store
|
||||
|
10
2022/Cargo.toml
Normal file
10
2022/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "advent-of-code-2022"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
advent_of_code_macro = { path = "../advent_of_code_macro" }
|
||||
paste = "1.0.9"
|
2253
2022/input/day_1.txt
Normal file
2253
2022/input/day_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
14
2022/input/day_1_sample.txt
Normal file
14
2022/input/day_1_sample.txt
Normal file
@ -0,0 +1,14 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
2253
2022/input/day_2.txt
Normal file
2253
2022/input/day_2.txt
Normal file
File diff suppressed because it is too large
Load Diff
14
2022/input/day_2_sample.txt
Normal file
14
2022/input/day_2_sample.txt
Normal file
@ -0,0 +1,14 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
43
2022/src/day_1.rs
Normal file
43
2022/src/day_1.rs
Normal file
@ -0,0 +1,43 @@
|
||||
advent_of_code_macro::solve_problem!(
|
||||
day 1,
|
||||
Input Vec<Vec<i32>>,
|
||||
parse |input: &str| {
|
||||
let lines = input.split('\n');
|
||||
let mut output = vec![];
|
||||
let mut curr_elf = vec![];
|
||||
for line in lines {
|
||||
if line.is_empty() {
|
||||
output.push(curr_elf);
|
||||
curr_elf = vec![];
|
||||
} else {
|
||||
curr_elf.push(line.parse().unwrap())
|
||||
}
|
||||
}
|
||||
output
|
||||
},
|
||||
part one |data: Input| {
|
||||
data.iter().map(|cals| cals.iter().sum()).max().unwrap()
|
||||
},
|
||||
part two |data: Input| {
|
||||
use std::mem::swap;
|
||||
let cals = data.iter().map(|cals| cals.iter().sum()).fold(
|
||||
(-1, -1, -1),
|
||||
|(mut first, mut second, mut third), mut curr: i32| {
|
||||
if curr > first {
|
||||
swap(&mut first, &mut curr);
|
||||
}
|
||||
if curr > second {
|
||||
swap(&mut second, &mut curr);
|
||||
}
|
||||
if curr > third {
|
||||
swap(&mut third, &mut curr);
|
||||
}
|
||||
|
||||
(first, second, third)
|
||||
},
|
||||
);
|
||||
cals.0 + cals.1 + cals.2
|
||||
},
|
||||
sample tests [24_000, 45_000],
|
||||
star tests [68_923, 200_044]
|
||||
);
|
15
2022/src/day_2.rs
Normal file
15
2022/src/day_2.rs
Normal file
@ -0,0 +1,15 @@
|
||||
advent_of_code_macro::solve_problem!(
|
||||
day 2,
|
||||
Input i32,
|
||||
parse |_input: &str| {
|
||||
1 + 1
|
||||
},
|
||||
part one |data: Input| {
|
||||
data + 5
|
||||
},
|
||||
part two |data: Input| {
|
||||
data + 10
|
||||
},
|
||||
sample tests [7, 12],
|
||||
star tests [7, 12]
|
||||
);
|
3
2022/src/lib.rs
Normal file
3
2022/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
||||
#[allow(dead_code)]
|
||||
mod day_1;
|
||||
mod day_2;
|
9
advent_of_code_macro/Cargo.toml
Normal file
9
advent_of_code_macro/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "advent_of_code_macro"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
paste = "1.0.9"
|
50
advent_of_code_macro/src/lib.rs
Normal file
50
advent_of_code_macro/src/lib.rs
Normal file
@ -0,0 +1,50 @@
|
||||
#[macro_export]
|
||||
macro_rules! solve_problem {
|
||||
(day $day:literal, Input $input_type:ty, parse $parse_input:expr, part one $part_one:expr, part two $part_two:expr, sample tests [$part_one_sample_ans:literal, $part_two_sample_ans:literal], star tests [$part_one_ans:literal, $part_two_ans:literal]) => {
|
||||
#[cfg(test)]
|
||||
paste::paste! {
|
||||
mod [<day_ $day _tests>] {
|
||||
type Input = $input_type;
|
||||
fn parse_input(s: &str) -> Input {
|
||||
$parse_input(s)
|
||||
}
|
||||
|
||||
fn part_one_solution(data: Input) -> i32 {
|
||||
$part_one(data)
|
||||
}
|
||||
|
||||
fn part_two_solution(data: Input) -> i32 {
|
||||
$part_two(data)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_one_sample() {
|
||||
let input = include_str!(concat!("../input/day_", $day, "_sample.txt"));
|
||||
let data = parse_input(input);
|
||||
assert_eq!($part_one_sample_ans, part_one_solution(data));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_one() {
|
||||
let input = include_str!(concat!("../input/day_", $day, ".txt"));
|
||||
let data = parse_input(input);
|
||||
assert_eq!($part_one_ans, part_one_solution(data));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_two_sample() {
|
||||
let input = include_str!(concat!("../input/day_", $day, "_sample.txt"));
|
||||
let data = parse_input(input);
|
||||
assert_eq!($part_two_sample_ans, part_two_solution(data));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part_two() {
|
||||
let input = include_str!(concat!("../input/day_", $day, ".txt"));
|
||||
let data = parse_input(input);
|
||||
assert_eq!($part_two_ans, part_two_solution(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user