Adds Days 4 and 5.1
This commit is contained in:
163
src/days/day4.rs
163
src/days/day4.rs
@@ -1,47 +1,176 @@
|
||||
use std::path::Path;
|
||||
|
||||
use crate::input::read_into_vec;
|
||||
|
||||
use super::*;
|
||||
|
||||
use advent_derive::advent_day;
|
||||
use regex::Regex;
|
||||
|
||||
struct Day4Part1 {
|
||||
|
||||
text: Vec<String>
|
||||
}
|
||||
|
||||
impl Day4Part1 {
|
||||
fn new() -> Self {
|
||||
Day4Part1{}
|
||||
Day4Part1{
|
||||
text: vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Day4Part2 {
|
||||
|
||||
text: Vec<String>,
|
||||
grid_size: usize
|
||||
}
|
||||
|
||||
impl Day4Part2 {
|
||||
fn new() -> Self {
|
||||
Day4Part2{}
|
||||
Day4Part2{
|
||||
text: vec![],
|
||||
grid_size: 0usize
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AdventDayPart1 for Day4Part1 {
|
||||
fn read_input(&mut self) -> Result<(),AdventError> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn solve(&mut self) -> Result<u64, AdventError> {
|
||||
todo!()
|
||||
}
|
||||
fn read_input_part1(part1: &mut Day4Part1, path: &Path) -> Result<(), AdventError> {
|
||||
part1.text = read_into_vec(path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl AdventDayPart2 for Day4Part2 {
|
||||
fn read_input(&mut self) -> Result<(),AdventError> {
|
||||
todo!()
|
||||
fn solve_part1(part1: &mut Day4Part1) -> Result<u64, AdventError> {
|
||||
let lr = part1.text.clone();
|
||||
let mut ud = vec![];
|
||||
let grid_size = part1.text.len();
|
||||
|
||||
for i in 0usize .. grid_size {
|
||||
ud.push(part1.text.iter().map(|s| {
|
||||
s.chars().nth(i).unwrap()
|
||||
}).collect::<String>());
|
||||
}
|
||||
|
||||
fn solve(&mut self) -> Result<u64, AdventError> {
|
||||
todo!()
|
||||
let mut lurd = vec![];
|
||||
for i in 0usize .. grid_size {
|
||||
let mut j = i;
|
||||
|
||||
lurd.push(part1.text.iter().map(|s| {
|
||||
let c;
|
||||
if j >= grid_size {
|
||||
c = ' ';
|
||||
} else {
|
||||
c = s.chars().nth(j).unwrap();
|
||||
}
|
||||
j += 1;
|
||||
c
|
||||
}).collect::<String>());
|
||||
}
|
||||
for i in 1usize .. grid_size {
|
||||
let mut j = i;
|
||||
|
||||
lurd.push(ud.iter().map(|s| {
|
||||
let c;
|
||||
if j >= grid_size {
|
||||
c = ' ';
|
||||
} else {
|
||||
c = s.chars().nth(j).unwrap();
|
||||
}
|
||||
j += 1;
|
||||
c
|
||||
}).collect::<String>());
|
||||
}
|
||||
|
||||
let mut ruld = vec![];
|
||||
for i in 0usize .. grid_size {
|
||||
let mut j = i;
|
||||
|
||||
ruld.push(lr.iter().map(|s| {
|
||||
let c;
|
||||
if j >= grid_size {
|
||||
c = ' ';
|
||||
} else {
|
||||
c = s.chars().rev().nth(j).unwrap();
|
||||
}
|
||||
j += 1;
|
||||
c
|
||||
}).collect::<String>());
|
||||
}
|
||||
for i in 1usize .. grid_size {
|
||||
let mut j = 0;
|
||||
ruld.push(lr.iter().skip(i).map(|s| {
|
||||
let c;
|
||||
if j >= grid_size {
|
||||
c = ' ';
|
||||
} else {
|
||||
c = s.chars().rev().nth(j).unwrap();
|
||||
}
|
||||
j += 1;
|
||||
c
|
||||
}).collect::<String>());
|
||||
}
|
||||
|
||||
let xmas_r = Regex::new("XMAS")?;
|
||||
let samx_r = Regex::new("SAMX")?;
|
||||
|
||||
// println!("input:\n{:?}", part1.text);
|
||||
// println!("lr:\n{:?}", lr);
|
||||
// println!("up:\n{:?}", ud);
|
||||
// println!("lurd:\n{:?}", lurd);
|
||||
// println!("ruld:\n{:?}", ruld);
|
||||
|
||||
Ok(
|
||||
lr.iter().chain(ud.iter()).chain(lurd.iter()).chain(ruld.iter()).fold(Ok(0), |sum, s| -> Result<u64, AdventError> {
|
||||
let mut sum = sum?;
|
||||
sum += xmas_r.find_iter(s.as_str()).count() as u64;
|
||||
sum += samx_r.find_iter(s.as_str()).count() as u64;
|
||||
Ok(sum)
|
||||
})?
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
fn read_input_part2(part2: &mut Day4Part2, path: &Path) -> Result<(), AdventError> {
|
||||
part2.text = read_into_vec(path)?;
|
||||
part2.grid_size = part2.text.len();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_m(part2: &mut Day4Part2, (x, y): (usize, usize)) -> bool {
|
||||
part2.text.iter().skip(y).next().unwrap().chars().nth(x).unwrap() == 'M'
|
||||
}
|
||||
|
||||
fn is_s(part2: &mut Day4Part2, (x, y): (usize, usize)) -> bool {
|
||||
part2.text.iter().skip(y).next().unwrap().chars().nth(x).unwrap() == 'S'
|
||||
}
|
||||
|
||||
fn is_xmas(part2: &mut Day4Part2, (x, y): (usize, usize)) -> bool {
|
||||
if x == 0 || x == part2.grid_size - 1 || y == 0 || y == part2.grid_size - 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let lr_mas = is_m(part2, (x-1, y-1)) && is_s(part2, (x+1, y+1)) || is_m(part2, (x+1, y+1)) && is_s(part2, (x-1, y-1));
|
||||
let rl_mas = is_m(part2, (x+1, y-1)) && is_s(part2, (x-1, y+1)) || is_m(part2, (x-1, y+1)) && is_s(part2, (x+1, y-1));
|
||||
|
||||
lr_mas && rl_mas
|
||||
}
|
||||
|
||||
|
||||
fn solve_part2(part2: &mut Day4Part2) -> Result<u64, AdventError> {
|
||||
let text = part2.text.clone();
|
||||
Ok(
|
||||
text.iter().enumerate().fold(0, |acc, (y, s)| {
|
||||
acc + s.chars().enumerate().fold(0, |acc, (x, c)| {
|
||||
if c == 'A' && is_xmas(part2, (x, y)) {
|
||||
acc + 1
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[advent_day(Day4Part1, Day4Part2)]
|
||||
struct Day4;
|
||||
|
||||
|
||||
93
src/days/day5.rs
Normal file
93
src/days/day5.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use std::path::Path;
|
||||
|
||||
use advent_derive::advent_day;
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::input::get_lines;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Order {
|
||||
left: u32,
|
||||
right: u32,
|
||||
}
|
||||
|
||||
struct Day5Part1 {
|
||||
ordering: Vec<Order>,
|
||||
queues: Vec<Vec<u32>>
|
||||
}
|
||||
|
||||
impl Day5Part1 {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
ordering: vec![],
|
||||
queues: vec![vec![]]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Day5Part2 {}
|
||||
|
||||
impl Day5Part2 {
|
||||
fn new() -> Self {
|
||||
Self{}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input_part1(part1: &mut Day5Part1, path: &Path) -> Result<(), AdventError> {
|
||||
let mut lines = get_lines(path);
|
||||
let order_regex = Regex::new(r"(\d+)\|(\d+)").unwrap();
|
||||
part1.ordering = lines.by_ref().take_while(|s| {
|
||||
s.as_ref().unwrap().trim() != ""
|
||||
}).map(|s| -> Order {
|
||||
let s = s.as_ref().unwrap().as_str();
|
||||
let (_, [l, r]) = order_regex.captures(s).unwrap().extract();
|
||||
Order {
|
||||
left: l.parse::<u32>().unwrap(),
|
||||
right: r.parse::<u32>().unwrap()
|
||||
}
|
||||
}).collect::<Vec<Order>>();
|
||||
part1.queues = lines.map(|s| {
|
||||
let s = s.as_ref().unwrap();
|
||||
let print_item_regex = Regex::new(r"\d+").unwrap();
|
||||
print_item_regex.find_iter(s.as_str()).map(|m| {
|
||||
m.as_str().parse::<u32>().unwrap()
|
||||
}).collect::<Vec<u32>>()
|
||||
}).collect::<Vec<Vec<u32>>>();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_correct(part1: &mut Day5Part1, q: Vec<u32>) -> bool {
|
||||
q.iter().enumerate().all(|(index, item)| {
|
||||
part1.ordering.iter().filter(|order| {
|
||||
order.left == *item
|
||||
}).all(|order| {
|
||||
index < q.iter().find_position(|x| { **x == order.right } ).or(Some((usize::MAX, &0))).unwrap().0
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn solve_part1(part1: &mut Day5Part1) -> Result<u64, AdventError> {
|
||||
Ok(part1.queues.clone().into_iter().filter(
|
||||
|q| {
|
||||
is_correct(part1, q.clone())
|
||||
}
|
||||
).fold(0u64, | acc, item| {
|
||||
acc + item[item.len() / 2] as u64
|
||||
}))
|
||||
}
|
||||
|
||||
fn read_input_part2(part2: &mut Day5Part2, path: &Path) -> Result<(), AdventError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn solve_part2(part2: &mut Day5Part2) -> Result<u64, AdventError> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
|
||||
#[advent_day(Day5Part1, Day5Part2)]
|
||||
struct Day5;
|
||||
@@ -18,4 +18,5 @@ pub trait AdventDay {
|
||||
pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
pub mod day4;
|
||||
pub mod day4;
|
||||
pub mod day5;
|
||||
@@ -25,4 +25,10 @@ impl From<TryFromIntError> for AdventError {
|
||||
fn from(e: TryFromIntError) -> Self {
|
||||
AdventError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<regex::Error> for AdventError {
|
||||
fn from(e: regex::Error) -> Self {
|
||||
AdventError(e.to_string())
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use day1::Day1;
|
||||
use day2::Day2;
|
||||
use day3::Day3;
|
||||
use day4::Day4;
|
||||
use day5::Day5;
|
||||
|
||||
use crate::error::AdventError;
|
||||
use crate::days::*;
|
||||
@@ -19,6 +20,7 @@ fn main() -> Result<(), AdventError> {
|
||||
Box::new(Day2::new()),
|
||||
Box::new(Day3::new()),
|
||||
Box::new(Day4::new()),
|
||||
Box::new(Day5::new())
|
||||
);
|
||||
|
||||
if env::args().len() == 1 {
|
||||
|
||||
Reference in New Issue
Block a user