Skip to content

Commit

Permalink
day5 part1
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Dec 8, 2024
1 parent c84dd5e commit f340e17
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
28 changes: 28 additions & 0 deletions inputs/2024/day5_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13

75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
5 changes: 5 additions & 0 deletions results/2024.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"day": 4,
"part1": "2591",
"part2": "1880"
},
{
"day": 5,
"part1": "6041",
"part2": ""
}
]
67 changes: 67 additions & 0 deletions src/aoc2024/day5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::aoc2024::Aoc2024;
use crate::traits::days::Day5;
use crate::traits::ParseInput;
use crate::traits::Solution;

impl ParseInput<Day5> for Aoc2024 {
type Parsed = Input;

fn parse_input(input: &str) -> Self::Parsed {
let mut is_in_pages = false;
let mut res = Input::default();

for line in input.lines() {
let line = line.trim();
if line.is_empty() {
is_in_pages = true;
continue;
}

if !is_in_pages {
let (left, right) = line.split_once("|").unwrap();
let left = left.parse().unwrap();
let right = right.parse().unwrap();
let pair = (left, right);
res.pairs.push(pair);
} else {
let page = line.split(',').map(|s| s.parse().unwrap()).collect();
res.pages.push(page);
}
}

res
}
}
#[derive(Debug, Default)]
pub struct Input {
pairs: Vec<(u32, u32)>,
pages: Vec<Vec<u32>>,
}

impl Solution<Day5> for Aoc2024 {
type Part1Output = u32;
type Part2Output = u32;

fn part1(input: &Input) -> u32 {
let mut res = 0;

'page: for page in &input.pages {
for (i, left) in page.iter().enumerate() {
for right in page[i + 1..].iter() {
let rev_pair = (*right, *left);
if input.pairs.contains(&rev_pair) {
continue 'page;
}
}
}

let mid = page.len() / 2 ;
res += page[mid];
}
res
}

fn part2(input: &Input) -> u32 {
todo!()
}
}
2 changes: 2 additions & 0 deletions src/aoc2024/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;

pub fn run_solution_for_day(day: u32, input: &str, results: Option<Results>) -> Option<TimingData> {
let r = results
Expand All @@ -18,6 +19,7 @@ pub fn run_solution_for_day(day: u32, input: &str, results: Option<Results>) ->
2 => run::<Aoc2024, Day2>(input, r),
3 => run::<Aoc2024, Day3>(input, r),
4 => run::<Aoc2024, Day4>(input, r),
5 => run::<Aoc2024, Day5>(input, r),
_ => return None,
};
Some(elapsed)
Expand Down

0 comments on commit f340e17

Please sign in to comment.