diff options
Diffstat (limited to '2022')
| -rw-r--r-- | 2022/10/a.rs | 23 | ||||
| -rw-r--r-- | 2022/10/b.rs | 29 | ||||
| -rw-r--r-- | 2022/10/common.rs | 13 |
3 files changed, 65 insertions, 0 deletions
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 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | fn main() { | ||
| 5 | let mut x: i64 = 1; | ||
| 6 | let mut cycles: usize = 0; | ||
| 7 | let mut step: usize = 20; | ||
| 8 | let mut score: i64 = 0; | ||
| 9 | let mut line = String::new(); | ||
| 10 | while std::io::stdin().read_line(&mut line).unwrap() > 0 { | ||
| 11 | let oldx = x; | ||
| 12 | match read_instruction(&line) { | ||
| 13 | Instruction::Add(n) => { x += n; cycles += 2; }, | ||
| 14 | Instruction::Noop => { cycles += 1; } | ||
| 15 | } | ||
| 16 | if cycles >= step { | ||
| 17 | score += oldx * (step as i64); | ||
| 18 | step += 40; | ||
| 19 | } | ||
| 20 | line.clear(); | ||
| 21 | } | ||
| 22 | println!("{score}"); | ||
| 23 | } | ||
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 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | fn draw(cycles: usize, x: i64) { | ||
| 5 | let c = cycles % 40; | ||
| 6 | if cycles > 0 && c == 0 { println!(); } | ||
| 7 | print!("{}", if (x - (c as i64)).abs() <= 1 { '#' } else { '.' }); | ||
| 8 | } | ||
| 9 | |||
| 10 | fn main() { | ||
| 11 | let mut x: i64 = 1; | ||
| 12 | let mut cycles: usize = 0; | ||
| 13 | let mut line = String::new(); | ||
| 14 | while std::io::stdin().read_line(&mut line).unwrap() > 0 { | ||
| 15 | match read_instruction(&line) { | ||
| 16 | Instruction::Add(n) => { | ||
| 17 | draw(cycles, x); | ||
| 18 | draw(cycles+1, x); | ||
| 19 | cycles += 2; | ||
| 20 | x += n; | ||
| 21 | }, | ||
| 22 | Instruction::Noop => { | ||
| 23 | draw(cycles, x); | ||
| 24 | cycles += 1; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | line.clear(); | ||
| 28 | } | ||
| 29 | } | ||
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 @@ | |||
| 1 | pub enum Instruction { | ||
| 2 | Add(i64), | ||
| 3 | Noop | ||
| 4 | } | ||
| 5 | |||
| 6 | pub fn read_instruction(line: &str) -> Instruction { | ||
| 7 | if &line[..4] == "addx" { | ||
| 8 | let n = line[5..line.len()-1].parse::<i64>().unwrap(); | ||
| 9 | Instruction::Add(n) | ||
| 10 | } else { | ||
| 11 | Instruction::Noop | ||
| 12 | } | ||
| 13 | } | ||
