Day 01
This commit is contained in:
parent
a4459a59d1
commit
6808dc5da7
9 changed files with 1101 additions and 5 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -1,6 +1,6 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "addr2line"
|
name = "addr2line"
|
||||||
|
@ -19,7 +19,7 @@ checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "advent-of-code-2024"
|
name = "advent-of-code-2024"
|
||||||
version = "24.0.0"
|
version = "24.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"reqwest",
|
"reqwest",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
authors = ["Toby <toby@tobot.dev>"]
|
authors = ["Toby <toby@tobot.dev>"]
|
||||||
name = "advent-of-code-2024"
|
name = "advent-of-code-2024"
|
||||||
version = "24.0.0"
|
version = "24.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "get_input"
|
default-run = "get_input"
|
||||||
|
|
||||||
|
@ -31,6 +31,10 @@ name = "template"
|
||||||
path = "src/day00/main.rs"
|
path = "src/day00/main.rs"
|
||||||
test = false
|
test = false
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "day01"
|
||||||
|
path = "src/day01/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "0.12.9", features = ["blocking"] }
|
reqwest = { version = "0.12.9", features = ["blocking"] }
|
||||||
|
|
||||||
|
|
1000
day01/input.txt
Normal file
1000
day01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
92
day01/main.rs
Normal file
92
day01/main.rs
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
pub const INPUT: &str = include_str!("./input.txt");
|
||||||
|
|
||||||
|
fn parse_input(input: &'static str) -> Vec<[u32; 2]> {
|
||||||
|
let input = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| -> [u32; 2] {
|
||||||
|
let nums = line
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|num| num.parse::<u32>().unwrap())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
[nums[0], nums[1]]
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
input
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let input = parse_input(INPUT);
|
||||||
|
let result_1 = part_1(&input);
|
||||||
|
println!("{result_1}");
|
||||||
|
let result_2 = part_2(&input);
|
||||||
|
println!("{result_2}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(input: &Vec<[u32; 2]>) -> u32 {
|
||||||
|
let mut left = vec![];
|
||||||
|
let mut right = vec![];
|
||||||
|
for pair in input {
|
||||||
|
left.push(pair[0]);
|
||||||
|
right.push(pair[1]);
|
||||||
|
}
|
||||||
|
left.sort_unstable();
|
||||||
|
right.sort_unstable();
|
||||||
|
let mut diff = 0;
|
||||||
|
for i in 0..left.len() {
|
||||||
|
diff += u32::abs_diff(left[i], right[i]);
|
||||||
|
}
|
||||||
|
diff
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(input: &Vec<[u32; 2]>) -> u32 {
|
||||||
|
let mut left = vec![];
|
||||||
|
let mut right = vec![];
|
||||||
|
for pair in input {
|
||||||
|
left.push(pair[0]);
|
||||||
|
right.push(pair[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = 0;
|
||||||
|
|
||||||
|
for num in left {
|
||||||
|
result += num * right.iter().filter(|&&x| x == num).count() as u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::parse_input;
|
||||||
|
|
||||||
|
const SAMPLE_INPUT: &str = r#"3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3"#;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_1_with_sample_solution() {
|
||||||
|
let input = parse_input(SAMPLE_INPUT);
|
||||||
|
assert_eq!(super::part_1(&input), 11)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_1_with_solution() {
|
||||||
|
let input = parse_input(super::INPUT);
|
||||||
|
assert_eq!(super::part_1(&input), 2192892);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_2_with_sample_solution() {
|
||||||
|
let input = parse_input(SAMPLE_INPUT);
|
||||||
|
assert_eq!(super::part_2(&input), 31)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_2_with_solution() {
|
||||||
|
let input = parse_input(super::INPUT);
|
||||||
|
assert_eq!(super::part_2(&input), 22962826);
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.header("Cookie", format!("session={session}"))
|
.header("Cookie", format!("session={session}"))
|
||||||
.build()?;
|
.build()?;
|
||||||
let input = client.execute(request)?.text()?;
|
let input = client.execute(request)?.text()?;
|
||||||
fs::create_dir_all(format!("src/day{day:02}/"))?;
|
fs::create_dir_all(format!("day{day:02}/"))?;
|
||||||
fs::write(format!("src/day{day:02}/input.txt"), input)?;
|
fs::write(format!("day{day:02}/input.txt"), input)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue