Skip to content

Commit

Permalink
add day2
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 2, 2024
1 parent ae9d26f commit ae0b249
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 2024/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"day1",
# "day2",
"day2",
# "day3",
# "day4",
# "day5",
Expand Down
19 changes: 19 additions & 0 deletions 2024/day2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
input.txt
flamegraph.svg
perf.data*
### Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

11 changes: 11 additions & 0 deletions 2024/day2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "day2"
authors = ["mirsella <[email protected]>"]
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.13.0"

28 changes: 28 additions & 0 deletions 2024/day2/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;
use std::{env, fs};

fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> {
let mut contents = String::new();
File::open(file_path)?.read_to_string(&mut contents)?;
let new_contents = contents.replace(old, new);
if contents != new_contents {
println!("Updating {}", file_path.display());
fs::write(file_path, new_contents)?;
}
Ok(())
}

fn main() -> io::Result<()> {
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
replace_in_file(
&"../Cargo.toml".into(),
&format!("# \"{pkg_name}\""),
&format!("\"{pkg_name}\""),
)?;

replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?;

Ok(())
}
68 changes: 68 additions & 0 deletions 2024/day2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use itertools::Itertools;

fn part1(input: &str) -> usize {
input
.lines()
.map(|l| {
let levels = l
.split_whitespace()
.map(|n| n.parse::<usize>().unwrap())
.collect_vec();
let ordering = levels[0].cmp(&levels[1]);
levels
.into_iter()
.tuple_windows()
.all(|(a, b)| a.cmp(&b) == ordering && a.abs_diff(b) >= 1 && a.abs_diff(b) <= 3)
})
.filter(|v| *v)
.count()
}
fn part2(input: &str) -> usize {
input
.lines()
.map(|l| {
let levels = l
.split_whitespace()
.map(|n| n.parse::<usize>().unwrap())
.collect_vec();
(-1..levels.len() as isize).any(|i| {
let new_levels = if i < 0 {
levels.clone()
} else {
let mut l = levels.clone();
l.remove(i as usize);
l
};
let ordering = new_levels[0].cmp(&new_levels[1]);
new_levels
.into_iter()
.tuple_windows()
.all(|(a, b)| a.cmp(&b) == ordering && a.abs_diff(b) >= 1 && a.abs_diff(b) <= 3)
})
})
.filter(|v| *v)
.count()
}
fn main() {
let input = include_str!("../input.txt");
println!("Part 1: {}", part1(input));
println!("Part 2: {}", part2(input));
}

#[cfg(test)]
mod tests {
const INPUT: &str = "7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9";
#[test]
fn part1() {
assert_eq!(super::part1(INPUT), 2);
}
#[test]
fn part2() {
assert_eq!(super::part2(INPUT), 4);
}
}

0 comments on commit ae0b249

Please sign in to comment.