aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit 0e6bebb63d5795ca9143b9713d1d82ebec8d7e03
parent fa26f694d8cf98272763952a698301ee9935c19e
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Fri, 27 Jun 2025 09:53:33 +0200

Day 10

Diffstat:
A2022/10/a.rs | 23+++++++++++++++++++++++
A2022/10/b.rs | 29+++++++++++++++++++++++++++++
A2022/10/common.rs | 13+++++++++++++
3 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/2022/10/a.rs 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 @@ -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 @@ -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::<i64>().unwrap(); + Instruction::Add(n) + } else { + Instruction::Noop + } +}