|
@@ -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);
|
|
|
+ }
|
|
|
+}
|