Solution for day5

This commit is contained in:
Savanni D'Gerinel 2024-12-06 23:09:41 -05:00
parent b2dcf20a27
commit e86d381098
5 changed files with 1763 additions and 0 deletions

1377
2024/data/day5.txt Normal file

File diff suppressed because it is too large Load Diff

130
2024/data/day6.txt Normal file
View File

@ -0,0 +1,130 @@
.........#...#................................#.........#............#.....#....#.....................................#...........
............................................................................................#...............#.......#.............
....#.......#......................................#.........................#..#....#....#.........#.............................
............##..#............#........................#........#........#.......#...........................#................#....
..................................#.............#....#..........................#................#..#....##........#..............
..................##.#...#....#...................##.....#.................#......##.....................##............#..........
..........#..................................#....................................................................................
......#...........#.......................................#........#........................#........#............................
..................#................................#........................#.............................#.......................
.............#............................................................................................##.................#....
........#..............................................................................................#..............#.#.......#.
..#...........#..............#..............#..#.....#.......#......#.......................#....#...................##..........#
..............##................................#..............##....................#...#........................................
......................#.................#.......#.......##.##....................................#................................
...........#...#.#..................#...#.....................................................................#...................
.....#....#...#..............................................................................................#.......#............
...............................#...........#.#.............#.........#.....................................#.#....................
........#...........#.......#............................#.....#.............................#.........................#..........
...................................................#...........#.....#..#....#.............#.....#..#..#.............#............
..............#.........#..#..........#.#.......................#...................................#.............................
..#.............#..#..#.#...........#...................................................#...........................#...........#.
........##....##..........................................................................#.......................................
.......................................................#.......#.............................#...................#........#.......
#.....#.#......#...............................................................#..........#........#..............................
.#.#............#..............#..........................#.#.....................................................#...............
.................#...#........#....#.....#...#...................................#....#.................#..........#...#.#...#....
...#..##........................#..#..................................................................................#.......#...
.................#...#.#..................................................#..#........#.................#...................#.....
.......................#.......#....................#......................................#...............................#......
...............................................................................................................................#..
...........##............................#....................#...................................................................
..#........................................#..#.............................#............................#........................
..#.#....#..................................#...................................................................................#.
.................#...#...............#..........#..............................#...#..............................#.#..#..........
............#..........#..........#...................................#.....#....................#................##.............#
....#...........#.......#......................................#........................................#........#......#...#.....
.............#.....#...................#....#.....................................................................................
.....................#.................................#.#...................#.......#.#...............#..........................
........#..............#.......#.............#...........................................................#........#..............#
............#............................................#.......##................#..............................................
...............................#..................................................................................................
#....................#......#.......................#..........#.........................#................#...............#..#...#
..................................................#.....#...........#...#..#......#...........................#...................
............#......................#.....#.........#....................................................................#....#....
.......#...................#...#...........................#.......#......#......................................#.....#..........
.................................................#.............#....................................#.............................
......#.................#.............................................................................#..##.......................
....#..............................................#..............#..........#.......#....#............................#..........
........#....#............#....#.................#........................................................#.....#.................
..#......................#.......................................................#................................................
.#.....................#..........................................................#.............................#.................
.............#..............#.#.....#.........................................#...............................#.........#.........
.............................................................................#............................#....#...#..............
#.#.#...................................................................#...........................................#.........#...
..................................................#.#............#..#...#..............#.....#....................................
......#..................................#............................##.........................................#................
..#....................................#............#.........#.........#...........#.....#..............................#........
......................................#.............#..................#.................#..........................##............
...............#.#........#.......................#..........#...............................#.............................#.#....
.................#....................................................................................#...........................
#...............................................#.#........#...#...................#............#.................#......#.#......
................#........................#......#.............................................#.#..............#....#.............
.......#..........#..........................#......#.............................................................................
....#.......#.................................................#..............#...........#.....#............#............#........
.....................#...................................#...............................................................#........
#..........................#..#...........................................#...............#............#............#.............
.............................#...................................#...#.......................................#......#.............
............#........#.................................................#.................#...................#....................
.........................................#................#.....................................#.................................
...........................................................................................#.....................#................
.....................#.........#..#.#......................................................#........#.............................
..#......#.........#.#...................................................#......................#.................................
.#...........#.........................................................................................#.....................#....
..........#....##.#..#..........#........#.#.................................#.....................#..............................
.....#........#.......#....#...........#...................#...............#..#....#..............................#..............#
.......#..............................#............#.................................................#........#................#..
...............................#..............................................#...................................................
#..............................................................#.........................................................#........
.......#....................................................#..............................#..................#..........##.......
.......................................#...............................................^.............#............................
..#...........#.........#............................#...................................#.#.......###.....#......................
........#............................................................................................................#............
...#...#...........#.................................................................#...#.......#........#.......................
...............#.....................................#..........#.......#.#.....#.#..................#............................
.....#..#..............#.#.......................#.................................................................#..............
#.....#.........................................#..##....#..........................#...................#..#......#........#......
...................##................#..#......................................#.....#............................................
....#...............................................................................................#...........................#.
....#..........#....#........................#....................................................................................
..................#.....................................................#.....................................#...................
...........................#......#.......................................................................#..............#........
...............#....................#...#...#.......#................#.......#.......#............................................
#.....##......#.............................................#...#.................................#.................#....#..##..#.
..........#.......................................#..................................#...#....#......................#............
......#................#.#........#....#......##.........#........................................#...............................
......#...#............#...#.........................................................#....#.....#..............#..................
.........................#...............#........#...#......#....................................................#..#.#..........
...##............#........#.#.........................#.............................#............#..............#.................
.......#.........#..............................#...................................#.......................#.#...................
#..........#.............#..............................................#.....................................................#...
........#..#...#.....................................#............................................................................
..................#...............................#..........................................#.............#..............#..#....
....#.#..#...#............................#..............#...#..........#......#....#.....................#..........#......#.....
................#..............#........................#......#..............#.......................##..........................
..........#.....#.............................................#.......#...........................................#...............
.........#....#........#............#...............#.............................................................................
...........#..#..........#..............#..........#......#.......................................................................
..........#....................#........................................#.....#..................#..#.#..#...#.....#..............
........#...........#...#..................##.........#......................#...#............#......................#............
...................................................#........................#.....#.............#.................................
........#................#..........................#.#...#......#........................................#.......................
.............#.........................................##....................#...................#.#..................#...........
..................#.............................................#...............................................................#.
.....#............#................#..............................#...##....#...#.................................................
#.......................###........#..............#.....#.............#...............#.............#......................#......
...........#.............................#........................#.........................................................#.....
............#.....#.....#...#....#........#...............................#.....................#..................#....#.........
...............#.#..................................#.................#.#......#...........#...#................#.................
.....#............................................#.....#...............#.....#.............#.......#..........#..................
.....................................#................................................#.........#..#..............................
.....................#...............................................#.#...........#........#.....................................
.............................................#....................#.............#......................#..........................
......................................#.........#.........................##........#................#...............#............
........................#.....#.....................................#....#........#......#..............#.......##...........#.#..
....................#........................................#.........#.........#......#....#............#...........#...........
...........##......#...........#................#.......##.....#........#.........#...........................................#...
...................#...#....#.............#..#......................................................#............#................
....#.........................#....#..................#..............#.............#...................................#..........
..................................#.......#.....................#.................................................................
.............................................#......#.......................................................#.....................

224
2024/src/day5.rs Normal file
View File

@ -0,0 +1,224 @@
use std::collections::{HashMap, HashSet};
use nom::{
bytes::complete::{tag, take_while},
character::{
complete::{digit1, line_ending},
is_digit,
},
multi::{many1, separated_list1},
sequence::terminated,
IResult,
};
const INPUT: &'static str = include_str!("../data/day5.txt");
pub fn day5a() -> String {
let (deps, manuals) = parse_input(INPUT);
format!("{}", part_a(deps, manuals))
}
pub fn day5b() -> String {
let (deps, manuals) = parse_input(INPUT);
format!("{}", part_b(deps, manuals))
}
fn part_a(deps: Dependencies, manuals: Vec<Manual>) -> i32 {
manuals
.into_iter()
.filter(|manual| is_safe(&deps, manual))
.map(|manual| middle_page(&manual))
.fold(0, |acc, page_num| acc + page_num)
}
fn part_b(deps: Dependencies, manuals: Vec<Manual>) -> i32 {
println!("{:?}", deps);
manuals
.into_iter()
.filter(|manual| !is_safe(&deps, manual))
.map(|manual| fix_manual(&deps, &manual))
.map(|manual| middle_page(&manual))
.fold(0, |acc, page_num| acc + page_num)
}
fn fix_manual(deps: &Dependencies, manual: &Manual) -> Manual {
let pages_present: HashSet<i32> = manual.iter().cloned().collect();
let mut result = vec![];
for page in pages_present.iter() {
handle(*page, &mut result, &pages_present, deps);
}
println!("{:?}", result);
result
}
fn handle(page: i32, result: &mut Vec<i32>, present: &HashSet<i32>, deps: &Dependencies) {
let empty_deps: HashSet<i32> = HashSet::new();
for page_dep in deps.get(&page).unwrap_or(&empty_deps) {
if present.contains(page_dep) && !result.contains(page_dep) {
handle(*page_dep, result, present, deps);
}
}
if !result.contains(&page) {
result.push(page);
}
}
fn middle_page(manual: &Manual) -> i32 {
let center_idx = manual.len() / 2;
manual[center_idx]
}
fn is_safe(deps: &Dependencies, manual: &Manual) -> bool {
let values_present: HashSet<i32> = manual.iter().cloned().collect();
let mut values_counted: HashSet<i32> = HashSet::new();
let empty_deps: HashSet<i32> = HashSet::new();
for page in manual.iter() {
for page_dep in deps.get(page).unwrap_or(&empty_deps).iter() {
if values_present.contains(page_dep) {
if !values_counted.contains(page_dep) {
return false;
}
}
}
values_counted.insert(*page);
}
true
}
type Dependencies = HashMap<i32, HashSet<i32>>;
type Manual = Vec<i32>;
fn parse_input(input: &str) -> (Dependencies, Vec<Manual>) {
let (_, data) = parse_input_(input).unwrap();
data
}
fn parse_input_(input: &str) -> IResult<&str, (Dependencies, Vec<Manual>)> {
let (input, dep_list) = many1(parse_dependency)(input)?;
let (input, _) = line_ending(input)?;
let (input, manuals) = many1(terminated(parse_manual, line_ending))(input)?;
let mut deps = HashMap::new();
for (predecessor, successor) in dep_list.into_iter() {
deps.entry(successor)
.and_modify(|preds: &mut HashSet<i32>| {
preds.insert(predecessor);
})
.or_insert(vec![predecessor].into_iter().collect::<HashSet<i32>>());
}
Ok((input, (deps, manuals)))
}
fn parse_dependency(input: &str) -> IResult<&str, (i32, i32)> {
let (input, pred) = parse_page_number(input)?;
let (input, _) = tag("|")(input)?;
let (input, succ) = parse_page_number(input)?;
let (input, _) = line_ending(input)?;
Ok((input, (pred, succ)))
}
fn parse_manual(input: &str) -> IResult<&str, Manual> {
separated_list1(tag(","), parse_page_number)(input)
}
fn parse_page_number(input: &str) -> IResult<&str, i32> {
let (input, digits) = digit1(input)?;
let page_num = digits.parse::<i32>().unwrap();
Ok((input, page_num))
}
#[cfg(test)]
mod test {
use super::*;
const TEST_DATA: &'static str = "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
";
#[test]
fn it_solves_part_a() {
let (deps, manuals) = parse_input(TEST_DATA);
assert_eq!(part_a(deps, manuals), 143);
let (deps, manuals) = parse_input(INPUT);
assert_eq!(part_a(deps, manuals), 6242);
}
#[test]
fn it_solves_part_b() {
let (deps, manuals) = parse_input(TEST_DATA);
assert_eq!(part_b(deps, manuals), 123);
let (deps, manuals) = parse_input(INPUT);
assert_eq!(part_b(deps, manuals), 5169);
}
/*
#[test]
fn it_finds_correct_manuals() {
let (deps, manuals) = parse_input(TEST_DATA);
let safe = safe_manuals(deps, manuals);
assert_eq!(safe.len(), 3);
assert_eq!(safe[0], vec![75, 47, 61, 53, 29]);
assert_eq!(safe[1], vec![97, 61, 53, 29, 13]);
assert_eq!(safe[2], vec![75, 29, 13]);
}
*/
#[test]
fn it_checks_manuals() {
let (deps, manuals) = parse_input(TEST_DATA);
assert!(is_safe(&deps, &manuals[0]));
assert!(is_safe(&deps, &manuals[1]));
assert!(is_safe(&deps, &manuals[2]));
assert!(!is_safe(&deps, &manuals[3]));
assert!(!is_safe(&deps, &manuals[4]));
assert!(!is_safe(&deps, &manuals[5]));
}
#[test]
fn it_finds_center_values() {
let (deps, manuals) = parse_input(TEST_DATA);
assert_eq!(middle_page(&manuals[0]), 61);
assert_eq!(middle_page(&manuals[1]), 53);
assert_eq!(middle_page(&manuals[2]), 29);
}
#[test]
fn it_fixes_manuals() {
let (deps, manuals) = parse_input(TEST_DATA);
assert_eq!(fix_manual(&deps, &manuals[3]), vec![97, 75, 47, 61, 53]);
assert_eq!(fix_manual(&deps, &manuals[4]), vec![61, 29, 13]);
assert_eq!(fix_manual(&deps, &manuals[5]), vec![97, 75, 47, 29, 13]);
}
}

26
2024/src/day6.rs Normal file
View File

@ -0,0 +1,26 @@
const INPUT: &'static str = include_str!("../data/day6.txt");
pub fn day6a() -> String {
unimplemented!();
}
pub fn day6b() -> String {
unimplemented!();
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &'static str = "....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
";
}

View File

@ -2,6 +2,8 @@ mod day1;
mod day2;
mod day3;
mod day4;
mod day5;
mod day6;
fn main() {
let day = std::env::args().skip(1).next();
@ -15,6 +17,10 @@ fn main() {
Some("3b") => day3::day3b(),
Some("4a") => day4::day4a(),
Some("4b") => day4::day4b(),
Some("5a") => day5::day5a(),
Some("5b") => day5::day5b(),
Some("6a") => day6::day6a(),
Some("6b") => day6::day6b(),
_ => panic!("unrecognized day"),
};