seperated part1 and part 2
Dieser Commit ist enthalten in:
Ursprung
68767c83ae
Commit
b727a37834
2 geänderte Dateien mit 31 neuen und 48 gelöschten Zeilen
|
@ -1,6 +1,4 @@
|
||||||
use std::fmt::Display;
|
use advent_of_code::{strings::{convert_to_array, parsenumber}, KD};
|
||||||
|
|
||||||
use advent_of_code::strings::{convert_to_array, parsenumber};
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use advent_of_code::{include_data, include_example};
|
use advent_of_code::{include_data, include_example};
|
||||||
|
|
||||||
|
@ -10,22 +8,6 @@ fn get_numbers(input: &str) -> Vec<u32> {
|
||||||
input.split(char::is_whitespace).map(parsenumber).collect()
|
input.split(char::is_whitespace).map(parsenumber).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
|
||||||
enum Direction {
|
|
||||||
Up,
|
|
||||||
Down,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for Direction {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
let _ = f.write_str(match *self {
|
|
||||||
Direction::Down => "Down",
|
|
||||||
Direction::Up => "Up",
|
|
||||||
});
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait SafeCheck {
|
trait SafeCheck {
|
||||||
fn safe(&self) -> bool;
|
fn safe(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
@ -74,9 +56,9 @@ fn safe(record: Vec<u32>) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let direction = if ups > downs {
|
let direction = if ups > downs {
|
||||||
Direction::Up
|
KD::Top
|
||||||
} else {
|
} else {
|
||||||
Direction::Down
|
KD::Bottom
|
||||||
};
|
};
|
||||||
let mut errors = 0;
|
let mut errors = 0;
|
||||||
i = record.clone().into_iter();
|
i = record.clone().into_iter();
|
||||||
|
@ -89,8 +71,9 @@ fn safe(record: Vec<u32>) -> bool {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match (direction, last > level) {
|
match (direction, last > level) {
|
||||||
(Direction::Up, false) | (Direction::Down, true) => last = level,
|
(KD::Top, false) | (KD::Bottom, true) => last = level,
|
||||||
(Direction::Up, true) | (Direction::Down, false) => errors += 1,
|
(KD::Top, true) | (KD::Bottom, false) => errors += 1,
|
||||||
|
_=>unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if errors > 0 {
|
if errors > 0 {
|
||||||
|
@ -112,9 +95,6 @@ fn main() {
|
||||||
safe_with_dampener += 1;
|
safe_with_dampener += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if safe_reports != 598 || safe_with_dampener == 623 {
|
|
||||||
println!("Broken implementation")
|
|
||||||
}
|
|
||||||
println!("Safe Reports: {safe_reports}");
|
println!("Safe Reports: {safe_reports}");
|
||||||
println!("Safe Reports with dampener: {safe_with_dampener}")
|
println!("Safe Reports with dampener: {safe_with_dampener}")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,38 +5,41 @@ use regex::{Captures, Regex};
|
||||||
|
|
||||||
include_data!(DATA 2024 03);
|
include_data!(DATA 2024 03);
|
||||||
|
|
||||||
fn main() {
|
fn parse_part1(input : Vec<Captures<'_>>) -> u32 {
|
||||||
let part1re = Regex::new(r"mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)").unwrap();
|
|
||||||
let multiplies: Vec<(u32, u32)> = part1re
|
|
||||||
.captures_iter(DATA)
|
|
||||||
.map(|capture| {
|
|
||||||
let i = capture.name("i").unwrap().as_str();
|
|
||||||
let j = capture.name("j").unwrap().as_str();
|
|
||||||
let a = parsenumber(i);
|
|
||||||
let b = parsenumber(j);
|
|
||||||
(a, b)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
for (i, j) in multiplies {
|
let (mut i,mut j): (u32, u32);
|
||||||
sum += i * j;
|
for capture in input {
|
||||||
|
i = parsenumber(capture.name("i").unwrap().as_str());
|
||||||
|
j=parsenumber(capture.name("j").unwrap().as_str());
|
||||||
|
sum += i * j
|
||||||
}
|
}
|
||||||
println!("The Cleaned output is: {}", sum);
|
sum
|
||||||
let part2re = Regex::new(r"(mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)|do\(\)|don't\(\))").unwrap();
|
}
|
||||||
let inputs: Vec<Captures<'_>> = part2re.captures_iter(DATA).collect();
|
|
||||||
let mut parse = true;
|
fn parse_part2(input : Vec<Captures<'_>>)->u32 {
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
for capture in inputs {
|
let mut parse = true;
|
||||||
|
let (mut i,mut j): (u32, u32);
|
||||||
|
for capture in input {
|
||||||
match (capture.get(1).unwrap().as_str(), parse) {
|
match (capture.get(1).unwrap().as_str(), parse) {
|
||||||
("do()", _) => parse = true,
|
("do()", _) => parse = true,
|
||||||
("don't()", _) => parse = false,
|
("don't()", _) => parse = false,
|
||||||
(_, true) => {
|
(_, true) => {
|
||||||
let a = parsenumber::<u32>(capture.name("i").unwrap().as_str());
|
i = parsenumber(capture.name("i").unwrap().as_str());
|
||||||
let b = parsenumber::<u32>(capture.name("j").unwrap().as_str());
|
j = parsenumber(capture.name("j").unwrap().as_str());
|
||||||
sum += a * b
|
sum += i * j
|
||||||
},
|
},
|
||||||
(_, _) => {},
|
(_, _) => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sum
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let part1 = Regex::new(r"mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)").unwrap();
|
||||||
|
let part2 = Regex::new(r"(mul\((?<i>\d{1,3}),(?<j>\d{1,3})\)|do\(\)|don't\(\))").unwrap();
|
||||||
|
let sum = parse_part1(part1.captures_iter(DATA).collect());
|
||||||
|
println!("The Cleaned output is: {}", sum);
|
||||||
|
let sum = parse_part2(part2.captures_iter(DATA).collect());
|
||||||
println!("The Cleaned with check output is: {}", sum);
|
println!("The Cleaned with check output is: {}", sum);
|
||||||
}
|
}
|
||||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren