Skip to content

Commit

Permalink
solve day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gaito-20 committed Dec 1, 2024
1 parent 61f450a commit d0d67c8
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use adv_code_2024::*;
use anyhow::*;
use std::fs::File;
use std::io::{BufRead, BufReader};
use code_timing_macros::time_snippet;
use const_format::concatcp;
use adv_code_2024::*;
use itertools::Itertools;
use std::fs::File;
use std::io::{BufRead, BufReader};

const DAY: &str = "01";
const INPUT_FILE: &str = concatcp!("input/", DAY, ".txt");
Expand All @@ -23,6 +24,22 @@ fn main() -> Result<()> {
//region Part 1
println!("=== Part 1 ===");

fn read_challenge<R: BufRead>(reader: R) -> (Vec<i32>, Vec<i32>) {
let mut left: Vec<i32> = Vec::new();
let mut right: Vec<i32> = Vec::new();

for line in reader.lines() {
let line = line.unwrap();

let mut iter = line.split_whitespace();
left.push(iter.next().unwrap().parse::<i32>().unwrap());
right.push(iter.next().unwrap().parse::<i32>().unwrap());
}
assert_eq!(left.len(), right.len());

(left, right)
}

fn pop_minimum(list: &mut Vec<i32>) -> i32 {
let mut min_index: usize = 0;
let mut minimum: &i32 = list.first().unwrap();
Expand All @@ -36,26 +53,13 @@ fn main() -> Result<()> {
}

fn part1<R: BufRead>(reader: R) -> Result<usize> {

let mut left: Vec<i32> = Vec::new();
let mut right: Vec<i32> = Vec::new();

for line in reader.lines() {
let line = line?;

let mut iter = line.split_whitespace();
left.push(iter.next().unwrap().parse::<i32>()?);
right.push(iter.next().unwrap().parse::<i32>()?);
}

assert_eq!(left.len(), right.len());

let (mut left, mut right) = read_challenge(reader);
let mut distance = 0;

while !left.is_empty() {
let left_min = pop_minimum(&mut left);
let right_min = pop_minimum(&mut right);

distance += (left_min - right_min).abs();
}

Expand All @@ -70,17 +74,26 @@ fn main() -> Result<()> {
//endregion

//region Part 2
// println!("\n=== Part 2 ===");
//
// fn part2<R: BufRead>(reader: R) -> Result<usize> {
// Ok(0)
// }
//
// assert_eq!(0, part2(BufReader::new(TEST.as_bytes()))?);
//
// let input_file = BufReader::new(File::open(INPUT_FILE)?);
// let result = time_snippet!(part2(input_file)?);
// println!("Result = {}", result);
println!("\n=== Part 2 ===");

fn part2<R: BufRead>(reader: R) -> Result<usize> {
let (left, right) = read_challenge(reader);

let right_map = right.iter().counts();
let mut distance = 0;

for elem in left {
distance += (elem as usize) * right_map.get(&elem).unwrap_or(&0);
}

Ok(distance)
}

assert_eq!(31, part2(BufReader::new(TEST.as_bytes()))?);

let input_file = BufReader::new(File::open(INPUT_FILE)?);
let result = time_snippet!(part2(input_file)?);
println!("Result = {}", result);
//endregion

Ok(())
Expand Down

0 comments on commit d0d67c8

Please sign in to comment.