advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 08be272b68f31424b8f5502051a966acb786d7b4
parent f1e0d8a4c29d7a2ddc2420947e2cdf1a29ea105b
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sat, 10 Dec 2022 11:10:47 -0500

Add 2022 day 10

Diffstat:
A2022/day10/Cargo.toml | 8++++++++
A2022/day10/input.txt | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day10/src/main.rs | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 238 insertions(+), 0 deletions(-)
diff --git a/2022/day10/Cargo.toml b/2022/day10/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "day10"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/2022/day10/input.txt b/2022/day10/input.txt
@@ -0,0 +1,137 @@
+addx 1
+noop
+addx 2
+addx 5
+addx 3
+noop
+addx -1
+addx 5
+noop
+noop
+addx 5
+noop
+addx 3
+noop
+addx 6
+addx -4
+noop
+noop
+addx 5
+noop
+addx -32
+addx 35
+addx 5
+addx -31
+addx 7
+addx -13
+addx 2
+addx 2
+addx 5
+addx 6
+addx -5
+addx 2
+addx 5
+addx 2
+addx 2
+addx -17
+addx 18
+addx 5
+addx 2
+addx -30
+addx 31
+addx 2
+addx 2
+addx -32
+addx -1
+addx 10
+addx -8
+noop
+noop
+addx 6
+addx 16
+noop
+addx -11
+addx 3
+addx -2
+addx 3
+noop
+addx 6
+noop
+addx -2
+noop
+addx 7
+addx 3
+addx -2
+addx 4
+addx -20
+noop
+addx -14
+addx -2
+addx 6
+addx 2
+addx 3
+noop
+addx 2
+addx -1
+addx 4
+noop
+addx 5
+noop
+addx 2
+addx 3
+addx -2
+addx 3
+noop
+addx 4
+noop
+addx 5
+noop
+addx 2
+addx -24
+addx -15
+addx 17
+addx -10
+addx 2
+addx -5
+addx 6
+noop
+addx 3
+addx 2
+addx 2
+noop
+addx 3
+addx 2
+addx 5
+addx -2
+addx 3
+addx 2
+addx 2
+addx 5
+addx 2
+addx -18
+addx -19
+noop
+addx 1
+addx 2
+addx 5
+addx 3
+noop
+noop
+noop
+noop
+addx 2
+addx 5
+addx 2
+addx 3
+noop
+addx -8
+addx 11
+noop
+noop
+addx 2
+addx 5
+addx 2
+addx 3
+noop
+addx -34
+noop
diff --git a/2022/day10/src/main.rs b/2022/day10/src/main.rs
@@ -0,0 +1,93 @@
+#[derive(Debug, Clone)]
+enum Op {
+    Noop,
+    Addx(i32),
+}
+
+impl Op {
+    fn from_str(s: &str) -> Option<Self> {
+        let tmp = s.split(' ').collect::<Vec<_>>();
+        match tmp[0] {
+            "noop" => Some(Self::Noop),
+            "addx" => Some(Self::Addx(tmp[1].parse::<i32>().unwrap())),
+            _ => None,
+        }
+    }
+}
+
+fn main() {
+    let input = std::fs::read_to_string("input.txt")
+        .unwrap()
+        .trim()
+        .split("\n")
+        .map(|x| Op::from_str(x).unwrap())
+        .collect::<Vec<Op>>();
+    // 13820
+    println!("Part 1: {}", part_1(&input));
+    // ZKGRKGRK
+    println!("Part 2:");
+    part_2(&input)
+}
+
+fn part_1(input: &Vec<Op>) -> i32 {
+    let mut res = 0;
+    fn check_signal(res: &mut i32, t: usize, x: i32) {
+        if t == 20 || t == 60 || t == 100 || t == 140 || t == 180 || t == 220 {
+            *res += t as i32 * x;
+        }
+    }
+    let mut x = 1;
+    let mut t = 1;
+
+    for op in input.iter() {
+        check_signal(&mut res, t, x);
+        match op {
+            Op::Noop => {
+                t += 1;
+            }
+            Op::Addx(a) => {
+                t += 1;
+                check_signal(&mut res, t, x);
+                t += 1;
+                x += a;
+            }
+        }
+    }
+    res
+}
+
+fn part_2(input: &Vec<Op>) {
+    const CRT_WIDTH: usize = 40;
+    const CRT_HEIGHT: usize = 6;
+    const CRT_PIXELS: usize = CRT_WIDTH * CRT_HEIGHT;
+    let mut crt = [false; CRT_PIXELS];
+    fn draw(crt: &mut [bool; CRT_PIXELS], t: usize, x: i32) {
+        let idx = t - 1;
+        let pos = (idx % CRT_WIDTH) as i32;
+        if pos == x - 1 || pos == x || pos == x + 1 {
+            crt[idx] = true;
+        }
+    }
+    let mut x = 1;
+    let mut t = 1;
+    for op in input.iter() {
+        draw(&mut crt, t, x);
+        match op {
+            Op::Noop => {
+                t += 1;
+            }
+            Op::Addx(a) => {
+                t += 1;
+                draw(&mut crt, t, x);
+                t += 1;
+                x += a;
+            }
+        }
+    }
+    for (i, lit) in crt.iter().enumerate() {
+        print!("{}", if *lit { '#' } else { '.' });
+        if (i + 1) % CRT_WIDTH == 0 {
+            println!();
+        }
+    }
+}