Adds Day 3 part 1.
This commit is contained in:
13
src/main.rs
13
src/main.rs
@@ -2,14 +2,15 @@ pub mod input;
|
||||
|
||||
pub mod one;
|
||||
pub mod two;
|
||||
pub mod three;
|
||||
|
||||
use input::AdventError;
|
||||
use one::{one_one, one_two};
|
||||
use two::{two_one, two_two};
|
||||
|
||||
fn main() -> Result<(), AdventError> {
|
||||
//one_one()?;
|
||||
//one_two()?;
|
||||
two_one()?;
|
||||
Ok(two_two()?)
|
||||
one::one()?;
|
||||
one::two()?;
|
||||
two::one()?;
|
||||
two::two()?;
|
||||
three::one()?;
|
||||
Ok(three::two()?)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ fn process(lines: Vec::<String>) -> Result<u32, AdventError> {
|
||||
Ok(calibrations.iter().sum::<u32>())
|
||||
}
|
||||
|
||||
pub fn one_one() -> Result<(), AdventError> {
|
||||
pub fn one() -> Result<(), AdventError> {
|
||||
let lines: Vec<String> = read_into_vec::<String>(Path::new("resources/input1.txt"))?;
|
||||
Ok(println!("{}", process(lines)?))
|
||||
}
|
||||
@@ -35,7 +35,7 @@ pub fn one_one() -> Result<(), AdventError> {
|
||||
struct Mutation(usize, char);
|
||||
struct Pattern(&'static str, char);
|
||||
|
||||
pub fn one_two() -> Result<(), AdventError> {
|
||||
pub fn two() -> Result<(), AdventError> {
|
||||
let mut lines = read_into_vec(Path::new("resources/input1.txt"))?;
|
||||
|
||||
let mut mutations = Vec::<Mutation>::new();
|
||||
|
||||
138
src/three/mod.rs
Normal file
138
src/three/mod.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
use std::{str::FromStr, path::Path, option::Option};
|
||||
|
||||
use crate::input::{AdventError, read_into_vec};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Number {
|
||||
value: u32,
|
||||
position: u8,
|
||||
length: u8
|
||||
}
|
||||
|
||||
impl FromStr for RowInfo {
|
||||
type Err = AdventError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut row_info = RowInfo::new();
|
||||
for (i, b) in s.as_bytes().into_iter().enumerate() {
|
||||
row_info.process(i, b)?;
|
||||
}
|
||||
Ok(row_info.finish(s.len())?.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct RowInfo {
|
||||
partial_start: Option<u8>,
|
||||
partial_number: Option<String>,
|
||||
pub symbol_positions: Vec<u8>,
|
||||
pub numbers: Vec<Number>,
|
||||
}
|
||||
|
||||
impl RowInfo {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
partial_start: None,
|
||||
partial_number: None,
|
||||
symbol_positions: Vec::<u8>::new(),
|
||||
numbers: Vec::<Number>::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn add_symbol(&mut self, i: usize) -> Result<(), AdventError> {
|
||||
self.symbol_positions.push(i as u8);
|
||||
if self.partial_start != None {
|
||||
self.finish_digit(i)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish_digit(&mut self, i: usize) -> Result<(), AdventError> {
|
||||
let number = Number {
|
||||
length: i as u8 - self.partial_start.ok_or(AdventError("Expected a partial to be started.".into()))?,
|
||||
position: self.partial_start.ok_or(AdventError("Expected a partial to be started.".into()))?,
|
||||
value: self.partial_number.clone().ok_or(AdventError("Expected partial to be started and have valid value.".into()))?.trim().parse()?
|
||||
};
|
||||
self.numbers.push(number);
|
||||
self.partial_start = None;
|
||||
self.partial_number = None;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process(&mut self, i: usize, b: &u8) -> Result<(), AdventError> {
|
||||
if b != &b'.' && !b.is_ascii_digit() {
|
||||
self.add_symbol(i)?
|
||||
}
|
||||
else if b.is_ascii_digit() {
|
||||
if self.partial_start.is_none() {
|
||||
self.partial_start = Some(i as u8);
|
||||
self.partial_number = Some((*b as char).to_string())
|
||||
} else {
|
||||
self.partial_number = Some(format!("{}{}", self.partial_number.as_ref().unwrap(), *b as char));
|
||||
}
|
||||
} else {
|
||||
if self.partial_start != None {
|
||||
self.finish_digit(i)?
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish(&mut self, i: usize) -> Result<&Self, AdventError> {
|
||||
if self.partial_start != None {
|
||||
self.finish_digit(i)?;
|
||||
}
|
||||
return Ok(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RowInfo {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_coords(y: usize, number: &Number) -> Vec<(u8, u8)> {
|
||||
let mut vec = Vec::<(u8, u8)>::new();
|
||||
let min_x = if number.position == 0 {
|
||||
0
|
||||
} else {
|
||||
number.position - 1
|
||||
};
|
||||
let max_x = number.position + number.length + 1;
|
||||
for x in min_x..max_x {
|
||||
if y != 0 {
|
||||
vec.push((x, y as u8 - 1));
|
||||
}
|
||||
if x < number.position || x >= number.position + number.length {
|
||||
vec.push((x, y as u8));
|
||||
}
|
||||
vec.push((x, y as u8 + 1));
|
||||
}
|
||||
vec
|
||||
}
|
||||
|
||||
pub fn one() -> Result<(), AdventError> {
|
||||
let rows = read_into_vec::<RowInfo>(Path::new("resources/input3.txt"))?;
|
||||
let mut sum: u32 = 0;
|
||||
for (i, row) in rows.iter().enumerate() {
|
||||
for number in row.numbers.iter() {
|
||||
let coords = gen_coords(i, number);
|
||||
if coords.iter().any(|c| -> bool {
|
||||
if c.1 as usize >= rows.len() {
|
||||
false
|
||||
} else {
|
||||
rows[c.1 as usize].symbol_positions.binary_search(&c.0).is_ok()
|
||||
}
|
||||
}) {
|
||||
sum += number.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{}", sum);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn two() -> Result<(), AdventError> {
|
||||
Ok(())
|
||||
}
|
||||
@@ -78,7 +78,7 @@ struct Game {
|
||||
}
|
||||
|
||||
|
||||
pub fn two_one() -> Result<(), AdventError> {
|
||||
pub fn one() -> Result<(), AdventError> {
|
||||
let games: Vec<Game> = read_into_vec(Path::new("resources/input2.txt"))?;
|
||||
let mut sum: u32 = 0;
|
||||
|
||||
@@ -103,7 +103,7 @@ pub fn two_one() -> Result<(), AdventError> {
|
||||
Ok(println!("{}", sum))
|
||||
}
|
||||
|
||||
pub fn two_two() -> Result<(), AdventError> {
|
||||
pub fn two() -> Result<(), AdventError> {
|
||||
let games: Vec<Game> = read_into_vec(Path::new("resources/input2.txt"))?;
|
||||
let mut power: u32 = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user