summaryrefslogtreecommitdiff
path: root/aoc22/day10/src/main.rs
blob: 42d6490ba9e7f911d5b4d5c9337b94a01c3f8829 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
fn main() {
    let input = lib::read_input(10);

    part1(&input);
    part2(&input);
}

fn part1(input: &str) {
    let addx = "addx ";

    let mut x = 1;
    let mut cycle = 1;
    let mut ans = 0;
    for line in input.lines() {
        if line.starts_with(addx) {
            cycle += 1;
            if [20, 60, 100, 140, 180, 220].contains(&cycle) {
                ans += cycle * x;
            }
            x += line[addx.len()..].parse::<i32>().unwrap();
            cycle += 1;
            if [20, 60, 100, 140, 180, 220].contains(&cycle) {
                ans += cycle * x;
            }
        } else {
            cycle += 1;
            if [20, 60, 100, 140, 180, 220].contains(&cycle) {
                ans += cycle * x;
            }
        }
    }
    println!("{}", ans);
}

fn part2(input: &str) {
    let addx = "addx ";

    let mut x = 1;
    let mut cycle = 1i32;
    let mut ans = ['.'; 40 * 6];
    for line in input.lines() {
        if line.starts_with(addx) {
            if [x - 1, x, x + 1].contains(&(cycle % 40)) {
                ans[cycle as usize - 1] = '#';
            }
            cycle += 1;
            x += line[addx.len()..].parse::<i32>().unwrap();
            if [x - 1, x, x + 1].contains(&(cycle % 40)) {
                ans[cycle as usize - 1] = '#';
            }
            cycle += 1;
        } else {
            if [x - 1, x, x + 1].contains(&(cycle % 40)) {
                ans[cycle as usize - 1] = '#';
            }
            cycle += 1;
        }
    }
    for i in 0..6 {
        println!("{}", &ans[i * 40..(i + 1) * 40].iter().collect::<String>());
    }
}