summaryrefslogtreecommitdiff
path: root/aoc/2024/03
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/03
parent62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff)
downloadpuzzles-master.tar.gz
puzzles-master.zip
add other challenges supportHEADmaster
Diffstat (limited to 'aoc/2024/03')
-rwxr-xr-xaoc/2024/03/python.py23
-rwxr-xr-xaoc/2024/03/raku.raku17
-rw-r--r--aoc/2024/03/rust/Cargo.lock7
-rw-r--r--aoc/2024/03/rust/Cargo.toml4
-rw-r--r--aoc/2024/03/rust/src/main.rs68
5 files changed, 119 insertions, 0 deletions
diff --git a/aoc/2024/03/python.py b/aoc/2024/03/python.py
new file mode 100755
index 0000000..f456580
--- /dev/null
+++ b/aoc/2024/03/python.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+import re
+from fileinput import input
+
+s = "\n".join(input())
+
+silver = sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", s))
+
+gold = 0
+active = True
+for g in re.findall(r"(?:mul\((\d{1,3}),(\d{1,3})\))|(don't\(\))|(do\(\))", s):
+ match g, active:
+ case (x, y, "", ""), True:
+ gold += int(x) * int(y)
+ case (_, _, "don't()", _), _:
+ active = False
+ case (_, _, _, "do()"), _:
+ active = True
+
+
+print("silver:", silver)
+print("gold:", gold)
diff --git a/aoc/2024/03/raku.raku b/aoc/2024/03/raku.raku
new file mode 100755
index 0000000..38f040e
--- /dev/null
+++ b/aoc/2024/03/raku.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/env raku
+
+my $p = slurp;
+
+sub p2 (($on, $tot), ($i, |n)) {
+ given $i {
+ when "mul" { return ($on , $tot + [*] $on, |n.list) }
+ when "do" { return (1 , $tot) }
+ when "don't" { return (0 , $tot) }
+ }
+};
+
+$p ~~ m:g/ mul\((\d ** 1..3)\,(\d ** 1..3)\) /;
+say "silver: ", $/.map({ [*] $_.list }).sum;
+
+$p ~~ m:g/ (mul)\((\d ** 1..3)\,(\d ** 1..3)\) | (do)\(\) | (don\'t)\(\) /;
+say "gold: ", ([[&p2]] <1 0>, |$/)[1];
diff --git a/aoc/2024/03/rust/Cargo.lock b/aoc/2024/03/rust/Cargo.lock
new file mode 100644
index 0000000..ac79b17
--- /dev/null
+++ b/aoc/2024/03/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/03/rust/Cargo.toml b/aoc/2024/03/rust/Cargo.toml
new file mode 100644
index 0000000..5a7c59b
--- /dev/null
+++ b/aoc/2024/03/rust/Cargo.toml
@@ -0,0 +1,4 @@
+[package]
+name = "puzzle"
+version = "0.1.0"
+edition = "2021"
diff --git a/aoc/2024/03/rust/src/main.rs b/aoc/2024/03/rust/src/main.rs
new file mode 100644
index 0000000..b268463
--- /dev/null
+++ b/aoc/2024/03/rust/src/main.rs
@@ -0,0 +1,68 @@
+use std::io::{self, Read};
+
+#[derive(Debug)]
+enum Instruction {
+ Do,
+ Dont,
+ Mul(u64, u64),
+}
+
+impl From<&Instruction> for u64 {
+ fn from(val: &Instruction) -> Self {
+ match val {
+ Instruction::Mul(x, y) => x * y,
+ _ => 0,
+ }
+ }
+}
+
+fn main() -> io::Result<()> {
+ let mut program = String::new();
+ io::stdin().read_to_string(&mut program)?;
+
+ let instrs = (0..program.len())
+ .filter_map(|idx| {
+ let next = &program[idx..];
+ if next.starts_with("do()") {
+ Some(Instruction::Do)
+ } else if next.starts_with("don't()") {
+ Some(Instruction::Dont)
+ } else if next.starts_with("mul(") {
+ let mut first = 0;
+ let mut curr = 0;
+
+ for ch in next.bytes().skip(4) {
+ match (ch, curr) {
+ ((b'0'..=b'9'), _) => curr = curr * 10 + (ch - b'0') as u64,
+ (_, 0) => return None,
+ (b',', _) => {
+ first = curr;
+ curr = 0;
+ }
+ (b')', _) => {
+ return Some(Instruction::Mul(first, curr));
+ }
+ _ => return None,
+ };
+ }
+ None
+ } else {
+ None
+ }
+ })
+ .collect::<Vec<_>>();
+
+ let silver: u64 = instrs.iter().map(u64::from).sum();
+ let (gold, _) = instrs
+ .iter()
+ .fold((0 as u64, true), |(acc, on), instr| match (instr, on) {
+ (Instruction::Do, _) => (acc, true),
+ (Instruction::Dont, _) => (acc, false),
+ (Instruction::Mul(_, _), true) => (acc + u64::from(instr), true),
+ _ => (acc, on),
+ });
+ println!("silver: {silver}");
+ println!("gold: {gold}");
+
+ return Ok(());
+}