DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: 8cb64e29451a837b5f931ff5cf2843d5d6f2ad44 aoc2024/src/day14/mod.rs -rw-r--r-- 3.0 KiB
8cb64e29Jonni Liljamo feat: day14 a month ago
                                                                                
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::{
    fs::File,
    io::{BufRead, BufReader},
    path::Path,
};

pub fn solve(input: &Path) -> anyhow::Result<()> {
    println!("part one: {}", part_one(input)?);
    //println!("part two: {}", part_two(input)?);

    Ok(())
}

#[derive(Debug)]
struct R {
    p: P<usize>,
    v: P<i32>,
}

impl R {
    fn from_str(line: &str) -> Self {
        let mut split = line.split_whitespace();
        let mut p_split = split.next().unwrap()[2..].split(",");
        let mut v_split = split.next().unwrap()[2..].split(",");
        Self {
            p: P {
                x: p_split.next().unwrap().parse().unwrap(),
                y: p_split.next().unwrap().parse().unwrap(),
            },
            v: P {
                x: v_split.next().unwrap().parse().unwrap(),
                y: v_split.next().unwrap().parse().unwrap(),
            },
        }
    }
}

#[derive(Debug)]
struct P<T> {
    x: T,
    y: T,
}

fn _print_robots_on_map(robots: &[R], w: usize, h: usize, q: bool) {
    for y in 0..h {
        if q && y == h / 2 {
            println!();
            continue;
        }
        for x in 0..w {
            if q && x == w / 2 {
                print!(" ");
                continue;
            }
            let robots_on_tile = robots.iter().filter(|r| r.p.x == x && r.p.y == y).count();
            if robots_on_tile > 0 {
                print!("{}", robots_on_tile);
            } else {
                //print!(".");
                print!(" ");
            }
        }
        println!();
    }
}

fn part_one(input: &Path) -> anyhow::Result<usize> {
    let reader = BufReader::new(File::open(input)?);

    let mut robots: Vec<R> = vec![];
    for line in reader.lines() {
        robots.push(R::from_str(&line?));
    }

    //let (w, h) = (11, 7);
    let (w, h) = (101, 103);

    //print_robots_on_map(&robots, w, h, false);
    for _ in 0..100 {
        //for i in 0..10403 {
        for robot in &mut robots {
            let mut nx: i32 = robot.p.x as i32 + robot.v.x;
            if nx >= w as i32 {
                nx = (nx - w as i32).abs();
            } else if nx < 0 {
                nx = (nx + w as i32).abs();
            }
            let mut ny: i32 = robot.p.y as i32 + robot.v.y;
            if ny >= h as i32 {
                ny = (ny - h as i32).abs();
            } else if ny < 0 {
                ny = (ny + h as i32).abs();
            }
            robot.p.x = nx as usize;
            robot.p.y = ny as usize;
        }
        //println!();
        //print_robots_on_map(&robots, w, h, true);
    }
    //println!();
    //print_robots_on_map(&robots, w, h, true);

    let q1 = robots
        .iter()
        .filter(|r| r.p.x < w / 2 && r.p.y < h / 2)
        .count();
    let q2 = robots
        .iter()
        .filter(|r| r.p.x > w / 2 && r.p.y < h / 2)
        .count();
    let q3 = robots
        .iter()
        .filter(|r| r.p.x > w / 2 && r.p.y > h / 2)
        .count();
    let q4 = robots
        .iter()
        .filter(|r| r.p.x < w / 2 && r.p.y > h / 2)
        .count();

    Ok(q1 * q2 * q3 * q4)
}