Skip to content

Commit

Permalink
feat: start day 1 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzHayden committed Dec 1, 2024
1 parent e72ce63 commit 9d234a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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
25 changes: 24 additions & 1 deletion src/bin/01.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
use regex::Regex;

advent_of_code::solution!(1);

pub fn part_one(input: &str) -> Option<u32> {
None
// 1. Parse the input into two lists using regex capturing groups
let mut total_diff: u32 = 0;
let mut left_locations: Vec<u32> = Vec::new();
let mut right_locations: Vec<u32> = Vec::new();
let re = Regex::new(r"(\d+)\s+(\d+)").unwrap();
for line in input.lines() {
let caps = re.captures(line).unwrap();
left_locations.push(caps[1].parse::<u32>().unwrap());
right_locations.push(caps[2].parse::<u32>().unwrap());
}

// 2. Sort the lists
left_locations.sort();
right_locations.sort();

// 3. Iterate through the lists and calculate the difference between the two corresponding elements
for (left, right) in left_locations.iter().zip(right_locations.iter()) {
total_diff += left - right;
}

// 4. Sum the differences and return the total difference
Some(total_diff)
}

pub fn part_two(input: &str) -> Option<u32> {
Expand Down

0 comments on commit 9d234a5

Please sign in to comment.