🏡 index : ~doyle/aoc.git

author Jordan Doyle <jordan@doyle.la> 2024-12-07 22:35:17.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2024-12-07 22:36:53.0 +00:00:00
commit
ece9cfe2e572d3fcaeb682ac759757d44f2ea803 [patch]
tree
3babee9fb9c2e4bf0346befc3ad3e8f031053963
parent
7f35208133d37b08259a7e205de36522ff837775
download
ece9cfe2e572d3fcaeb682ac759757d44f2ea803.tar.gz

Add 2024 day 7



Diff

 Cargo.toml |  4 ++++
 README     |  4 ++--
 2024/07.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 173c6d7..fc0c471 100644
--- a/Cargo.toml
+++ a/Cargo.toml
@@ -34,6 +34,10 @@
name = "aoc2024-04"
path = "2024/04.rs"

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

[profile.release]
overflow-checks = true

diff --git a/README b/README
index 10906c9..6244692 100644
--- a/README
+++ a/README
@@ -20,8 +20,8 @@
|        4 | Rust     | |        4 | Haskell  | |        4 | Jsonnet  |
|        5 | PHP      | |        5 | Rust     | |        5 | HCL/TF   |
|        6 | Zig      | |        6 | Haskell  | |        6 | Apache2  |
+---------------------+ |        7 | Haskell  | +---------------------+
                        |        8 | Haskell  |
|        7 | Rust     | |        7 | Haskell  | +---------------------+
+---------------------+ |        8 | Haskell  |
                        |        9 | Haskell  |
                        |       10 | Haskell  |
                        |       11 | Haskell  |
diff --git a/2024/07.rs b/2024/07.rs
new file mode 100644
index 0000000..a366cb3 100644
--- /dev/null
+++ a/2024/07.rs
@@ -1,0 +1,58 @@
use std::{io::Read, str::FromStr};

use arrayvec::ArrayVec;

fn main() {
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    let input = input
        .lines()
        .map(|v| {
            let (l, r) = v.split_once(": ").unwrap();

            (
                u64::from_str(l).unwrap(),
                r.split_whitespace()
                    .map(u16::from_str)
                    .collect::<Result<ArrayVec<u16, 16>, _>>()
                    .unwrap(),
            )
        })
        .collect::<ArrayVec<_, 1024>>();

    play(&input, false);
    play(&input, true);
}

fn play(input: &ArrayVec<(u64, ArrayVec<u16, 16>), 1024>, part2: bool) {
    let mut res = 0;

    for (total, numbers) in input {
        let mut table = vec![u64::from(numbers[0])];

        for number in numbers.into_iter().skip(1) {
            let mut next_table = Vec::new();

            for val in table {
                let add = val + u64::from(*number);
                next_table.push(add);

                let mul = val * u64::from(*number);
                next_table.push(mul);

                if part2 {
                    let con = (val * 10_u64.pow(number.ilog10() + 1)) + u64::from(*number);
                    next_table.push(con);
                }
            }

            table = next_table;
        }

        if table.contains(total) {
            res += total;
        }
    }

    eprintln!("{res:?}");
}