summaryrefslogtreecommitdiff
path: root/aoc/2024/07
diff options
context:
space:
mode:
authormhsn <mail@mhsn.net>2026-03-18 21:48:13 +0000
committermhsn <mail@mhsn.net>2026-03-18 21:48:13 +0000
commit86bac31392a76da84817eec020d2b84d099b3cc1 (patch)
treee2ee52db59b86b914d5b4bcceb19c9b5d899fff4 /aoc/2024/07
parent62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff)
downloadpuzzles-master.tar.gz
puzzles-master.zip
add other challenges supportHEADmaster
Diffstat (limited to 'aoc/2024/07')
-rwxr-xr-xaoc/2024/07/python.py37
-rw-r--r--aoc/2024/07/rust/Cargo.lock7
-rw-r--r--aoc/2024/07/rust/Cargo.toml6
-rw-r--r--aoc/2024/07/rust/src/main.rs71
4 files changed, 121 insertions, 0 deletions
diff --git a/aoc/2024/07/python.py b/aoc/2024/07/python.py
new file mode 100755
index 0000000..008af39
--- /dev/null
+++ b/aoc/2024/07/python.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+from fileinput import input
+
+lines = [line.strip() for line in input()]
+
+
+def solve(w, acc, rest, gold=False):
+ if not rest:
+ return w == acc
+ if w < acc:
+ return False
+ head, *rest = rest
+ return (
+ solve(w, acc + head, rest, gold)
+ or solve(w, acc * head, rest, gold)
+ or (gold and solve(w, int(str(acc) + str(head)), rest, gold))
+ )
+
+
+silver = 0
+gold = 0
+
+for line in lines:
+ want, nums = line.split(":")
+ want = int(want)
+ nums = [int(n) for n in nums.split()]
+ head, *rest = nums
+
+ if solve(want, head, rest):
+ silver += want
+ if solve(want, head, rest, True):
+ gold += want
+
+
+print("silver:", silver)
+print("gold:", gold)
diff --git a/aoc/2024/07/rust/Cargo.lock b/aoc/2024/07/rust/Cargo.lock
new file mode 100644
index 0000000..ac79b17
--- /dev/null
+++ b/aoc/2024/07/rust/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "puzzle"
+version = "0.1.0"
diff --git a/aoc/2024/07/rust/Cargo.toml b/aoc/2024/07/rust/Cargo.toml
new file mode 100644
index 0000000..88e7b42
--- /dev/null
+++ b/aoc/2024/07/rust/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "puzzle"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/aoc/2024/07/rust/src/main.rs b/aoc/2024/07/rust/src/main.rs
new file mode 100644
index 0000000..df586b1
--- /dev/null
+++ b/aoc/2024/07/rust/src/main.rs
@@ -0,0 +1,71 @@
+use std::io;
+
+fn main() -> io::Result<()> {
+ let eqns = io::stdin()
+ .lines()
+ .flatten()
+ .collect::<Vec<_>>()
+ .iter()
+ .map(|line| line.split_once(": ").unwrap())
+ .map(|(ans, nums)| {
+ (
+ ans.parse::<u64>().unwrap(),
+ nums.split_whitespace()
+ .map(|s| s.parse::<u64>().unwrap())
+ .collect::<Vec<_>>(),
+ )
+ })
+ .collect::<Vec<_>>();
+
+ let silver: u64 = eqns.iter().map(crate::silver).sum();
+ let gold: u64 = eqns.iter().map(crate::gold).sum();
+
+ println!("silver: {silver}");
+ println!("gold: {gold}");
+
+ return Ok(());
+}
+
+fn concat(x: &u64, y: &u64) -> u64 {
+ x * (10u64.pow(y.ilog10() + 1)) + y
+}
+
+fn calibration(want: u64, x: &u64, y: &u64, rest: &[u64], gold: bool) -> bool {
+ if *x > want {
+ return false;
+ }
+ match rest {
+ [] => want == x + y || want == x * y || (gold && want == concat(x, y)),
+ [z, rest @ ..] => {
+ calibration(want, &(x + y), z, rest, gold)
+ || calibration(want, &(x * y), z, rest, gold)
+ || (gold && calibration(want, &concat(x, y), z, rest, gold))
+ }
+ }
+}
+
+fn silver((want, nums): &(u64, Vec<u64>)) -> u64 {
+ match nums.as_slice() {
+ [x, y, rest @ ..] => {
+ if calibration(*want, x, y, rest, false) {
+ *want
+ } else {
+ 0
+ }
+ }
+ _ => 0,
+ }
+}
+
+fn gold((want, nums): &(u64, Vec<u64>)) -> u64 {
+ match nums.as_slice() {
+ [x, y, rest @ ..] => {
+ if calibration(*want, x, y, rest, true) {
+ *want
+ } else {
+ 0
+ }
+ }
+ _ => 0,
+ }
+}