Upload day 1
Some checks are pending
Continuous Integration / Continuous Integration (push) Waiting to run
Some checks are pending
Continuous Integration / Continuous Integration (push) Waiting to run
This commit is contained in:
parent
36608db7b3
commit
51e34a6b06
3 changed files with 93 additions and 0 deletions
|
@ -6,6 +6,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
|
|||
|
||||
<!--- advent_readme_stars table --->
|
||||
|
||||
<!--- benchmarking table --->
|
||||
## Benchmarks
|
||||
|
||||
| Day | Part 1 | Part 2 |
|
||||
| :---: | :---: | :---: |
|
||||
| [Day 1](./src/bin/01.rs) | `89.5µs` | `125.4µs` |
|
||||
|
||||
**Total: 0.21ms**
|
||||
<!--- benchmarking table --->
|
||||
|
||||
---
|
||||
|
|
0
data/examples/01.txt
Normal file
0
data/examples/01.txt
Normal file
85
src/bin/01.rs
Normal file
85
src/bin/01.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
advent_of_code::solution!(1);
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: Vec<u32> = Vec::new();
|
||||
let mut right: Vec<u32> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
|
||||
if let (Some(l), Some(r)) = (split.next(), split.next()) {
|
||||
if let (Ok(l_number), Ok(r_number)) = (l.parse::<u32>(), r.parse::<u32>()) {
|
||||
left.push(l_number);
|
||||
right.push(r_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let total_distance: u32 = left
|
||||
.iter()
|
||||
.zip(right.iter())
|
||||
.map(|(&x, &y)| if x > y { x - y } else { y - x })
|
||||
.sum();
|
||||
|
||||
Some(total_distance)
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
if input.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut left: Vec<u32> = Vec::new();
|
||||
let mut right: Vec<u32> = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
|
||||
if let (Some(l), Some(r)) = (split.next(), split.next()) {
|
||||
if let (Ok(l_number), Ok(r_number)) = (l.parse::<u32>(), r.parse::<u32>()) {
|
||||
left.push(l_number);
|
||||
right.push(r_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let mut right_list_counts: HashMap<u32, u32> = HashMap::new();
|
||||
for &num in &right {
|
||||
*right_list_counts.entry(num).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let total_similarity_score: u32 = left
|
||||
.iter()
|
||||
.map(|&num| num * right_list_counts.get(&num).unwrap_or(&0))
|
||||
.sum();
|
||||
|
||||
Some(total_similarity_score)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue