From 0e6bebb63d5795ca9143b9713d1d82ebec8d7e03 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 27 Jun 2025 09:53:33 +0200 Subject: Day 10 --- 2022/10/a.rs | 23 +++++++++++++++++++++++ 2022/10/b.rs | 29 +++++++++++++++++++++++++++++ 2022/10/common.rs | 13 +++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 2022/10/a.rs create mode 100644 2022/10/b.rs create mode 100644 2022/10/common.rs diff --git a/2022/10/a.rs b/2022/10/a.rs new file mode 100644 index 0000000..cdab283 --- /dev/null +++ b/2022/10/a.rs @@ -0,0 +1,23 @@ +mod common; +use common::*; + +fn main() { + let mut x: i64 = 1; + let mut cycles: usize = 0; + let mut step: usize = 20; + let mut score: i64 = 0; + let mut line = String::new(); + while std::io::stdin().read_line(&mut line).unwrap() > 0 { + let oldx = x; + match read_instruction(&line) { + Instruction::Add(n) => { x += n; cycles += 2; }, + Instruction::Noop => { cycles += 1; } + } + if cycles >= step { + score += oldx * (step as i64); + step += 40; + } + line.clear(); + } + println!("{score}"); +} diff --git a/2022/10/b.rs b/2022/10/b.rs new file mode 100644 index 0000000..ab0091f --- /dev/null +++ b/2022/10/b.rs @@ -0,0 +1,29 @@ +mod common; +use common::*; + +fn draw(cycles: usize, x: i64) { + let c = cycles % 40; + if cycles > 0 && c == 0 { println!(); } + print!("{}", if (x - (c as i64)).abs() <= 1 { '#' } else { '.' }); +} + +fn main() { + let mut x: i64 = 1; + let mut cycles: usize = 0; + let mut line = String::new(); + while std::io::stdin().read_line(&mut line).unwrap() > 0 { + match read_instruction(&line) { + Instruction::Add(n) => { + draw(cycles, x); + draw(cycles+1, x); + cycles += 2; + x += n; + }, + Instruction::Noop => { + draw(cycles, x); + cycles += 1; + } + } + line.clear(); + } +} diff --git a/2022/10/common.rs b/2022/10/common.rs new file mode 100644 index 0000000..c2216a0 --- /dev/null +++ b/2022/10/common.rs @@ -0,0 +1,13 @@ +pub enum Instruction { + Add(i64), + Noop +} + +pub fn read_instruction(line: &str) -> Instruction { + if &line[..4] == "addx" { + let n = line[5..line.len()-1].parse::().unwrap(); + Instruction::Add(n) + } else { + Instruction::Noop + } +} -- cgit v1.3