diff options
author | Guillermo Ramos | 2024-12-04 10:28:51 +0100 |
---|---|---|
committer | Guillermo Ramos | 2024-12-04 10:51:41 +0100 |
commit | 1c9620d4666fdf41ef751f100dfb17f14133dd35 (patch) | |
tree | ece2ef499492b2426b13bd9b48f98f35a60123c2 | |
parent | a42dc9c3efb52a74fc581835f585679e8a5e2bb1 (diff) | |
download | AoC-1c9620d4666fdf41ef751f100dfb17f14133dd35.tar.gz |
2024: move to single Rust crate
-rw-r--r-- | 2024/1/p1.rs | 23 | ||||
-rw-r--r-- | 2024/1/p2.rs | 25 | ||||
-rw-r--r-- | 2024/2/p1.rs | 42 | ||||
-rw-r--r-- | 2024_rust/.gitignore (renamed from 2024/.gitignore) | 2 | ||||
-rw-r--r-- | 2024_rust/Cargo.lock (renamed from 2024/3/Cargo.lock) | 2 | ||||
-rw-r--r-- | 2024_rust/Cargo.toml (renamed from 2024/3/Cargo.toml) | 2 | ||||
-rw-r--r-- | 2024_rust/inputs/1 (renamed from 2024/1/input) | 0 | ||||
-rw-r--r-- | 2024_rust/inputs/2 (renamed from 2024/2/input) | 0 | ||||
-rw-r--r-- | 2024_rust/inputs/3 (renamed from 2024/3/input) | 0 | ||||
-rw-r--r-- | 2024_rust/src/day1.rs | 45 | ||||
-rw-r--r-- | 2024_rust/src/day2.rs (renamed from 2024/2/p2.rs) | 26 | ||||
-rw-r--r-- | 2024_rust/src/day3.rs (renamed from 2024/3/src/main.rs) | 16 | ||||
-rw-r--r-- | 2024_rust/src/dayN.rs | 11 | ||||
-rw-r--r-- | 2024_rust/src/main.rs | 10 |
14 files changed, 92 insertions, 112 deletions
diff --git a/2024/1/p1.rs b/2024/1/p1.rs deleted file mode 100644 index 4ffeb63..0000000 --- a/2024/1/p1.rs +++ /dev/null @@ -1,23 +0,0 @@ -const INPUT_FILE: &str = "input"; - -fn main() { - let input = std::fs::read_to_string(INPUT_FILE).expect("Reading input file"); - let mut left: Vec<u32> = vec![]; - let mut right: Vec<u32> = vec![]; - for line in input.lines() { - let mut fields = line.split_whitespace(); - let e = "Wrong file format"; - left.push(fields.next().expect(e).parse().expect(e)); - right.push(fields.next().expect(e).parse().expect(e)); - } - left.sort_unstable(); - right.sort_unstable(); - - let mut distance: u32 = 0; - for i in 0..left.len() { - let l = left[i]; - let r = right[i]; - distance += if l >= r { l - r } else { r - l }; - } - println!("Distance: {}", distance); -} diff --git a/2024/1/p2.rs b/2024/1/p2.rs deleted file mode 100644 index ebaa4db..0000000 --- a/2024/1/p2.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::collections::HashMap; - -const INPUT_FILE: &str = "input"; - -fn main() { - let input = std::fs::read_to_string(INPUT_FILE).expect("Reading input file"); - let mut left: Vec<u32> = vec![]; - let mut right: HashMap<u32, u32> = HashMap::new(); - for line in input.lines() { - let mut fields = line.split_whitespace(); - let e = "Wrong file format"; - left.push(fields.next().expect(e).parse().expect(e)); - let r: u32 = fields.next().expect(e).parse().expect(e); - match right.get(&r) { - None => right.insert(r, 1), - Some(v) => right.insert(r, v + 1), - }; - } - - let mut distance: u32 = 0; - for l in left { - distance += l * right.get(&l).unwrap_or(&0); - } - println!("Distance: {}", distance); -} diff --git a/2024/2/p1.rs b/2024/2/p1.rs deleted file mode 100644 index 7c0abe3..0000000 --- a/2024/2/p1.rs +++ /dev/null @@ -1,42 +0,0 @@ -const INPUT_FILE: &str = "input"; - -#[derive(PartialEq, Clone, Copy)] -enum Direction { - Up, - Down, - Unknown, -} -use Direction::*; - -fn is_safe(levels: &[u32]) -> bool { - let mut direction = Unknown; - for i in 0..levels.len() - 1 { - let [x, y] = levels[i..=i + 1] else { - unreachable!() - }; - let (diff, d) = if x > y { (x - y, Down) } else { (y - x, Up) }; - if direction == Unknown { - direction = d; - } - if diff == 0 || diff > 3 || direction != d { - return false; - } - } - return true; -} - -fn main() { - let input = std::fs::read_to_string(INPUT_FILE).unwrap(); - - let mut total = 0; - for report in input.lines() { - let levels: Vec<u32> = report - .split_whitespace() - .map(|l| l.parse().unwrap()) - .collect(); - if is_safe(&levels) { - total += 1; - } - } - println!("Levels! {:?}", total); -} diff --git a/2024/.gitignore b/2024_rust/.gitignore index 638e3e9..2b310b5 100644 --- a/2024/.gitignore +++ b/2024_rust/.gitignore @@ -1,2 +1,2 @@ # Cargo generated stuff -*/target +target diff --git a/2024/3/Cargo.lock b/2024_rust/Cargo.lock index 4c00902..3a80814 100644 --- a/2024/3/Cargo.lock +++ b/2024_rust/Cargo.lock @@ -12,7 +12,7 @@ dependencies = [ ] [[package]] -name = "day3" +name = "aoc2024" version = "0.1.0" dependencies = [ "regex", diff --git a/2024/3/Cargo.toml b/2024_rust/Cargo.toml index ee3c600..d8258dc 100644 --- a/2024/3/Cargo.toml +++ b/2024_rust/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "day3" +name = "aoc2024" version = "0.1.0" edition = "2021" diff --git a/2024/1/input b/2024_rust/inputs/1 index 7d1b595..7d1b595 100644 --- a/2024/1/input +++ b/2024_rust/inputs/1 diff --git a/2024/2/input b/2024_rust/inputs/2 index cd46e29..cd46e29 100644 --- a/2024/2/input +++ b/2024_rust/inputs/2 diff --git a/2024/3/input b/2024_rust/inputs/3 index 7ca1d6d..7ca1d6d 100644 --- a/2024/3/input +++ b/2024_rust/inputs/3 diff --git a/2024_rust/src/day1.rs b/2024_rust/src/day1.rs new file mode 100644 index 0000000..0b6c13d --- /dev/null +++ b/2024_rust/src/day1.rs @@ -0,0 +1,45 @@ +pub fn p1(input: &str) -> String { + let mut left: Vec<u32> = vec![]; + let mut right: Vec<u32> = vec![]; + for line in input.lines() { + let mut fields = line.split_whitespace(); + let e = "Wrong file format"; + left.push(fields.next().expect(e).parse().expect(e)); + right.push(fields.next().expect(e).parse().expect(e)); + } + left.sort_unstable(); + right.sort_unstable(); + + let mut distance: u32 = 0; + for i in 0..left.len() { + let l = left[i]; + let r = right[i]; + distance += if l >= r { l - r } else { r - l }; + } + + distance.to_string() +} + +use std::collections::HashMap; + +pub fn p2(input: &str) -> String { + let mut left: Vec<u32> = vec![]; + let mut right: HashMap<u32, u32> = HashMap::new(); + for line in input.lines() { + let mut fields = line.split_whitespace(); + let e = "Wrong file format"; + left.push(fields.next().expect(e).parse().expect(e)); + let r: u32 = fields.next().expect(e).parse().expect(e); + match right.get(&r) { + None => right.insert(r, 1), + Some(v) => right.insert(r, v + 1), + }; + } + + let mut distance: u32 = 0; + for l in left { + distance += l * right.get(&l).unwrap_or(&0); + } + + distance.to_string() +} diff --git a/2024/2/p2.rs b/2024_rust/src/day2.rs index 78224c4..f809141 100644 --- a/2024/2/p2.rs +++ b/2024_rust/src/day2.rs @@ -1,5 +1,3 @@ -const INPUT_FILE: &str = "input"; - #[derive(PartialEq, Clone, Copy)] enum Direction { Up, @@ -15,7 +13,7 @@ fn is_safe(levels: &[u32]) -> bool { unreachable!() }; let (diff, d) = if x > y { (x - y, Down) } else { (y - x, Up) }; - if let Unknown = direction { + if direction == Unknown { direction = d; } if diff == 0 || diff > 3 || direction != d { @@ -25,6 +23,21 @@ fn is_safe(levels: &[u32]) -> bool { return true; } +pub fn p1(input: &str) -> String { + let mut total = 0; + for report in input.lines() { + let levels: Vec<u32> = report + .split_whitespace() + .map(|l| l.parse().unwrap()) + .collect(); + if is_safe(&levels) { + total += 1; + } + } + + total.to_string() +} + fn is_safe_with_dampener(levels: &[u32]) -> bool { for i in 0..levels.len() { let mut levels_without_i: Vec<u32> = levels.to_vec(); @@ -36,9 +49,7 @@ fn is_safe_with_dampener(levels: &[u32]) -> bool { return false; } -fn main() { - let input = std::fs::read_to_string(INPUT_FILE).unwrap(); - +pub fn p2(input: &str) -> String { let mut total = 0; for report in input.lines() { let levels: Vec<u32> = report @@ -49,5 +60,6 @@ fn main() { total += 1; } } - println!("Levels! {:?}", total); + + total.to_string() } diff --git a/2024/3/src/main.rs b/2024_rust/src/day3.rs index 0775483..cdcd62a 100644 --- a/2024/3/src/main.rs +++ b/2024_rust/src/day3.rs @@ -1,8 +1,6 @@ use regex::Regex; -const INPUT_FILE: &str = "input"; - -fn p1(input: &str) { +pub fn p1(input: &str) -> String { let re = Regex::new(r"mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap(); let mut result: u32 = 0; @@ -10,10 +8,10 @@ fn p1(input: &str) { result += x.parse::<u32>().unwrap() * y.parse::<u32>().unwrap(); } - println!("Result: {}", result); + result.to_string() } -fn p2(input: &str) { +pub fn p2(input: &str) -> String { let re = Regex::new(r"do\(\)|don't\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap(); let mut doing = true; @@ -31,11 +29,5 @@ fn p2(input: &str) { } } - println!("Result: {}", result); -} - -fn main() { - let input = std::fs::read_to_string(INPUT_FILE).unwrap(); - p1(&input); - p2(&input); + result.to_string() } diff --git a/2024_rust/src/dayN.rs b/2024_rust/src/dayN.rs new file mode 100644 index 0000000..d325291 --- /dev/null +++ b/2024_rust/src/dayN.rs @@ -0,0 +1,11 @@ +pub fn p1(input: &str) -> String { + let result = "TODO"; + + result.to_string() +} + +pub fn p2(input: &str) -> String { + let result = "TODO"; + + result.to_string() +} diff --git a/2024_rust/src/main.rs b/2024_rust/src/main.rs new file mode 100644 index 0000000..ed94ec2 --- /dev/null +++ b/2024_rust/src/main.rs @@ -0,0 +1,10 @@ +mod day3; +use day3 as current_day; +const INPUT_FILE: &str = "inputs/3"; + +fn main() { + let input = std::fs::read_to_string(INPUT_FILE).unwrap(); + + println!("Result (P1): {}", current_day::p1(&input)); + println!("Result (P2): {}", current_day::p2(&input)); +} |