diff --git a/AdventOfCode.code-workspace b/AdventOfCode.code-workspace index d08bcee..2f2d09e 100644 --- a/AdventOfCode.code-workspace +++ b/AdventOfCode.code-workspace @@ -6,6 +6,9 @@ ], "settings": { "editor.wordBasedSuggestions": false, - "javascript.suggest.names": false + "javascript.suggest.names": false, + "rust-analyzer.linkedProjects": [ + "./rust/Cargo.toml" + ] } } \ No newline at end of file diff --git a/Rust/day1/Cargo.toml b/Rust/day1/Cargo.toml deleted file mode 100644 index 014a020..0000000 --- a/Rust/day1/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "day1" -version = "0.1.0" -authors = ["GitHub "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] - -[[bin]] -name = "day1" -path = "main.rs" diff --git a/Rust/day1/main.rs b/Rust/day1/main.rs deleted file mode 100644 index f4893e9..0000000 --- a/Rust/day1/main.rs +++ /dev/null @@ -1,64 +0,0 @@ -use std::fs::File; -use std::io; -use std::io::prelude::*; -use std::path::Path; - -fn parse_input(buf: B) -> Vec { - buf.lines() - .map(|line| { - line.expect("Failed to read line.") - .parse() - .expect("Failed to parse line input.") - }) - .collect() -} - -fn puzzle_a(nums: &[u64]) -> Option { - for a in nums { - for b in nums { - if a + b == 2020 { - return Some(a * b); - } - } - } - None -} - -fn puzzle_b(nums: &[u64]) -> Option { - for a in nums { - for b in nums { - for c in nums { - if a + b + c == 2020 { - return Some(a * b * c); - } - } - } - } - - None -} - -fn open_file

(filename: P) -> io::Result> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file)) -} - -fn main() -> Result<(), &'static str> { - let buffer = open_file("input").unwrap(); - let puzzle_input = parse_input(buffer); - - println!( - "Part A:\n{}", - puzzle_a(&puzzle_input).expect("No solution for A found.") - ); - println!("--------"); - println!( - "Part B:\n{}", - puzzle_b(&puzzle_input).expect("No solution for B found.") - ); - - Ok(()) -} diff --git a/Rust/.gitignore b/rust/.gitignore similarity index 100% rename from Rust/.gitignore rename to rust/.gitignore diff --git a/Rust/day1/Cargo.lock b/rust/Cargo.lock similarity index 64% rename from Rust/day1/Cargo.lock rename to rust/Cargo.lock index 464d0c4..a0cd482 100644 --- a/Rust/day1/Cargo.lock +++ b/rust/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] -name = "day1" -version = "0.1.0" +name = "advent-of-code" +version = "20.1.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..67efdd7 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "advent-of-code" +version = "20.1.2" +edition = "2021" +resolver = "2" + +[profile.release] +strip = "symbols" +lto = "fat" +opt-level = 3 +codegen-units = 1 + +[profile.dev.package."*"] +opt-level = 3 +codegen-units = 1 +[profile.release.package."*"] +opt-level = 3 +codegen-units = 1 +[profile.dev.build-override] +opt-level = 3 +codegen-units = 1 +[profile.release.build-override] +opt-level = 3 +codegen-units = 1 + +[[bin]] +name = "day01" +path = "src/day01/main.rs" diff --git a/rust/src/day00/input.txt b/rust/src/day00/input.txt new file mode 100644 index 0000000..ee01333 --- /dev/null +++ b/rust/src/day00/input.txt @@ -0,0 +1 @@ +Placeholder input \ No newline at end of file diff --git a/rust/src/day00/main.rs b/rust/src/day00/main.rs new file mode 100644 index 0000000..cc219d2 --- /dev/null +++ b/rust/src/day00/main.rs @@ -0,0 +1,11 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +pub fn main() { + part_1(INPUT); + part_2(INPUT); +} diff --git a/rust/src/day00/part_1.rs b/rust/src/day00/part_1.rs new file mode 100644 index 0000000..9673a09 --- /dev/null +++ b/rust/src/day00/part_1.rs @@ -0,0 +1,24 @@ +pub(crate) fn part_1(_input: &'static str) -> u64 { + todo!("Part 1") +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_sample_solution() { + assert_eq!( + super::part_1(SAMPLE_INPUT), + todo!("Add result from example part 1") + ); + } + + #[test] + fn test_with_solution() { + assert_eq!( + super::part_1(crate::INPUT), + todo!("Add result for solved part 1") + ); + } +} diff --git a/rust/src/day00/part_2.rs b/rust/src/day00/part_2.rs new file mode 100644 index 0000000..928ee24 --- /dev/null +++ b/rust/src/day00/part_2.rs @@ -0,0 +1,24 @@ +pub(crate) fn part_2(_input: &'static str) -> u64 { + todo!("Part 2") +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_sample_solution() { + assert_eq!( + super::part_2(SAMPLE_INPUT), + todo!("Add result from example part 2") + ); + } + + #[test] + fn test_with_solution() { + assert_eq!( + super::part_2(crate::INPUT), + todo!("Add result for solved part 2") + ); + } +} diff --git a/rust/src/day00/sample_input.txt b/rust/src/day00/sample_input.txt new file mode 100644 index 0000000..e69de29 diff --git a/Rust/day1/input b/rust/src/day01/input.txt similarity index 100% rename from Rust/day1/input rename to rust/src/day01/input.txt diff --git a/rust/src/day01/main.rs b/rust/src/day01/main.rs new file mode 100644 index 0000000..b853910 --- /dev/null +++ b/rust/src/day01/main.rs @@ -0,0 +1,19 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +fn parse_input(input: &'static str) -> Vec { + input + .lines() + .map(|line| line.parse().expect("Failed to parse line input.")) + .collect() +} + +pub fn main() { + let input = parse_input(INPUT); + part_1(&input); + part_2(&input); +} diff --git a/rust/src/day01/part_1.rs b/rust/src/day01/part_1.rs new file mode 100644 index 0000000..c29123d --- /dev/null +++ b/rust/src/day01/part_1.rs @@ -0,0 +1,27 @@ +pub(crate) fn part_1(nums: &[u64]) -> u64 { + for a in nums { + for b in nums { + if a + b == 2020 { + let result = a * b; + println!("Part 1: {result}"); + return result; + } + } + } + unreachable!("No solution found for part 1"); +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_1(&crate::parse_input(SAMPLE_INPUT)), 514579); + } + + #[test] + fn test_with_solution() { + assert_eq!(super::part_1(&crate::parse_input(crate::INPUT)), 964875); + } +} diff --git a/rust/src/day01/part_2.rs b/rust/src/day01/part_2.rs new file mode 100644 index 0000000..1d0f3e6 --- /dev/null +++ b/rust/src/day01/part_2.rs @@ -0,0 +1,29 @@ +pub(crate) fn part_2(nums: &[u64]) -> u64 { + for a in nums { + for b in nums { + for c in nums { + if a + b + c == 2020 { + let result = a * b * c; + println!("Part 2: {result}"); + return result; + } + } + } + } + unreachable!("No solution found for part 2") +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_2(&crate::parse_input(SAMPLE_INPUT)), 241861950); + } + + #[test] + fn test_with_solution() { + assert_eq!(super::part_2(&crate::parse_input(crate::INPUT)), 158661360); + } +} diff --git a/rust/src/day01/sample_input.txt b/rust/src/day01/sample_input.txt new file mode 100644 index 0000000..0bb977d --- /dev/null +++ b/rust/src/day01/sample_input.txt @@ -0,0 +1,6 @@ +1721 +979 +366 +299 +675 +1456 \ No newline at end of file