formatted some code and renamed nonndiagonal to straight
Dieser Commit ist enthalten in:
Ursprung
50f451efd2
Commit
f3339b38e1
7 geänderte Dateien mit 67 neuen und 106 gelöschten Zeilen
|
@ -42,11 +42,7 @@ impl Round {
|
||||||
ok
|
ok
|
||||||
}
|
}
|
||||||
fn power(&self) -> u64 {
|
fn power(&self) -> u64 {
|
||||||
let mut sack = Sack {
|
let mut sack = Sack { reds: 0, greens: 0, blues: 0 };
|
||||||
reds: 0,
|
|
||||||
greens: 0,
|
|
||||||
blues: 0,
|
|
||||||
};
|
|
||||||
for take in self.takes.clone() {
|
for take in self.takes.clone() {
|
||||||
if take.red > sack.reds {
|
if take.red > sack.reds {
|
||||||
sack.reds = take.red
|
sack.reds = take.red
|
||||||
|
@ -70,11 +66,7 @@ impl From<&str> for Round {
|
||||||
takes: Vec::<Take>::new(),
|
takes: Vec::<Take>::new(),
|
||||||
};
|
};
|
||||||
for taking in value.get(doublecolon + 1..).unwrap().split(";") {
|
for taking in value.get(doublecolon + 1..).unwrap().split(";") {
|
||||||
let mut take = Take {
|
let mut take = Take { red: 0, green: 0, blue: 0 };
|
||||||
red: 0,
|
|
||||||
green: 0,
|
|
||||||
blue: 0,
|
|
||||||
};
|
|
||||||
for color in taking.split(',').map(str::trim) {
|
for color in taking.split(',').map(str::trim) {
|
||||||
let mut i = color.splitn(2, char::is_whitespace);
|
let mut i = color.splitn(2, char::is_whitespace);
|
||||||
let amount = parsenumber(i.next().unwrap());
|
let amount = parsenumber(i.next().unwrap());
|
||||||
|
@ -96,11 +88,7 @@ fn main() {
|
||||||
for game in DATA.split('\n') {
|
for game in DATA.split('\n') {
|
||||||
gamerounds.push(game.into());
|
gamerounds.push(game.into());
|
||||||
}
|
}
|
||||||
let sack = Sack {
|
let sack = Sack { reds: 12, greens: 13, blues: 14 };
|
||||||
reds: 12,
|
|
||||||
greens: 13,
|
|
||||||
blues: 14,
|
|
||||||
};
|
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
for round in gamerounds.clone() {
|
for round in gamerounds.clone() {
|
||||||
if round.possible(sack) {
|
if round.possible(sack) {
|
||||||
|
|
|
@ -7,10 +7,7 @@ include_data!(DATA 2024 01);
|
||||||
pub fn splitspace(input: &str) -> (u32, u32) {
|
pub fn splitspace(input: &str) -> (u32, u32) {
|
||||||
println!("{}", input);
|
println!("{}", input);
|
||||||
let mut output = input.split_ascii_whitespace();
|
let mut output = input.split_ascii_whitespace();
|
||||||
(
|
(parsenumber(output.next().unwrap()), parsenumber(output.next().unwrap()))
|
||||||
parsenumber(output.next().unwrap()),
|
|
||||||
parsenumber(output.next().unwrap()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn distance(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 {
|
fn distance(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 {
|
||||||
|
|
|
@ -34,8 +34,8 @@ fn main() {
|
||||||
let a = parsenumber::<u32>(capture.name("i").unwrap().as_str());
|
let a = parsenumber::<u32>(capture.name("i").unwrap().as_str());
|
||||||
let b = parsenumber::<u32>(capture.name("j").unwrap().as_str());
|
let b = parsenumber::<u32>(capture.name("j").unwrap().as_str());
|
||||||
sum += a * b
|
sum += a * b
|
||||||
}
|
},
|
||||||
(_, _) => {}
|
(_, _) => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("The Cleaned with check output is: {}", sum);
|
println!("The Cleaned with check output is: {}", sum);
|
||||||
|
|
|
@ -18,23 +18,13 @@ struct DirectionalKartesian<T: Integer> {
|
||||||
}
|
}
|
||||||
impl<T: Integer> From<(Kartesian<T>, KartesianDirection)> for DirectionalKartesian<T> {
|
impl<T: Integer> From<(Kartesian<T>, KartesianDirection)> for DirectionalKartesian<T> {
|
||||||
fn from(value: (Kartesian<T>, KartesianDirection)) -> Self {
|
fn from(value: (Kartesian<T>, KartesianDirection)) -> Self {
|
||||||
DirectionalKartesian {
|
DirectionalKartesian { x: value.0.x, y: value.0.y, dir: value.1 }
|
||||||
x: value.0.x,
|
|
||||||
y: value.0.y,
|
|
||||||
dir: value.1,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Integer + Copy> Into<(Kartesian<T>, KartesianDirection)> for &DirectionalKartesian<T> {
|
impl<T: Integer + Copy> Into<(Kartesian<T>, KartesianDirection)> for &DirectionalKartesian<T> {
|
||||||
fn into(self) -> (Kartesian<T>, KartesianDirection) {
|
fn into(self) -> (Kartesian<T>, KartesianDirection) {
|
||||||
(
|
(Kartesian { x: self.x, y: self.y }, self.dir)
|
||||||
Kartesian {
|
|
||||||
x: self.x,
|
|
||||||
y: self.y,
|
|
||||||
},
|
|
||||||
self.dir,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,19 +52,15 @@ fn find_guard(map: Table) -> Kartesian<i32> {
|
||||||
fn get_path(map: Table) -> Vec<DirectionalKartesian<i32>> {
|
fn get_path(map: Table) -> Vec<DirectionalKartesian<i32>> {
|
||||||
let mut position = (find_guard(map.clone()), KartesianDirection::Top);
|
let mut position = (find_guard(map.clone()), KartesianDirection::Top);
|
||||||
let maximum = Kartesian::new(map.len() as i32, map[0].len() as i32);
|
let maximum = Kartesian::new(map.len() as i32, map[0].len() as i32);
|
||||||
debug!(
|
debug!("Guard is on coordinate {}:{}", position.0.x + 1, position.0.y + 1);
|
||||||
"Guard is on coordinate {}:{}",
|
|
||||||
position.0.x + 1,
|
|
||||||
position.0.y + 1
|
|
||||||
);
|
|
||||||
let mut passed = Vec::new();
|
let mut passed = Vec::new();
|
||||||
passed.push(position.into());
|
passed.push(position.into());
|
||||||
loop {
|
loop {
|
||||||
position.0 = match position.0.checked_add_max(position.1.vector(), maximum) {
|
position.0 = match position.0.checked_add_max(position.1.vector().into(), maximum) {
|
||||||
None => {
|
None => {
|
||||||
debug!("Guard left the space after {} steps", passed.len());
|
debug!("Guard left the space after {} steps", passed.len());
|
||||||
return passed;
|
return passed;
|
||||||
}
|
},
|
||||||
Some(pos) => pos,
|
Some(pos) => pos,
|
||||||
};
|
};
|
||||||
if map[position.0.x as usize][position.0.y as usize] != '#' {
|
if map[position.0.x as usize][position.0.y as usize] != '#' {
|
||||||
|
@ -82,33 +68,26 @@ fn get_path(map: Table) -> Vec<DirectionalKartesian<i32>> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
position.0 -= position.1.vector();
|
position.0 -= position.1.vector();
|
||||||
debug!(
|
debug!("Guard turned right on {}:{}", position.0.x + 1, position.0.y + 1);
|
||||||
"Guard turned right on {}:{}",
|
|
||||||
position.0.x + 1,
|
|
||||||
position.0.y + 1
|
|
||||||
);
|
|
||||||
position.1 = position.1.clockwise(false);
|
position.1 = position.1.clockwise(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn solver1(input: &str) -> usize {
|
fn solver1(input: &str) -> usize {
|
||||||
let map = convert_to_array(input, line_to_char);
|
let map = convert_to_array::<_, _, '\n'>(input, line_to_char);
|
||||||
let mut path = get_path(map);
|
let mut path = get_path(map);
|
||||||
path.sort();
|
path.sort();
|
||||||
path.dedup_by_key(|p| (p.x, p.y));
|
path.dedup_by_key(|p| (p.x, p.y));
|
||||||
path.len()
|
path.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_looping(
|
fn is_looping(_path: Vec<DirectionalKartesian<i32>>, _start_point: DirectionalKartesian<i32>) -> bool {
|
||||||
path: Vec<DirectionalKartesian<i32>>,
|
|
||||||
start_point: DirectionalKartesian<i32>,
|
|
||||||
) -> bool {
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables, unused_mut)]
|
#[allow(unused_variables, unused_mut)]
|
||||||
fn solver2(input: &str) -> usize {
|
fn solver2(input: &str) -> usize {
|
||||||
let map = convert_to_array(input, line_to_char);
|
let map = convert_to_array::<_, _, '\n'>(input, line_to_char);
|
||||||
let maximum = Kartesian::new(map.len() as i32, map[0].len() as i32);
|
let maximum = Kartesian::new(map.len() as i32, map[0].len() as i32);
|
||||||
let mut path = get_path(map.clone());
|
let mut path = get_path(map.clone());
|
||||||
let mut p = path.iter();
|
let mut p = path.iter();
|
||||||
|
@ -123,10 +102,7 @@ fn solver2(input: &str) -> usize {
|
||||||
currentpoint = point.into();
|
currentpoint = point.into();
|
||||||
currentpoint.1 = currentpoint.1.clockwise(false);
|
currentpoint.1 = currentpoint.1.clockwise(false);
|
||||||
loop {
|
loop {
|
||||||
currentpoint.0 = match currentpoint
|
currentpoint.0 = match currentpoint.0.checked_add_max(currentpoint.1.vector(), maximum) {
|
||||||
.0
|
|
||||||
.checked_add_max(currentpoint.1.vector(), maximum)
|
|
||||||
{
|
|
||||||
None => break,
|
None => break,
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
};
|
};
|
||||||
|
@ -145,14 +121,8 @@ fn solver2(input: &str) -> usize {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!("Guard left the space after walking through {} distinct positions", solver1(DATA));
|
||||||
"Guard left the space after walking through {} distinct positions",
|
println!("There are {} positions that are good for loops", solver2(DATA));
|
||||||
solver1(DATA)
|
|
||||||
);
|
|
||||||
println!(
|
|
||||||
"There are {} positions that are good for loops",
|
|
||||||
solver2(DATA)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -19,27 +19,14 @@ enum Operation {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Operation {
|
impl Operation {
|
||||||
fn op<
|
fn op<T: Integer + Add + Mul + Pow<u32, Output = T> + std::ops::DivAssign + AddAssign + From<u32> + Copy>(self, a: T, b: T) -> T {
|
||||||
T: Integer
|
|
||||||
+ Add
|
|
||||||
+ Mul
|
|
||||||
+ Pow<u32, Output = T>
|
|
||||||
+ std::ops::DivAssign
|
|
||||||
+ AddAssign
|
|
||||||
+ From<u32>
|
|
||||||
+ Copy,
|
|
||||||
>(
|
|
||||||
self,
|
|
||||||
a: T,
|
|
||||||
b: T,
|
|
||||||
) -> T {
|
|
||||||
match self {
|
match self {
|
||||||
Operation::Add => a + b,
|
Operation::Add => a + b,
|
||||||
Operation::Multiply => a * b,
|
Operation::Multiply => a * b,
|
||||||
Operation::Concat => {
|
Operation::Concat => {
|
||||||
let ta = a * T::from(10).pow(numberlength(b));
|
let ta = a * T::from(10).pow(numberlength(b));
|
||||||
ta + b
|
ta + b
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,10 +34,7 @@ impl Operation {
|
||||||
fn get_data(line: &str) -> (u64, Vec<u64>) {
|
fn get_data(line: &str) -> (u64, Vec<u64>) {
|
||||||
let (result, numbers) = line.split_once(':').unwrap();
|
let (result, numbers) = line.split_once(':').unwrap();
|
||||||
|
|
||||||
(
|
(parsenumber(result.trim()), Vec::from(convert_to_array::<_, _, ' '>(numbers, parsenumber)))
|
||||||
parsenumber(result.trim()),
|
|
||||||
Vec::from(convert_to_array::<_, _, ' '>(numbers, parsenumber)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_equation(result: u64, numbers: Vec<u64>, ops: Vec<Operation>) -> bool {
|
fn test_equation(result: u64, numbers: Vec<u64>, ops: Vec<Operation>) -> bool {
|
||||||
|
@ -87,7 +71,11 @@ fn get_ops(len: usize, conf: usize) -> Vec<Operation> {
|
||||||
|
|
||||||
fn get_ops_with_concat(len: u32) -> Vec<Vec<Operation>> {
|
fn get_ops_with_concat(len: u32) -> Vec<Vec<Operation>> {
|
||||||
let mut ops = Vec::with_capacity(3u64.saturating_pow(len) as usize);
|
let mut ops = Vec::with_capacity(3u64.saturating_pow(len) as usize);
|
||||||
for i in [Operation::Add, Operation::Concat, Operation::Multiply] {
|
for i in [
|
||||||
|
Operation::Add,
|
||||||
|
Operation::Concat,
|
||||||
|
Operation::Multiply,
|
||||||
|
] {
|
||||||
if len > 1 {
|
if len > 1 {
|
||||||
for mut oplist in get_ops_with_concat(len - 1) {
|
for mut oplist in get_ops_with_concat(len - 1) {
|
||||||
oplist.insert(0, i);
|
oplist.insert(0, i);
|
||||||
|
@ -146,32 +134,56 @@ mod test {
|
||||||
for c in [
|
for c in [
|
||||||
(
|
(
|
||||||
190,
|
190,
|
||||||
Vec::from([10, 19]),
|
Vec::from([
|
||||||
|
10, 19,
|
||||||
|
]),
|
||||||
Vec::from([Operation::Multiply]),
|
Vec::from([Operation::Multiply]),
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
3267,
|
3267,
|
||||||
Vec::from([81, 40, 27]),
|
Vec::from([
|
||||||
Vec::from([Operation::Add, Operation::Multiply]),
|
81, 40, 27,
|
||||||
|
]),
|
||||||
|
Vec::from([
|
||||||
|
Operation::Add,
|
||||||
|
Operation::Multiply,
|
||||||
|
]),
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
3267,
|
3267,
|
||||||
Vec::from([81, 40, 27]),
|
Vec::from([
|
||||||
Vec::from([Operation::Multiply, Operation::Add]),
|
81, 40, 27,
|
||||||
|
]),
|
||||||
|
Vec::from([
|
||||||
|
Operation::Multiply,
|
||||||
|
Operation::Add,
|
||||||
|
]),
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
292,
|
292,
|
||||||
Vec::from([11, 6, 16, 20]),
|
Vec::from([
|
||||||
Vec::from([Operation::Add, Operation::Multiply, Operation::Add]),
|
11, 6, 16, 20,
|
||||||
|
]),
|
||||||
|
Vec::from([
|
||||||
|
Operation::Add,
|
||||||
|
Operation::Multiply,
|
||||||
|
Operation::Add,
|
||||||
|
]),
|
||||||
true,
|
true,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
292,
|
292,
|
||||||
Vec::from([11, 6, 16, 20]),
|
Vec::from([
|
||||||
Vec::from([Operation::Add, Operation::Multiply, Operation::Multiply]),
|
11, 6, 16, 20,
|
||||||
|
]),
|
||||||
|
Vec::from([
|
||||||
|
Operation::Add,
|
||||||
|
Operation::Multiply,
|
||||||
|
Operation::Multiply,
|
||||||
|
]),
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
] {
|
] {
|
||||||
|
|
|
@ -136,14 +136,8 @@ fn main() {
|
||||||
let mut file_disk = raw_disk.clone();
|
let mut file_disk = raw_disk.clone();
|
||||||
compact_blocks(&mut block_disk);
|
compact_blocks(&mut block_disk);
|
||||||
let mut cksum = calculate_checksum(block_disk);
|
let mut cksum = calculate_checksum(block_disk);
|
||||||
println!(
|
println!("Checksum for the blockcompacted disk is: {}", cksum);
|
||||||
"Checksum for the blockcompacted disk is: {}",
|
|
||||||
cksum
|
|
||||||
);
|
|
||||||
compact_files(&mut file_disk);
|
compact_files(&mut file_disk);
|
||||||
cksum = calculate_checksum(file_disk);
|
cksum = calculate_checksum(file_disk);
|
||||||
println!(
|
println!("Checksum for the filecompacted disk is: {}", cksum,);
|
||||||
"Checksum for the filecompacted disk is: {}",
|
|
||||||
cksum,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -352,12 +352,12 @@ impl KartesianDirection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter(none: bool, diagonal: bool, nondiagonal: bool) -> KartesianIterator {
|
pub fn iter(none: bool, diagonal: bool, straight: bool) -> KartesianIterator {
|
||||||
KartesianIterator {
|
KartesianIterator {
|
||||||
i: 0,
|
i: 0,
|
||||||
none: none,
|
none,
|
||||||
diagonal: diagonal,
|
diagonal,
|
||||||
nondiagonal: nondiagonal,
|
straight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,7 +383,7 @@ pub struct KartesianIterator {
|
||||||
i: u8,
|
i: u8,
|
||||||
diagonal: bool,
|
diagonal: bool,
|
||||||
none: bool,
|
none: bool,
|
||||||
nondiagonal: bool,
|
straight: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for KartesianIterator {
|
impl Iterator for KartesianIterator {
|
||||||
|
@ -391,7 +391,7 @@ impl Iterator for KartesianIterator {
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let mut i = self.i;
|
let mut i = self.i;
|
||||||
if self.nondiagonal {
|
if self.straight {
|
||||||
if i < 4 {
|
if i < 4 {
|
||||||
self.i += 1;
|
self.i += 1;
|
||||||
}
|
}
|
||||||
|
|
Laden …
Tabelle hinzufügen
In neuem Issue referenzieren