Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-13 #31

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

| Year-Day | Task | Scala | Rust | Others |
|----------|:-------------------------------------------------------------------------------|:-----------------------------------------------------------------------:|:----------------------------------------------:|:----------------------------------------------------------------------:|
| 2024-13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent13.scala) | [Rust](rust/y2024/src/bin/solution_2024_13.rs) | |
| 2024-12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent12.scala) | [Rust](rust/y2024/src/bin/solution_2024_12.rs) | |
| 2024-11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent11.scala) | [Rust](rust/y2024/src/bin/solution_2024_11.rs) | |
| 2024-10 | [Hoof It](https://adventofcode.com/2024/day/10) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent10.scala) | [Rust](rust/y2024/src/bin/solution_2024_10.rs) | |
| 2024-09 | [Disk Fragmenter](https://adventofcode.com/2024/day/9) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent09.scala) | [Rust](rust/y2024/src/bin/solution_2024_09.rs) | |
| 2024-08 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [Scala](scala2/src/main/scala/jurisk/adventofcode/y2024/Advent08.scala) | [Rust](rust/y2024/src/bin/solution_2024_08.rs) | |
Expand Down
6 changes: 1 addition & 5 deletions python/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
Note - Python 3 is required.

```
pip install z3-solver
pip install more-itertools
pip install numpy
pip install sympy
pip install pytest
pip install -r requirements.txt
python y20AB/dayXY.py
python -m pytest
```
Expand Down
5 changes: 5 additions & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
z3-solver
more-itertools
numpy
sympy
pytest
42 changes: 42 additions & 0 deletions rust/common/src/bool_ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub trait BoolOptionOps {
fn then_some_unit(self) -> Option<()>;
fn then_none(self) -> Option<()>;
}

impl BoolOptionOps for bool {
fn then_some_unit(self) -> Option<()> {
if self { Some(()) } else { None }
}

fn then_none(self) -> Option<()> {
if self { None } else { Some(()) }
}
}

pub trait BoolResultOps {
#[expect(clippy::missing_errors_doc)]
fn then_ok_unit<E, FE>(self, error: FE) -> Result<(), E>
where
FE: FnOnce() -> E;

#[expect(clippy::missing_errors_doc)]
fn then_err_unit<E, FE>(self, error: FE) -> Result<(), E>
where
FE: FnOnce() -> E;
}

impl BoolResultOps for bool {
fn then_ok_unit<E, FE>(self, error: FE) -> Result<(), E>
where
FE: FnOnce() -> E,
{
if self { Ok(()) } else { Err(error()) }
}

fn then_err_unit<E, FE>(self, error: FE) -> Result<(), E>
where
FE: FnOnce() -> E,
{
if self { Err(error()) } else { Ok(()) }
}
}
1 change: 1 addition & 0 deletions rust/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(step_trait)]

pub mod area2d;
pub mod bool_ops;
pub mod circular;
pub mod coords2d;
pub mod coords3d;
Expand Down
45 changes: 45 additions & 0 deletions rust/common/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use num_traits::Num;

#[must_use]
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn factors(n: u32) -> Vec<u32> {
Expand Down Expand Up @@ -61,3 +63,46 @@ pub fn gcd(a: i64, b: i64) -> i64 {
pub fn lcm(a: i64, b: i64) -> i64 {
a.abs() * b.abs() / gcd(a, b)
}

#[must_use]
pub fn solve_two_variable_integer_linear_equation_system<N: Num + Copy>(
a: N,
b: N,
c: N,
d: N,
e: N,
f: N,
) -> Option<(N, N)> {
let div = |dividend: N, divisor: N| -> Option<N> {
(dividend % divisor == N::zero()).then(|| dividend / divisor)
};

let det = a * d - b * c;
if det == N::zero() {
None
} else {
let x = div(e * d - b * f, det);
let y = div(a * f - e * c, det);
match (x, y) {
(Some(x), Some(y)) => Some((x, y)),
_ => None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_solve_two_variable_integer_linear_equation_system() {
assert_eq!(
solve_two_variable_integer_linear_equation_system(2, 1, 3, -1, 15, 5),
Some((4, 7))
);
assert_eq!(
solve_two_variable_integer_linear_equation_system(1, 2, 1, 2, 5, 6),
None
);
}
}
10 changes: 10 additions & 0 deletions rust/common/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ pub fn segments_separated_by_double_newline(input: &str) -> Vec<String> {
.collect()
}

pub fn parse_segments_separated_by_double_newline<T: FromStr>(input: &str) -> Result<Vec<T>, Error>
where
<T as FromStr>::Err: Debug,
{
segments_separated_by_double_newline(input)
.into_iter()
.map(|x| parse_str(&x))
.collect()
}

/// # Errors
///
/// Will return `Err` if parsing fails.
Expand Down
16 changes: 15 additions & 1 deletion rust/y2024/resources/13-test-00.txt
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
noop
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400

Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176

Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450

Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
Loading
Loading