This commit is contained in:
Adam Jeniski 2022-09-19 19:10:56 -04:00
parent 9ad9239c08
commit 5424f4fcf3
5 changed files with 120 additions and 40 deletions

10
inputs/11.txt Normal file
View File

@ -0,0 +1,10 @@
4134384626
7114585257
1582536488
4865715538
5733423513
8532144181
1288614583
2248711141
6415871681
7881531438

View File

@ -1,7 +1,10 @@
#![allow(dead_code)]
fn parse_input(input: &str) -> Vec<u32> {
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 {

View File

@ -2,25 +2,14 @@
* File: day10.rs
* Author: Adam Jeniski; @Ajetski
*/
use std::collections::{HashMap};
use std::collections::HashMap;
type Stack = Vec<char>;
fn part_1_solution(input: &str) -> u64 {
let point_table = HashMap::<char, u64>::from([
(')', 3),
(']', 57),
('}', 1197),
('>', 25137)
]);
let point_table = HashMap::<char, u64>::from([(')', 3), (']', 57), ('}', 1197), ('>', 25137)]);
let match_table = HashMap::<char, char>::from([
(')', '('),
(']', '['),
('}', '{'),
('>', '<')
]);
let match_table = HashMap::<char, char>::from([(')', '('), (']', '['), ('}', '{'), ('>', '<')]);
let mut count = 0;
@ -43,31 +32,17 @@ fn part_1_solution(input: &str) -> u64 {
}
fn part_2_solution(input: &str) -> u64 {
let point_table = HashMap::<char, u64>::from([
(')', 1),
(']', 2),
('}', 3),
('>', 4)
]);
let point_table = HashMap::<char, u64>::from([(')', 1), (']', 2), ('}', 3), ('>', 4)]);
let match_table_from_start = HashMap::<char, char>::from([
('(', ')'),
('[', ']'),
('{', '}'),
('<', '>')
]);
let match_table_from_end = HashMap::<char, char>::from([
(')', '('),
(']', '['),
('}', '{'),
('>', '<')
]);
let match_table_from_start =
HashMap::<char, char>::from([('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]);
let match_table_from_end =
HashMap::<char, char>::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);
}

90
src/day15.rs Normal file
View File

@ -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<Ordering> {
Some(self.cmp(other))
}
}
fn part_one(input: Vec<Vec<u8>>) -> Result<u32, &'static str> {
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<Vec<u8>> {
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));
// }
}

View File

@ -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;