diff --git a/inputs/11.txt b/inputs/11.txt new file mode 100644 index 0000000..1a0fab9 --- /dev/null +++ b/inputs/11.txt @@ -0,0 +1,10 @@ +4134384626 +7114585257 +1582536488 +4865715538 +5733423513 +8532144181 +1288614583 +2248711141 +6415871681 +7881531438 diff --git a/src/day1.rs b/src/day1.rs index f24df0f..f18d86f 100755 --- a/src/day1.rs +++ b/src/day1.rs @@ -1,7 +1,10 @@ #![allow(dead_code)] fn parse_input(input: &str) -> Vec { - input.split_ascii_whitespace().map(|x| x.parse().unwrap() ).collect() + input + .split_ascii_whitespace() + .map(|x| x.parse().unwrap()) + .collect() } fn part_1_solution(input: &str) -> u64 { diff --git a/src/day10.rs b/src/day10.rs index 1da2823..bd948e8 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -2,26 +2,15 @@ * File: day10.rs * Author: Adam Jeniski; @Ajetski */ - -use std::collections::{HashMap}; +use std::collections::HashMap; type Stack = Vec; fn part_1_solution(input: &str) -> u64 { - let point_table = HashMap::::from([ - (')', 3), - (']', 57), - ('}', 1197), - ('>', 25137) - ]); + let point_table = HashMap::::from([(')', 3), (']', 57), ('}', 1197), ('>', 25137)]); + + let match_table = HashMap::::from([(')', '('), (']', '['), ('}', '{'), ('>', '<')]); - let match_table = HashMap::::from([ - (')', '('), - (']', '['), - ('}', '{'), - ('>', '<') - ]); - let mut count = 0; for line in input.split_ascii_whitespace() { @@ -43,31 +32,17 @@ fn part_1_solution(input: &str) -> u64 { } fn part_2_solution(input: &str) -> u64 { - let point_table = HashMap::::from([ - (')', 1), - (']', 2), - ('}', 3), - ('>', 4) - ]); + let point_table = HashMap::::from([(')', 1), (']', 2), ('}', 3), ('>', 4)]); - let match_table_from_start = HashMap::::from([ - ('(', ')'), - ('[', ']'), - ('{', '}'), - ('<', '>') - ]); + let match_table_from_start = + HashMap::::from([('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]); + + let match_table_from_end = + HashMap::::from([(')', '('), (']', '['), ('}', '{'), ('>', '<')]); - let match_table_from_end = HashMap::::from([ - (')', '('), - (']', '['), - ('}', '{'), - ('>', '<') - ]); - - let mut counts = vec![]; - 'outer:for line in input.split_ascii_whitespace() { + 'outer: for line in input.split_ascii_whitespace() { let mut stack = Stack::new(); for c in line.chars() { if "([{<".contains(c) { @@ -84,7 +59,9 @@ fn part_2_solution(input: &str) -> u64 { while !stack.is_empty() { let c = stack.pop().unwrap(); count *= 5; - count += point_table.get(match_table_from_start.get(&c).unwrap()).unwrap(); + count += point_table + .get(match_table_from_start.get(&c).unwrap()) + .unwrap(); } counts.push(count); } diff --git a/src/day15.rs b/src/day15.rs new file mode 100644 index 0000000..c536a54 --- /dev/null +++ b/src/day15.rs @@ -0,0 +1,90 @@ +use std::{cmp::Ordering, vec}; + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +struct Path { + x: usize, + y: usize, + length: u32, +} +impl From<&Path> for i32 { + fn from(val: &Path) -> Self { + val.x as i32 + val.y as i32 - val.length as i32 + } +} +impl Ord for Path { + fn cmp(&self, other: &Self) -> Ordering { + i32::from(self).cmp(&i32::from(other)) + } +} +impl PartialOrd for Path { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +fn part_one(input: Vec>) -> Result { + let height = input.len(); + let width = input[0].len(); + let mut dp = vec![vec![0u32; width]; height]; + for row in 0..height { + for col in 0..width { + let mut min = u32::MAX; + if row > 0 { + min = std::cmp::min(min, dp[row - 1][col] as u32); + } + if col > 0 { + min = std::cmp::min(min, dp[row][col - 1] as u32); + } + if min == u32::MAX { + min = 0; + } + println!("{} {} {}", row, col, min); + dp[row][col] += input[row][col] as u32 + min; + } + } + for row in 0..height { + for col in 0..width { + let mut min = u32::MAX; + if row + 1 < height { + min = std::cmp::min(min, input[row + 1][col] as u32); + } + if col + 1 < width { + min = std::cmp::min(min, input[row][col + 1] as u32); + } + if min == u32::MAX { + min = 0; + } + dp[row][col] = std::cmp::min(dp[row][col], dp[row][col] + input[row][col] as u32 + min); + } + } + Ok(dp[height - 1][width - 1] - 1) +} + +#[cfg(test)] +mod tests { + + use super::*; + + fn parse_input(input: &str) -> Vec> { + input + .split_ascii_whitespace() + .map(|s| s.chars().map(|c| c.to_digit(10).unwrap() as u8).collect()) + .collect() + } + + #[test] + fn part_one_sample_test() { + let input = parse_input(include_str!("../inputs/15_test.txt")); + let res = part_one(input); + println!("{:?}", res); + assert!(res == Ok(40)); + } + + // #[test] + // fn part_one_test() { + // let input = parse_input(include_str!("../inputs/15.txt")); + // let res = part_one(input); + // println!("{:?}", res); + // assert!(res == Ok(40)); + // } +} diff --git a/src/lib.rs b/src/lib.rs index bfa4f35..9882163 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ pub(crate) mod day1; pub(crate) mod day10; pub(crate) mod day9; -//pub(crate) mod day11; //todo, finish day11 +pub(crate) mod day11; pub(crate) mod day13; pub(crate) mod day14; -pub(crate) mod day15; +//pub(crate) mod day15;