aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-10 20:43:34 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-10 20:43:34 +0100
commit3fdd2db85ff1b86d2318bf5f1668bb7afc032813 (patch)
treed53e2425566f351077aa7d76224792a9f33c6d39 /AoC2022
parent95aa169c8aa57aea0e004486fc7ecba6552e51b1 (diff)
downloadscratch-3fdd2db85ff1b86d2318bf5f1668bb7afc032813.tar.gz
scratch-3fdd2db85ff1b86d2318bf5f1668bb7afc032813.tar.bz2
scratch-3fdd2db85ff1b86d2318bf5f1668bb7afc032813.zip
[AoC2022] Rust Day 10
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/10/solver.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/AoC2022/10/solver.rs b/AoC2022/10/solver.rs
new file mode 100644
index 0000000..0b2ca75
--- /dev/null
+++ b/AoC2022/10/solver.rs
@@ -0,0 +1,55 @@
+use std::fs;
+use std::io::{self, BufRead};
+
+fn probe(cycle: &i32, register: &i32) -> Option<i32> {
+ let probes = vec![20, 60, 100, 140, 180, 220];
+
+ if probes.contains(cycle) {
+ Some(cycle * register)
+ } else {
+ None
+ }
+}
+
+fn draw_pixel(cycle: &i32, register: &i32) {
+ let pixel = (cycle - 1) % 40;
+ if pixel == 0 {
+ println!("");
+ }
+ let mark = if (register - 1) <= pixel && pixel <= (register + 1) {
+ "#"
+ } else {
+ "."
+ };
+ print!("{}", mark);
+}
+fn main() {
+ let file = fs::File::open("input").unwrap();
+ let lines = io::BufReader::new(file).lines();
+ let mut register = 1;
+ let mut cycle = 1;
+ let mut measures = Vec::<i32>::new();
+
+ for line in lines {
+ draw_pixel(&cycle, &register);
+ cycle += 1;
+ let step1 = probe(&cycle, &register);
+ let inst = line.expect("valid instruction");
+ let inst = inst.split(" ").collect::<Vec<&str>>();
+ let step2 = if inst[0] == "addx" {
+ draw_pixel(&cycle, &register);
+ cycle += 1;
+ register += inst[1].parse::<i32>().unwrap();
+ probe(&cycle, &register)
+ } else {
+ None
+ };
+
+ match [step1, step2] {
+ [Some(x), _] => measures.push(x),
+ [None, Some(y)] => measures.push(y),
+ _ => (),
+ }
+ }
+ println!("\n{:?} {}", measures, measures.iter().sum::<i32>());
+}