🏡 index : ~doyle/aoc.git

author Jordan Doyle <jordan@doyle.la> 2024-12-08 15:14:34.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2024-12-08 15:15:44.0 +00:00:00
commit
cfe1a0f1c93e8e6e3bb88fcb2be6ea9c7023dfc2 [patch]
tree
7ebce7831d82ed164a75f31ed407890583cd2211
parent
ece9cfe2e572d3fcaeb682ac759757d44f2ea803
download
cfe1a0f1c93e8e6e3bb88fcb2be6ea9c7023dfc2.tar.gz

Add 2024 day 8



Diff

 Cargo.toml |  4 ++++
 README     |  4 ++--
 2024/08.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index fc0c471..254d04e 100644
--- a/Cargo.toml
+++ a/Cargo.toml
@@ -38,6 +38,10 @@
name = "aoc2024-07"
path = "2024/07.rs"

[[bin]]
name = "aoc2024-08"
path = "2024/08.rs"

[profile.release]
overflow-checks = true

diff --git a/README b/README
index 6244692..02fb2af 100644
--- a/README
+++ a/README
@@ -21,8 +21,8 @@
|        5 | PHP      | |        5 | Rust     | |        5 | HCL/TF   |
|        6 | Zig      | |        6 | Haskell  | |        6 | Apache2  |
|        7 | Rust     | |        7 | Haskell  | +---------------------+
+---------------------+ |        8 | Haskell  |
                        |        9 | Haskell  |
|        8 | Rust     | |        8 | Haskell  |
+---------------------+ |        9 | Haskell  |
                        |       10 | Haskell  |
                        |       11 | Haskell  |
                        |       12 | Rust     |
diff --git a/2024/08.rs b/2024/08.rs
new file mode 100644
index 0000000..cf75790 100644
--- /dev/null
+++ a/2024/08.rs
@@ -1,0 +1,65 @@
use std::collections::{HashMap, HashSet};

#[allow(clippy::cast_possible_wrap)]
fn main() {
    let mut frequencies = HashMap::<char, Vec<(i64, i64)>>::new();
    let mut max_x = 0;
    let mut max_y = 0;

    for (y, line) in std::io::stdin().lines().enumerate() {
        for (x, c) in line.unwrap().chars().enumerate() {
            if c != '.' {
                frequencies.entry(c).or_default().push((x as i64, y as i64));
            }
            max_x = x as i64;
        }
        max_y = y as i64;
    }

    let mut part1 = HashSet::new();
    let mut part2 = HashSet::new();

    for frequencies in frequencies.values() {
        for (x1, y1) in frequencies {
            for (x2, y2) in frequencies {
                if x1 == x2 && y1 == y2 {
                    continue;
                }

                let vx = x2 - x1;
                let vy = y2 - y1;

                let derive = |out: &mut HashSet<_>, n| {
                    let mut hit = false;

                    let dx = x1 + n * vx;
                    let dy = y1 + n * vy;
                    if dx >= 0 && dx <= max_x && dy >= 0 && dy <= max_y {
                        out.insert((dx, dy));
                        hit = true;
                    }

                    let dx = x2 - n * vx;
                    let dy = y2 - n * vy;
                    if dx >= 0 && dx <= max_x && dy >= 0 && dy <= max_y {
                        out.insert((dx, dy));
                        hit = true;
                    }

                    hit
                };

                derive(&mut part1, 2);

                for i in 0.. {
                    if !derive(&mut part2, i) {
                        break;
                    }
                }
            }
        }
    }

    eprintln!("{}", part1.len());
    eprintln!("{}", part2.len());
}