Skip to content

Commit

Permalink
Add day 01
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishco committed Dec 1, 2024
1 parent 39a02a4 commit cfbd90d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
6 changes: 6 additions & 0 deletions data/examples/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
77 changes: 73 additions & 4 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
use std::collections::HashMap;

advent_of_code::solution!(1);

pub fn part_one(input: &str) -> Option<u32> {
None
// println!("{}", input);
let mut column1 = Vec::new();
let mut column2 = Vec::new();

for line in input.lines(){
// let line = line?;
let numbers: Vec<i32> = line
.split_whitespace()
.filter_map(|s| s.parse().ok())
.collect();

if numbers.len() == 2 { // Ensure we have two numbers
column1.push(numbers[0].clone());
column2.push(numbers[1].clone());
}
}
println!("{:?}", column1);
println!("{:?}", column2);

column1.sort();//sort_by(|a, b| a.cmp(b));
column2.sort();// (|a, b| a.cmp(b));

// let result:i32 = column1.iter()
// .zip(column1.iter())
// .map(|(&left, &right)| (left - right).abs())
// .sum();
let mut result: i32 = 0;
for i in 0..column1.len() {
let left = column1[i];
let right = column2[i];
result += (left - right).abs();
}

Some(result as u32)
}

pub fn part_two(input: &str) -> Option<u32> {
None
let mut column1 = Vec::new();
let mut column2 = Vec::new();

for line in input.lines(){
// let line = line?;
let numbers: Vec<i32> = line
.split_whitespace()
.filter_map(|s| s.parse().ok())
.collect();

if numbers.len() == 2 {
column1.push(numbers[0].clone());
column2.push(numbers[1].clone());
}
}
// println!("{:?}", column1);
// println!("{:?}", column2);

column1.sort();
column2.sort();

let mut count_map = HashMap::new();
for &num in &column2 {
*count_map.entry(num).or_insert(0) += 1;
}

let mut result: i32 = 0;
for i in 0..column1.len() {
let left = column1[i];

let count = count_map.get(&left).unwrap_or(&0);
result += left *count;
}

Some(result as u32)
}

#[cfg(test)]
Expand All @@ -15,12 +84,12 @@ mod tests {
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(11));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(31));
}
}

0 comments on commit cfbd90d

Please sign in to comment.