updated old solutions

* splitspace is now parsing
* added  some examples(04 is syntetic)
Dieser Commit ist enthalten in:
Sebastian Tobie 2024-12-08 02:04:16 +01:00
Ursprung 61c433eab7
Commit 8e7e46f47f
9 geänderte Dateien mit 256 neuen und 71 gelöschten Zeilen

6
examples/2024/01.txt Normale Datei
Datei anzeigen

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

6
examples/2024/02.txt Normale Datei
Datei anzeigen

@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

17
examples/2024/04.txt Normale Datei
Datei anzeigen

@ -0,0 +1,17 @@
S.MX
.A.M
S.MA
M.MS
.A..
S.S.
S.S.
.A..
M.M.
M.SS
.A.A
M.SM
SAMX
....
M...
AA..
S.S.

10
examples/2024/06.txt Normale Datei
Datei anzeigen

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

Datei anzeigen

@ -1,9 +1,18 @@
use advent_of_code::strings::{convert_to_array, parsenumber, splitspace}; 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};
include_data!(DATA 2024 01); include_data!(DATA 2024 01);
pub fn splitspace(input: &str) -> (u32, u32) {
println!("{}", input);
let mut output = input.split_ascii_whitespace();
(
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 {
let mut distance = 0; let mut distance = 0;
for i in 0..leftlist.len() { for i in 0..leftlist.len() {
@ -38,8 +47,8 @@ fn main() {
let mut leftlist = Vec::<u32>::with_capacity(1000); let mut leftlist = Vec::<u32>::with_capacity(1000);
let mut rightlist = Vec::<u32>::with_capacity(1000); let mut rightlist = Vec::<u32>::with_capacity(1000);
for (left, right) in convert_to_array::<_, _, '\n'>(DATA, splitspace) { for (left, right) in convert_to_array::<_, _, '\n'>(DATA, splitspace) {
leftlist.push(parsenumber(left)); leftlist.push(left);
rightlist.push(parsenumber(right)); rightlist.push(right);
} }
leftlist.sort(); leftlist.sort();
rightlist.sort(); rightlist.sort();

Datei anzeigen

@ -101,7 +101,7 @@ fn safe(record: Vec<u32>) -> bool {
} }
fn main() { fn main() {
let numbers = convert_to_array::<_,_,'\n'>(DATA, get_numbers); let numbers = convert_to_array::<_, _, '\n'>(DATA, get_numbers);
let mut safe_reports = 0; let mut safe_reports = 0;
let mut safe_with_dampener = 0; let mut safe_with_dampener = 0;
for report in numbers { for report in numbers {

Datei anzeigen

@ -31,8 +31,8 @@ fn main() {
("do()", _) => parse = true, ("do()", _) => parse = true,
("don't()", _) => parse = false, ("don't()", _) => parse = false,
(_, true) => { (_, true) => {
let a = parsenumber(capture.name("i").unwrap().as_str()); let a = parsenumber::<u32>(capture.name("i").unwrap().as_str());
let b = parsenumber(capture.name("j").unwrap().as_str()); let b = parsenumber::<u32>(capture.name("j").unwrap().as_str());
sum += a * b sum += a * b
} }
(_, _) => {} (_, _) => {}

Datei anzeigen

@ -1,78 +1,220 @@
#[allow(unused_variables)] use std::ops::Neg;
use std::ops::RangeInclusive;
#[allow(unused_variables)]
#[allow(unused_imports)] #[allow(unused_imports)]
use advent_of_code::{include_data, include_example}; use advent_of_code::{include_data, include_example};
use advent_of_code::{
matrix,
strings::{convert_to_array, line_to_char},
};
include_data!(DATA 2024 04); include_example!(DATA 2024 04);
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
enum Direction { struct TextPos {
Forward(usize, usize), startx: usize,
Backward(usize, usize), starty: usize,
endx: usize,
endy: usize,
length: usize,
} }
impl Direction { #[inline]
fn iter(&self) -> RangeInclusive<usize> { fn dist(a: (usize, usize), b: (usize, usize)) -> (i32, i32) {
let (from, to) = match self { (a.0 as i32 - b.0 as i32, a.1 as i32 - b.1 as i32)
Direction::Forward(from, to) => (from, to), }
Direction::Backward(to, from) => (from, to),
}; #[inline]
*from..=*to fn dist_abs(a: (usize, usize), b: (usize, usize)) -> (usize, usize) {
let t = dist(a, b);
(t.0.abs() as usize, t.1.abs() as usize)
}
impl TextPos {
const fn center(self) -> (usize, usize) {
(
self.startx.saturating_add(self.endx).div_ceil(2),
self.starty.saturating_add(self.endy).div_ceil(2),
)
}
fn crossing(self, other: &&Self) -> bool {
if self.length != other.length && self.length != 3 {
todo!()
}
if self.center() != other.center() {
return false;
}
true
} }
} }
fn main() { #[test]
let table: Vec<Vec<char>> = DATA fn test_crossing() {
.split('\n') let cases = [
.map(|line| line.chars().collect()) (
.collect(); TextPos {
let length = table[0].len(); startx: 0,
let mut xmas; starty: 0,
let mut amount = 0; endx: 2,
let directions = [ endy: 2,
Direction::Forward(0, length - 1), length: 3,
Direction::Backward(0, length - 1), },
TextPos {
startx: 0,
starty: 0,
endx: 2,
endy: 2,
length: 3,
},
true,
),
(
TextPos {
startx: 1,
starty: 0,
endx: 1,
endy: 2,
length: 3,
},
TextPos {
startx: 0,
starty: 1,
endx: 2,
endy: 1,
length: 3,
},
true,
),
(
TextPos {
startx: 0,
starty: 0,
endx: 2,
endy: 0,
length: 3,
},
TextPos {
startx: 1,
starty: 0,
endx: 2,
endy: 0,
length: 3,
},
false,
),
]; ];
for r in directions.clone() { for case in cases {
for c in directions.clone() { assert_eq!(case.0.crossing(&&case.1), case.2)
println!("Horizontal: {:?}, vertical: {:?}", r.clone(), c.clone()); }
for row in r.iter() { }
xmas = 0;
for column in c.iter() { fn valid(x: usize, y: i32, max: usize) -> Option<usize> {
println!( let tmp = if y < 0 {
"Row {}, Column {}, dir {:?} {:?}", x.checked_sub(y.neg() as usize)
row, } else {
column, x.checked_add(y as usize)
r.clone(), };
c.clone() if let Some(xi) = tmp {
); if xi < max {
match (xmas, table[row][column]) { return Some(xi);
(_x, 'X') => { }
xmas = 1; }
//println!("Found X, previous was {}", _x) None
} }
(1, 'M') => {
xmas = 2; fn validxy(
//println!("Found XM") x: usize,
} y: usize,
(2, 'A') => { modx: i32,
xmas = 3; mody: i32,
//println!("Found XMA") heigth: usize,
} length: usize,
(3, 'S') => { ) -> Option<(usize, usize)> {
xmas = 0; if let Some(xi) = valid(x, modx, length) {
amount += 1; if let Some(yi) = valid(y, mody, heigth) {
//println!("found XMAS in {}:{}", row, column); return Some((xi, yi));
} }
(_x, _ch) => { }
xmas = 0; None
//println!("Reset, was {}({})", _x, _ch) }
fn find_text(data: Vec<Vec<char>>, length: usize, heigth: usize, text: &str) -> (usize, usize) {
let first: char = text.chars().nth(0).unwrap();
let mut amount = 0;
let mut found;
let mut character;
let mut chars;
let mut coords = Vec::new();
let mut currentpos;
for row in 0..heigth {
for column in 0..length {
let char = data[row][column];
if char != first {
continue;
}
for (x, y) in matrix(false, true, true) {
if x == y && y == 0 {
continue;
}
found = 1;
chars = text.chars();
chars.next();
loop {
character = chars.next();
if character == None {
amount += 1;
currentpos = TextPos {
startx: row,
starty: column,
endx: (row as i32 + x * found) as usize,
endy: (column as i32 + y * found) as usize,
length: text.len(),
};
coords.push(currentpos);
break;
}
if let Some((posmodx, posmody)) =
validxy(row, column, x * found, y * found, length, heigth)
{
if data[posmodx][posmody] == character.unwrap() {
found += 1;
} else {
break;
} }
} else {
break;
} }
} }
} }
} }
} }
println!("There are {} XMAS in text", amount); let mut xtexts = Vec::new();
for node in coords.clone() {
for o in coords.iter().filter(|other| node.crossing(other)) {
let other = *o;
if node == other {
continue;
}
let x = (node.min(other), node.max(other));
if !xtexts.contains(&x) {
xtexts.push(x);
}
}
}
println!("{:?}", xtexts);
(amount, xtexts.len())
}
fn main() {
let table = convert_to_array(DATA, line_to_char);
let length = table[0].len();
let (amount, _) = find_text(table.clone(), length, table.len(), "XMAS");
println!("There are {} XMAS in text", amount);
let (amount, amount_cross) = find_text(table.clone(), length, table.len(), "MAS");
println!(
"There are {} MAS in text, are {} crossed",
amount, amount_cross
);
} }

Datei anzeigen

@ -1,11 +1,6 @@
use num::{pow::Pow, Integer}; use num::{pow::Pow, Integer};
use std::ops::AddAssign; use std::ops::AddAssign;
pub fn splitspace(input: &str) -> (&str, &str) {
let mut output = input.split_ascii_whitespace();
(output.next().unwrap().trim(), output.next().unwrap().trim())
}
pub fn parsenumber<T: Integer + From<u32> + Pow<u32, Output = T> + AddAssign + Copy>( pub fn parsenumber<T: Integer + From<u32> + Pow<u32, Output = T> + AddAssign + Copy>(
input: &str, input: &str,
) -> T { ) -> T {