day5
This commit is contained in:
parent
19e598e07d
commit
540ff9ae04
5 changed files with 252 additions and 6 deletions
|
|
@ -1,4 +1,3 @@
|
|||
use std::net::Shutdown::Read;
|
||||
use shared::{Answer, Solution};
|
||||
use regex::Regex;
|
||||
|
||||
|
|
@ -6,7 +5,7 @@ pub struct Day03;
|
|||
|
||||
impl Solution for Day03 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
let lines = input.split("\n").collect();
|
||||
let lines: Vec<&str> = input.split("\n").collect();
|
||||
|
||||
let pattern = Regex::new(
|
||||
// r"[^\d.^]?(\d+)[\d.]|[^\d.](\d+)[\d.$]?"
|
||||
|
|
@ -18,7 +17,7 @@ impl Solution for Day03 {
|
|||
for i in 0..lines.len() {
|
||||
for m in pattern.find_iter(lines[i]) {
|
||||
match lines.get(i-1) {
|
||||
Some()
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
56
aoc2023/src/day05.rs
Normal file
56
aoc2023/src/day05.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
use std::fmt::Debug;
|
||||
use shared::{Solution, Answer};
|
||||
|
||||
pub struct Day05;
|
||||
|
||||
impl Solution for Day05 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
let blocks: Vec<&str> = input.split("\n\n")
|
||||
.collect();
|
||||
|
||||
let seeds: Vec<u64> = blocks[0].split(" ")
|
||||
.skip(1)
|
||||
.map(|x| x.parse::<u64>().unwrap())
|
||||
.collect();
|
||||
|
||||
let rules: Vec<Vec<Vec<u64>>> = blocks.iter()
|
||||
.skip(1)
|
||||
.map(|rule_block|
|
||||
rule_block.split("\n")
|
||||
.skip(1)
|
||||
.map(|rule| rule.split(" ").map( |y|
|
||||
y.parse::<u64>().unwrap()).collect()
|
||||
).collect()
|
||||
).collect();
|
||||
|
||||
let final_seeds: Vec<u64> = seeds.iter()
|
||||
.map(|seed|
|
||||
{
|
||||
let mut res = seed.clone();
|
||||
for rule in &rules {
|
||||
res = map(res, &rule);
|
||||
}
|
||||
res
|
||||
}
|
||||
).collect();
|
||||
|
||||
Answer::from(final_seeds.iter().min().unwrap().clone())
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn map(seed: u64, rules: &Vec<Vec<u64>>) -> u64 {
|
||||
for rule in rules {
|
||||
let dest = rule[0];
|
||||
let source = rule[1];
|
||||
let range = rule[2];
|
||||
|
||||
if (source..(source+range)).contains(&seed) {
|
||||
return seed - source + dest
|
||||
}
|
||||
}
|
||||
seed
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod day01;
|
||||
pub mod day02;
|
||||
mod day03;
|
||||
pub mod day03;
|
||||
pub mod day05;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue