diff --git a/2022/rust/Cargo.toml b/2022/rust/Cargo.toml index 8c69839..72be5db 100644 --- a/2022/rust/Cargo.toml +++ b/2022/rust/Cargo.toml @@ -10,4 +10,12 @@ path = "src/day1.rs" name = "day2" path = "src/day2.rs" +[[bin]] +name = "day2-alt1" +path = "src/day2-alt1.rs" + +[[bin]] +name = "day3" +path = "src/day3.rs" + [dependencies] diff --git a/2022/rust/src/day2-alt1.rs b/2022/rust/src/day2-alt1.rs new file mode 100644 index 0000000..6fef5fb --- /dev/null +++ b/2022/rust/src/day2-alt1.rs @@ -0,0 +1,44 @@ +mod common; + +fn main() { + let input = common::day_input(2); + let bytes = input.as_bytes(); + println!("Part 1: {}", part1(bytes)); + println!("Part 2: {}", part2(bytes)); +} + +fn sum_outcomes(b: &[u8], t: [isize; 3]) -> i32 { + (0..b.len()).step_by(4).fold(0, |p, x| { + let y: usize = ((b[x] - 65) % 3).into(); + p + [[4, 8, 3], [1, 5, 9], [7, 2, 6]][y as usize] + [((((b[x + 2] - 88) % 3) as isize + t[y]).rem_euclid(3)) as usize] + }) +} + +fn part1(input: &[u8]) -> i32 { + sum_outcomes(input, [0, 0, 0]) +} + +fn part2(input: &[u8]) -> i32 { + sum_outcomes(input, [-1, 0, 1]) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &[u8] = "A Y +B X +C Z" + .as_bytes(); + + #[test] + fn test_part1() { + assert_eq!(part1(TEST_INPUT), 15) + } + + #[test] + fn test_part2() { + assert_eq!(part2(TEST_INPUT), 12) + } +} diff --git a/2022/rust/src/day2.rs b/2022/rust/src/day2.rs index 925fd97..3ab4a9e 100644 --- a/2022/rust/src/day2.rs +++ b/2022/rust/src/day2.rs @@ -3,7 +3,7 @@ use std::str::FromStr; mod common; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Copy, Clone)] enum Choice { Rock = 1, Paper = 2, diff --git a/2022/rust/src/day3.rs b/2022/rust/src/day3.rs new file mode 100644 index 0000000..084f9cc --- /dev/null +++ b/2022/rust/src/day3.rs @@ -0,0 +1,32 @@ +mod common; + +fn main() { + let input = common::day_input(2); + println!("Part 1: {}", part1(&input)); + println!("Part 2: {}", part2(&input)); +} + +fn part1(input: &str) -> i32 { + 0 +} + +fn part2(input: &str) -> i32 { + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = ""; + + #[test] + fn test_part1() { + assert_eq!(part1(TEST_INPUT), 0) + } + + #[test] + fn test_part2() { + assert_eq!(part2(TEST_INPUT), 0) + } +} diff --git a/2022/rust/src/scaffold.rs b/2022/rust/src/scaffold.rs new file mode 100644 index 0000000..084f9cc --- /dev/null +++ b/2022/rust/src/scaffold.rs @@ -0,0 +1,32 @@ +mod common; + +fn main() { + let input = common::day_input(2); + println!("Part 1: {}", part1(&input)); + println!("Part 2: {}", part2(&input)); +} + +fn part1(input: &str) -> i32 { + 0 +} + +fn part2(input: &str) -> i32 { + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = ""; + + #[test] + fn test_part1() { + assert_eq!(part1(TEST_INPUT), 0) + } + + #[test] + fn test_part2() { + assert_eq!(part2(TEST_INPUT), 0) + } +}