Initial commit.
This commit is contained in:
commit
2524727f26
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
.vscode
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "advent_2023"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "advent_2023"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1000
resources/input1.txt
Normal file
1000
resources/input1.txt
Normal file
File diff suppressed because it is too large
Load Diff
16
src/input/mod.rs
Normal file
16
src/input/mod.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use std::{path::Path, io::{BufReader, BufRead, self}, fs::File};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AdventError(String);
|
||||
|
||||
impl From<io::Error> for AdventError {
|
||||
fn from(e: io::Error) -> Self {
|
||||
AdventError(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_into_vec(path: &Path) -> Result<Vec<String>, AdventError> {
|
||||
let file = File::open(path).expect("no such file");
|
||||
let buf = BufReader::new(file);
|
||||
buf.lines().map(|line| line.map_err(Into::into)).collect::<Result<Vec<String>, AdventError>>()
|
||||
}
|
12
src/main.rs
Normal file
12
src/main.rs
Normal file
@ -0,0 +1,12 @@
|
||||
pub mod input;
|
||||
|
||||
pub mod one;
|
||||
|
||||
use input::AdventError;
|
||||
use one::one_one;
|
||||
use one::one_two;
|
||||
|
||||
fn main() -> Result<(), AdventError> {
|
||||
one_one()?;
|
||||
Ok(one_two()?)
|
||||
}
|
86
src/one/mod.rs
Normal file
86
src/one/mod.rs
Normal file
@ -0,0 +1,86 @@
|
||||
|
||||
use std::{path::Path, vec};
|
||||
|
||||
use crate::input::AdventError;
|
||||
|
||||
use super::input::read_into_vec;
|
||||
|
||||
pub fn process(lines: Vec::<String>) -> Result<u32, AdventError> {
|
||||
let calibrations: Vec<u32> = lines.iter().map(|v| {
|
||||
let first_digit = v.as_bytes()
|
||||
.iter()
|
||||
.skip_while(|i| {
|
||||
**i < 48u8 || **i > 57u8
|
||||
})
|
||||
.take(1).collect::<Vec::<&u8>>()[0];
|
||||
|
||||
let last_digit = v.as_bytes()
|
||||
.iter()
|
||||
.rev()
|
||||
.skip_while(|i| { **i < 48u8 || **i > 57u8 })
|
||||
.take(1).collect::<Vec::<&u8>>()[0];
|
||||
|
||||
(*first_digit - 48) as u32 * 10 + (*last_digit - 48) as u32
|
||||
|
||||
}).collect();
|
||||
|
||||
Ok(calibrations.iter().sum::<u32>())
|
||||
}
|
||||
|
||||
pub fn one_one() -> Result<(), AdventError> {
|
||||
let lines = read_into_vec(Path::new("resources/input1.txt"))?;
|
||||
println!("{}", process(lines)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Mutation(usize, char);
|
||||
struct Pattern(String, char);
|
||||
|
||||
pub fn one_two() -> Result<(), AdventError> {
|
||||
let lines = read_into_vec(Path::new("resources/input1.txt"))?;
|
||||
|
||||
let new_lines: Vec<String> = lines.iter().map(|str| {
|
||||
let patterns = vec![
|
||||
Pattern("one".to_string(), '1'),
|
||||
Pattern("two".to_string(), '2'),
|
||||
Pattern("three".to_string(), '3'),
|
||||
Pattern("four".to_string(), '4'),
|
||||
Pattern("five".to_string(), '5'),
|
||||
Pattern("six".to_string(), '6'),
|
||||
Pattern("seven".to_string(), '7'),
|
||||
Pattern("eight".to_string(), '8'),
|
||||
Pattern("nine".to_string(), '9')
|
||||
];
|
||||
|
||||
let mut mutations = Vec::<Mutation>::new();
|
||||
|
||||
for pattern in patterns.iter() {
|
||||
let indices = str.match_indices(&pattern.0);
|
||||
indices.into_iter().for_each(|index| {
|
||||
mutations.push(Mutation(index.0, pattern.1));
|
||||
mutations.push(Mutation(index.0 + pattern.0.len(), pattern.1))
|
||||
});
|
||||
}
|
||||
|
||||
mutations.sort_by(|l, r| l.0.cmp(&r.0));
|
||||
let mut offset = 0;
|
||||
|
||||
let mut new_str = str.clone();
|
||||
|
||||
for mutation in mutations.iter() {
|
||||
new_str.insert(mutation.0 + offset, mutation.1);
|
||||
offset += 1;
|
||||
}
|
||||
|
||||
new_str
|
||||
|
||||
}).collect();
|
||||
|
||||
println!("{}", process(new_lines)?);
|
||||
|
||||
Ok(())
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user