day 1 pt 2

This commit is contained in:
RussellChamp committed 2024-12-02 13:55:29 -05:00
1 parent 4ac4aa5100
commit 4cc13144f0
1 file changed
+21 -14
+21 -14
View File
@@ -5,7 +5,7 @@ use std::io::{self, BufRead};
use std::path::Path; use std::path::Path;
use regex::Regex; use regex::Regex;
fn solve_part1() { fn solve() {
println!("Day 01: Part 1"); println!("Day 01: Part 1");
println!("GOAL: Find the total distance between the left list and the right list"); println!("GOAL: Find the total distance between the left list and the right list");
println!("ANSWER: What is the total distance between your lists?"); println!("ANSWER: What is the total distance between your lists?");
@@ -18,9 +18,8 @@ fn solve_part1() {
for line in lines.flatten() { for line in lines.flatten() {
let vals: Vec<&str> = re_split.split(&line).collect(); let vals: Vec<&str> = re_split.split(&line).collect();
// println!("{} and {}", vals[0], vals[1]); let a = vals[0].parse::<u32>().unwrap();
let a = vals[0].parse::<i32>().unwrap(); let b = vals[1].parse::<u32>().unwrap();
let b = vals[1].parse::<i32>().unwrap();
vec_a.push(a); vec_a.push(a);
vec_b.push(b); vec_b.push(b);
} }
@@ -30,21 +29,29 @@ fn solve_part1() {
for i in 0..1000 { for i in 0..1000 {
let diff = vec_a[i].abs_diff(vec_b[i]); let diff = vec_a[i].abs_diff(vec_b[i]);
result += diff; result += diff;
println!("Line {}: abs({} - {}) = {}", i+1, vec_a[i], vec_b[i], diff) // println!("Line {}: abs({} - {}) = {}", i+1, vec_a[i], vec_b[i], diff)
} }
} println!("Total {result}");
println!("Total {result}");
}
fn solve_part2 () { println!("Day 01: Part 2");
println!("Day 01: Part 2"); println!("GOAL: Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list");
println!("GOAL: #GOAL2#"); println!("ANSWER: What is their similarity score?");
println!("ANSWER: #ANSWER2#");
result = 0;
for i in 0..1000 {
let needle = vec_a[i];
let found = vec_b.iter().filter(|&&b| b == needle).count() as u32;
let similarity = found * needle;
result += similarity;
println!("Line {}: {} found {} times = {}, total {}", i + 1, needle, found, similarity, result);
}
println!("Total {result}");
}
} }
fn main() { fn main() {
solve_part1() solve()
// solve_part2()
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>