Skip to content

Commit

Permalink
Fix day 01
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishco committed Dec 1, 2024
1 parent cfbd90d commit 27fb4ab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
Empty file added data/examples/02.txt
Empty file.
34 changes: 11 additions & 23 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@ use std::collections::HashMap;
advent_of_code::solution!(1);

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

for line in input.lines(){
// let line = line?;
for line in input.lines() {
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());
if numbers.len() == 2 {
column1.push(numbers[0]);
column2.push(numbers[1]);
}
}
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();
column1.sort();
column2.sort();
let mut result: i32 = 0;
for i in 0..column1.len() {
let left = column1[i];
Expand All @@ -43,16 +34,15 @@ pub fn part_two(input: &str) -> Option<u32> {
let mut column1 = Vec::new();
let mut column2 = Vec::new();

for line in input.lines(){
// let line = line?;
for line in input.lines() {
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());
column1.push(numbers[0]);
column2.push(numbers[1]);
}
}
// println!("{:?}", column1);
Expand All @@ -67,11 +57,9 @@ pub fn part_two(input: &str) -> Option<u32> {
}

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

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

Some(result as u32)
Expand Down
26 changes: 26 additions & 0 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
advent_of_code::solution!(2);

pub fn part_one(input: &str) -> Option<u32> {
None
}

pub fn part_two(input: &str) -> Option<u32> {
None
}

#[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);
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ fn main() {
AppArguments::Time { day, all, store } => time::handle(day, all, store),
AppArguments::Download { day } => download::handle(day),
AppArguments::Read { day } => read::handle(day),
AppArguments::Scaffold { day, download, overwrite } => {
AppArguments::Scaffold {
day,
download,
overwrite,
} => {
scaffold::handle(day, overwrite);
if download {
download::handle(day);
Expand Down

0 comments on commit 27fb4ab

Please sign in to comment.