diff --git a/Cargo.lock b/Cargo.lock index fdf73ca..8437ddf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "advent-of-code" version = "0.1.0" dependencies = [ "advent-of-code-macros", + "num", "regex", ] @@ -28,12 +29,91 @@ dependencies = [ "memchr", ] +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "proc-macro2" version = "1.0.92" diff --git a/Cargo.toml b/Cargo.toml index 24c6161..d54fbbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -309,6 +309,7 @@ path = "src/bin/2024/25.rs" name = "advent_of_code" [dependencies] +num="0.4.*" [dependencies.advent-of-code-macros] path = "macros" diff --git a/src/bin/2024/02.rs b/src/bin/2024/02.rs index aa2e7d0..7b9b65b 100644 --- a/src/bin/2024/02.rs +++ b/src/bin/2024/02.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use advent_of_code::strings::parsenumber; +use advent_of_code::strings::{convert_to_array, parsenumber}; #[allow(unused_imports)] use advent_of_code_macros::{include_data, include_example}; @@ -101,7 +101,7 @@ fn safe(record: Vec) -> bool { } fn main() { - let numbers = DATA.split('\n').map(get_numbers); + let numbers = convert_to_array(DATA, get_numbers); let mut safe_reports = 0; let mut safe_with_dampener = 0; for report in numbers { diff --git a/src/lib.rs b/src/lib.rs index e8dfd78..91b8396 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,78 @@ +use num::*; + pub mod strings; + +pub fn matrix(zero: bool, diag: bool, non_diag: bool) -> Vec<(i32, i32)> { + const MIN: i32 = -1; + const MAX: i32 = 1; + let mut d = Vec::new(); + for i in MIN..=MAX { + for j in MIN..=MAX { + let ia = i.abs(); + let ja = j.abs(); + if zero && i == 0 && j == 0 { + d.push((i, j)); + } + if diag && ia == 1 && ja == 1 { + d.push((i, j)); + } + if non_diag && ia != ja && ia + ja == 1 { + d.push((i, j)); + } + } + } + d +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +struct Kartesian +where + T: Integer, +{ + x: T, + y: T, +} +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +struct Euclidian +where + T: Integer, +{ + x: T, + y: T, + z: T, +} + +type Table = Vec>; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +enum KartesianDirection { + TopLeft, + Top, + TopRight, + Left, + None, + Right, + BottomLeft, + Bottom, + BottomRight, +} + +impl KartesianDirection { + fn vector>(self) -> Kartesian { + const BELOW: i8 = -1; + const ON: i8 = 0; + const UPPER: i8 = 1; + Kartesian:: { + x: match self { + Self::TopLeft | Self::Left | Self::BottomLeft => BELOW.into(), + Self::Top | Self::None | Self::Bottom => ON.into(), + Self::TopRight | Self::Right | Self::BottomRight => UPPER.into(), + }, + y: match self { + Self::TopLeft | Self::Top | Self::TopRight => BELOW.into(), + Self::Left | Self::None | Self::Right => ON.into(), + Self::BottomLeft | Self::Bottom | Self::BottomRight => UPPER.into(), + }, + } + } +} diff --git a/src/strings.rs b/src/strings.rs index 7feb745..05f4a5a 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -156,3 +156,12 @@ pub fn get_string_numbers(input: &str) -> Vec { } output } + +#[inline] +pub fn line_to_char(line: &str) -> Vec { + Vec::from_iter(line.chars()) +} + +pub fn convert_to_array T>(input: &str, func: F) -> Vec { + input.split('\n').map(func).collect() +}