Jake Fenton 1 ano atrás
pai
commit
a49a8e3337
2 arquivos alterados com 255 adições e 0 exclusões
  1. 109 0
      src/bin/10.rs
  2. 146 0
      src/examples/10.txt

+ 109 - 0
src/bin/10.rs

@@ -0,0 +1,109 @@
+pub fn part_one(input: &str) -> Option<i32> {
+    let mut cycle = 1;
+    let mut looking_for = 20;
+    let mut register = 1;
+    let mut reg_sum = 0;
+    
+    for line in input.lines() {
+        if line.starts_with("n") {
+            if cycle == looking_for {
+                reg_sum += cycle * register;
+
+                looking_for += 40;
+            }
+            cycle += 1;
+        } else {
+            // cycle will tick over while executing
+            if cycle == looking_for - 1 {
+
+                reg_sum += register * looking_for;
+                looking_for += 40;
+            } else if cycle == looking_for {
+                reg_sum += register * looking_for;
+                looking_for += 40;
+            }
+            register += line.split_once(" ").unwrap().1.parse::<i32>().unwrap();
+            cycle += 2;
+        }
+
+        if cycle > 220 {
+            break
+        }
+    }
+
+    Some(reg_sum)
+}
+
+pub fn part_two(input: &str) -> Option<i32> {
+    let mut cycle = 1;
+    let mut looking_for = 20;
+    let mut register = 1;
+    let mut reg_sum = 0;
+
+    let mut lines = input.lines();
+
+    // let mut op = lines.next().unwrap();
+    let mut queued_ad: Option<i32> = None;
+    let mut output: String = String::new();
+    let mut waiting = false;
+
+    for clock in 0..240 {
+        println!("cycle {clock} val {register}");
+        if clock > 1 && clock % 40 == 0 {
+            output += "\n";
+        }
+        if (register.clamp(0, 39) - 1 <= clock % 40) && (clock % 40 <= register.clamp(0, 39) + 1) {
+            output += "#";
+        } else {
+            output += " ";
+        }
+
+        match queued_ad {
+            Some(i) => {
+                println!("cycle {clock} finishing addx {i}");
+                register += i;
+                queued_ad = None;
+                continue;
+            }
+            _ => {}
+        }
+
+        let op = lines.next().unwrap();
+        println!("op is {op}");
+        if op.starts_with("n") {
+            // do nothing this clock cycle
+            println!("cycle {clock} noop");
+        } else {
+            println!("cycle {clock} queuing add");
+            queued_ad = Some(op.split_once(" ").unwrap().1.parse().unwrap());
+            waiting = true;
+        }
+    }
+
+    println!("{}", output);
+
+    todo!()
+}
+
+fn main() {
+    let input = &advent_of_code::read_file("inputs", 10);
+    advent_of_code::solve!(1, part_one, input);
+    advent_of_code::solve!(2, part_two, input);
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_part_one() {
+        let input = advent_of_code::read_file("examples", 10);
+        assert_eq!(part_one(&input), Some(13140));
+    }
+
+    #[test]
+    fn test_part_two() {
+        let input = advent_of_code::read_file("examples", 10);
+        assert_eq!(part_two(&input), None);
+    }
+}

+ 146 - 0
src/examples/10.txt

@@ -0,0 +1,146 @@
+addx 15
+addx -11
+addx 6
+addx -3
+addx 5
+addx -1
+addx -8
+addx 13
+addx 4
+noop
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx -35
+addx 1
+addx 24
+addx -19
+addx 1
+addx 16
+addx -11
+noop
+noop
+addx 21
+addx -15
+noop
+noop
+addx -3
+addx 9
+addx 1
+addx -3
+addx 8
+addx 1
+addx 5
+noop
+noop
+noop
+noop
+noop
+addx -36
+noop
+addx 1
+addx 7
+noop
+noop
+noop
+addx 2
+addx 6
+noop
+noop
+noop
+noop
+noop
+addx 1
+noop
+noop
+addx 7
+addx 1
+noop
+addx -13
+addx 13
+addx 7
+noop
+addx 1
+addx -33
+noop
+noop
+noop
+addx 2
+noop
+noop
+noop
+addx 8
+noop
+addx -1
+addx 2
+addx 1
+noop
+addx 17
+addx -9
+addx 1
+addx 1
+addx -3
+addx 11
+noop
+noop
+addx 1
+noop
+addx 1
+noop
+noop
+addx -13
+addx -19
+addx 1
+addx 3
+addx 26
+addx -30
+addx 12
+addx -1
+addx 3
+addx 1
+noop
+noop
+noop
+addx -9
+addx 18
+addx 1
+addx 2
+noop
+noop
+addx 9
+noop
+noop
+noop
+addx -1
+addx 2
+addx -37
+addx 1
+addx 3
+noop
+addx 15
+addx -21
+addx 22
+addx -6
+addx 1
+noop
+addx 2
+addx 1
+noop
+addx -10
+noop
+noop
+addx 20
+addx 1
+addx 2
+addx 2
+addx -6
+addx -11
+noop
+noop
+noop