From da12b07502762afac94e71b25545cf585b47cefe Mon Sep 17 00:00:00 2001 From: Cassandra de la Cruz-Munoz Date: Wed, 6 Dec 2023 22:11:10 +0100 Subject: [PATCH] All caught up (days 3 and 4 parts 1 and 2) --- src/03part1.rs | 41 ++++++++ src/03part2.rs | 256 +++++++++++++++++++++++++++++++++++++++++++++++++ src/04part1.rs | 37 +++++++ src/04part2.rs | 47 +++++++++ 4 files changed, 381 insertions(+) create mode 100644 src/03part1.rs create mode 100644 src/03part2.rs create mode 100644 src/04part1.rs create mode 100644 src/04part2.rs diff --git a/src/03part1.rs b/src/03part1.rs new file mode 100644 index 0000000..148969e --- /dev/null +++ b/src/03part1.rs @@ -0,0 +1,41 @@ +use std::fs; + +fn main() { + let file_path = "input/03input.txt"; + let contents = fs::read_to_string(file_path).expect("should read file"); + let lines_count = contents.clone().lines().count(); + let mut lines = contents.lines(); + let first_line: Vec = lines.next().unwrap().chars().collect(); + let mut matrix: Vec> = Vec::new(); + matrix.push(first_line); + loop { + match lines.next() { + Some(line) => { + matrix.push(line.chars().collect()); + }, + None => break + } + } + let mut total_val = 0; + println!("{:?}", matrix); + for i in 0..lines_count { + let mut temp_value = 0; + let mut symbol = false; + for j in 0..matrix.get(i).unwrap().len() { + if matrix.get(i).unwrap().get(j).unwrap().is_ascii_digit() { + temp_value = temp_value * 10 + matrix.get(i).unwrap().get(j).unwrap().to_digit(10).unwrap(); + symbol = symbol || (i > 0 && ((j > 0 && matrix.get(i-1).unwrap().get(j-1).unwrap().is_ascii_punctuation() && matrix.get(i-1).unwrap().get(j-1).unwrap() != &'.') || (matrix.get(i-1).unwrap().get(j).unwrap().is_ascii_punctuation() && matrix.get(i-1).unwrap().get(j).unwrap() != &'.') || (j < matrix.get(i-1).unwrap().len()-1 && matrix.get(i-1).unwrap().get(j+1).unwrap().is_ascii_punctuation() && matrix.get(i-1).unwrap().get(j+1).unwrap() != &'.'))) || + ((j > 0 && matrix.get(i).unwrap().get(j-1).unwrap().is_ascii_punctuation() && matrix.get(i).unwrap().get(j-1).unwrap() != &'.') || (matrix.get(i).unwrap().get(j).unwrap().is_ascii_punctuation() && matrix.get(i).unwrap().get(j).unwrap() != &'.') || (j < matrix.get(i).unwrap().len()-1 && matrix.get(i).unwrap().get(j+1).unwrap().is_ascii_punctuation() && matrix.get(i).unwrap().get(j+1).unwrap() != &'.')) || + (i < matrix.len()-1 && ((j > 0 && matrix.get(i+1).unwrap().get(j-1).unwrap().is_ascii_punctuation() && matrix.get(i+1).unwrap().get(j-1).unwrap() != &'.') || (matrix.get(i+1).unwrap().get(j).unwrap().is_ascii_punctuation() && matrix.get(i+1).unwrap().get(j).unwrap() != &'.') || (j < matrix.get(i+1).unwrap().len()-1 && matrix.get(i+1).unwrap().get(j+1).unwrap().is_ascii_punctuation() && matrix.get(i+1).unwrap().get(j+1).unwrap() != &'.'))); + } else { + if symbol { + println!("{}", temp_value); + total_val += temp_value; + symbol = false; + } + temp_value = 0; + } + } + } + println!("{}", total_val); +} diff --git a/src/03part2.rs b/src/03part2.rs new file mode 100644 index 0000000..413c3d4 --- /dev/null +++ b/src/03part2.rs @@ -0,0 +1,256 @@ +use std::{fs, collections::HashMap}; + +fn main() { + let file_path = "input/03input.txt"; + let contents = fs::read_to_string(file_path).expect("should read file"); + let lines_count = contents.clone().lines().count(); + let mut lines = contents.lines(); + let first_line: Vec = lines.next().unwrap().chars().collect(); + let mut matrix: Vec> = Vec::new(); + matrix.push(first_line); + loop { + match lines.next() { + Some(line) => { + matrix.push(line.chars().collect()); + }, + None => break + } + } + let mut total_val = 0; + for i in 0..lines_count { + for j in 0..matrix.get(i).unwrap().len() { + let mut values = Vec::new(); + if matrix.get(i).unwrap().get(j).unwrap() == &'*' { + let mut locations = HashMap::new(); + for k in i-1..i+2 { + for l in j-1..j+2 { + if k < lines_count && l <= matrix.get(k).unwrap().len() && (k != i || l != j) { + locations.insert((k+2-i)*3+(l+2-j)-4, matrix.get(k).unwrap().get(l).unwrap().is_ascii_digit()); + } + } + } + println!("{} {} {:?}", i, j, locations); + /* + * 012 + * 3*5 + * 678 + */ + if i > 0 && j > 0 && *locations.get(&0).unwrap() { + if j < matrix.get(i-1).unwrap().len()-1 && *locations.get(&2).unwrap() && *locations.get(&1).unwrap() { + // 012 + let mut value = 0; + for k in 0..matrix.get(i-1).unwrap().len() { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j+1 { + value = 0; + } else { + break; + } + } + values.push(value); + } else if j < matrix.get(i-1).unwrap().len()-1 && *locations.get(&2).unwrap() && !*locations.get(&1).unwrap() { + // 0.2 + { + let mut value = 0; + for k in 0..j { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j { + value = 0; + } + } + values.push(value); + } + { + let mut value = 0; + for k in j+1..matrix.get(i-1).unwrap().len() { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + } else if *locations.get(&1).unwrap() { + // 01. + let mut value = 0; + for k in 0..j+1 { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j+1 { + value = 0; + } else { + break; + } + } + values.push(value); + } else { + // 0.. + let mut value = 0; + for k in 0..j { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j { + value = 0; + } else { + break; + } + } + values.push(value); + } + } else if i > 0 && *locations.get(&1).unwrap() { + if (j < matrix.get(i-1).unwrap().len()-1 && !*locations.get(&2).unwrap()) || j == matrix.get(i-1).unwrap().len()-1 { + // .1. + values.push(matrix.get(i-1).unwrap().get(j).unwrap().to_digit(10).unwrap()); + } else { + // .12 + let mut value = 0; + for k in j..matrix.get(i-1).unwrap().len() { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + } else if i < lines_count-1 && *locations.get(&2).unwrap() { + // ..2 + let mut value = 0; + for k in j+1..matrix.get(i-1).unwrap().len() { + if matrix.get(i-1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i-1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + if j > 0 && *locations.get(&3).unwrap() { + // 3* + let mut value = 0; + for k in 0..j { + if matrix.get(i).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j-1 { + value = 0; + } + } + values.push(value); + } + if j < matrix.get(i).unwrap().len()-1 && *locations.get(&5).unwrap() { + // *5 + let mut value = 0; + for k in j+1..matrix.get(i).unwrap().len() { + if matrix.get(i).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + if i < lines_count-1 && j > 0 && *locations.get(&6).unwrap() { + if j < matrix.get(i+1).unwrap().len()-1 && *locations.get(&8).unwrap() && *locations.get(&7).unwrap() { + // 678 + let mut value = 0; + for k in 0..matrix.get(i+1).unwrap().len() { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j+1 { + value = 0; + } else { + break; + } + } + values.push(value); + } else if j < matrix.get(i+1).unwrap().len()-1 && *locations.get(&8).unwrap() && !*locations.get(&7).unwrap() { + // 6.8 + { + let mut value = 0; + for k in 0..j { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j { + value = 0; + } + } + values.push(value); + } + { + let mut value = 0; + for k in j+1..matrix.get(i+1).unwrap().len() { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + } else if *locations.get(&7).unwrap() { + // 67. + let mut value = 0; + for k in 0..j+1 { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j+1 { + value = 0; + } else { + break; + } + } + values.push(value); + } else { + // 6.. + let mut value = 0; + for k in 0..j { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else if k < j { + value = 0; + } else { + break; + } + } + values.push(value); + } + } else if i < lines_count - 1 && *locations.get(&7).unwrap() { + if (j < matrix.get(i+1).unwrap().len()-1 && !*locations.get(&8).unwrap()) || j == matrix.get(i+1).unwrap().len()-1 { + // .7. + values.push(matrix.get(i+1).unwrap().get(j).unwrap().to_digit(10).unwrap()); + } else { + // .78 + let mut value = 0; + for k in j..matrix.get(i+1).unwrap().len() { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + } else if i < lines_count-1 && *locations.get(&8).unwrap() { + // ..8 + let mut value = 0; + for k in j+1..matrix.get(i+1).unwrap().len() { + if matrix.get(i+1).unwrap().get(k).unwrap().is_ascii_digit() { + value = value * 10 + matrix.get(i+1).unwrap().get(k).unwrap().to_digit(10).unwrap(); + } else { + break; + } + } + values.push(value); + } + println!("{:?}", values); + if values.len() == 2 { + total_val += values.into_iter().fold(1, |acc, x| acc * x); + } + } + } + } + println!("{}", total_val); +} diff --git a/src/04part1.rs b/src/04part1.rs new file mode 100644 index 0000000..13fa517 --- /dev/null +++ b/src/04part1.rs @@ -0,0 +1,37 @@ +use std::{fs, char::CharTryFromError}; + +fn main() { + let file_path = "input/04input.txt"; + let contents = fs::read_to_string(file_path).expect("should read file"); + let mut lines = contents.lines(); + let mut total_points = 0; + loop { + match lines.next() { + Some(line) => { + let mut points = 0; + let mut sub_line = line.get(line.find(":").unwrap()+1..line.len()).unwrap().split("|"); + let mut winning_nums: Vec = sub_line.next().unwrap().split_whitespace().map(|x| x.parse::().ok().unwrap()).collect(); + let mut chosen_nums: Vec = sub_line.next().unwrap().split_whitespace().map(|x| x.parse::().ok().unwrap()).collect(); + winning_nums.sort(); + chosen_nums.sort(); + let mut j = 0; + for i in winning_nums { + while j < chosen_nums.len() && chosen_nums[j] < i { + j += 1; + } + if j < chosen_nums.len() && i == chosen_nums[j] { + j += 1; + if points == 0 { + points = 1; + } else { + points *= 2; + } + } + } + total_points += points; + }, + None => break, + } + } + println!("{}", total_points); +} diff --git a/src/04part2.rs b/src/04part2.rs new file mode 100644 index 0000000..05ccd0c --- /dev/null +++ b/src/04part2.rs @@ -0,0 +1,47 @@ +use std::{fs, collections::HashMap}; + +fn main() { + let file_path = "input/04input.txt"; + let contents = fs::read_to_string(file_path).expect("should read file"); + let mut lines = contents.lines(); + let mut total_cards = 0; + let mut extra_cards = HashMap::new(); + let mut card_index = 0; + loop { + match lines.next() { + Some(line) => { + card_index += 1; + let mut points = 0; + let mut sub_line = line.get(line.find(":").unwrap()+1..line.len()).unwrap().split("|"); + let mut winning_nums: Vec = sub_line.next().unwrap().split_whitespace().map(|x| x.parse::().ok().unwrap()).collect(); + let mut chosen_nums: Vec = sub_line.next().unwrap().split_whitespace().map(|x| x.parse::().ok().unwrap()).collect(); + winning_nums.sort(); + chosen_nums.sort(); + let mut j = 0; + for i in winning_nums { + while j < chosen_nums.len() && chosen_nums[j] < i { + j += 1; + } + if j < chosen_nums.len() && i == chosen_nums[j] { + j += 1; + points += 1; + } + } + if extra_cards.get(&card_index).is_none() { + extra_cards.insert(card_index, 0); + } + println!("{}", points); + for i in 1..points+1 { + if extra_cards.get(&(card_index+i)).is_none() { + extra_cards.insert(card_index + i, 0); + } + extra_cards.insert(card_index + i, *extra_cards.get(&card_index).unwrap() + 1 + *extra_cards.get(&(card_index+i)).unwrap()); + } + total_cards += 1 + extra_cards.get(&card_index).unwrap(); + println!("{:?}", extra_cards); + }, + None => break + } + } + println!("{}", total_cards); +}