Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-the-Bomb committed Jan 2, 2024
1 parent efe7fd9 commit 288072a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ overflow-checks = true
lto = "fat"

[dependencies]
rustworkx-core = "0.13.2"
rustworkx-core = "0.13"
59 changes: 36 additions & 23 deletions src/bin/day24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ macro_rules! parse_part {
}
}

#[inline]
fn float_cmp(a: f64, b: f64) -> bool {
(a - b).abs() < f64::EPSILON
}

impl FromStr for Hailstone {
type Err = ParseHailstoneError;

Expand Down Expand Up @@ -63,23 +68,32 @@ impl Hailstone {

#[inline]
fn b(&self) -> f64 {
self.y_pos - self.m() * self.x_pos
self.m()
.mul_add(-self.x_pos, self.y_pos)
}

#[inline]
fn evaluate(&self, x: f64) -> f64 {
self.m() * x + self.b()
self.m()
.mul_add(x, self.b())
}

#[inline]
fn in_domain(&self, x: f64, y: f64) -> bool {
(if self.x_vel > 0.0 { x >= self.x_pos }
else if self.x_vel == 0.0 { x == self.x_pos }
else { x <= self.x_pos })
&&
(if self.y_vel > 0.0 { y >= self.y_pos }
else if self.y_vel == 0.0 { y == self.y_pos }
else { y <= self.y_pos })
(if self.x_vel > 0.0 {
x >= self.x_pos
} else if float_cmp(self.x_vel, 0.0) {
float_cmp(x, self.x_pos)
} else {
x <= self.x_pos
})
&& (if self.y_vel > 0.0 {
y >= self.y_pos
} else if float_cmp(self.x_vel, 0.0) {
float_cmp(y, self.y_pos)
} else {
y <= self.y_pos
})
}

#[inline]
Expand All @@ -106,20 +120,19 @@ impl Day24 {
hailstones
.iter()
.enumerate()
.flat_map(|(i, hs1)|
hailstones
.iter()
.take(i)
.filter(|hs2|
hs1.intersection(hs2)
.map(|(x, y)|
hs1.in_domain(x, y)
&& hs2.in_domain(x, y)
&& (200_000_000_000_000.0..=400_000_000_000_000.0).contains(&x)
&& (200_000_000_000_000.0..=400_000_000_000_000.0).contains(&y)
)
.unwrap_or_default()
.flat_map(|(i, hs1)| hailstones
.iter()
.take(i)
.filter(|hs2| hs1
.intersection(hs2)
.map(|(x, y)|
hs1.in_domain(x, y)
&& hs2.in_domain(x, y)
&& (200_000_000_000_000.0..=400_000_000_000_000.0).contains(&x)
&& (200_000_000_000_000.0..=400_000_000_000_000.0).contains(&y)
)
.unwrap_or_default()
)
)
.count()
}
Expand All @@ -140,7 +153,7 @@ impl Solution for Day24 {
println!("Part 2: {p2}");

assert_eq!(p1, 14672);
//assert_eq!(p2, 646_810_057_104_753);
assert_eq!(p2, 646_810_057_104_753);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/bin/day25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use aoc_2023::Solution;
pub struct Day25;

impl Day25 {
/// # Panics
///
/// If failed to parse line
pub fn part_one<T: Display>(&self, inp: T) -> usize {
let mut graph = GraphMap::new();
let inp = inp
Expand All @@ -20,19 +23,16 @@ impl Day25 {
let (left, right) = line
.split_once(':')
.unwrap();
for node in right
.trim()
.split_whitespace()
{
for node in right.split_whitespace() {
graph.add_edge(left, node, 1);
graph.add_edge(node, left, 1);
}
}

if let Ok(Some((_, partition_1))) = min_cut(
&graph,
|_| Ok::<usize, usize>(1))
{
|_| Ok::<usize, usize>(1)
) {
let size_1 = partition_1.len();
let size_2 = graph.node_count() - size_1;

Expand All @@ -51,11 +51,11 @@ impl Solution for Day25 {
const NAME: &'static str = "Snowverload";

fn run(&self, inp: String) {
let p1 = self.part_one(&inp);
let p1 = self.part_one(inp);

println!("Part 1: {p1}");

assert_eq!(p1, 554064);
assert_eq!(p1, 554_064);
}
}

Expand Down

0 comments on commit 288072a

Please sign in to comment.