summaryrefslogtreecommitdiff
path: root/2024/day15.rs
blob: f3c499e8e016615eaca38483cb33745a0136c459 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::env;
use std::fs::File;
use std::io::Read;

#[derive(Debug, PartialEq, Clone, Copy)]
enum Field {
    Wall,
    Box,
    Empty,
}

fn sim(rx: i64, ry: i64, dx: i64, dy: i64, mut m: &mut Vec<Vec<Field>>) -> (i64, i64) {
    let mx = rx + dx;
    let my = ry + dy;

    let f = m[my as usize][mx as usize];
    if f == Field::Empty {
        return (mx, my);
    } else if f == Field::Wall {
        return (rx, ry);
    }

    let mut x = mx + dx;
    let mut y = my + dy;
    loop {
        let f = m[y as usize][x as usize];
        if f == Field::Wall {
            return (rx, ry);
        }
        if f == Field::Empty {
            m[my as usize][mx as usize] = Field::Empty;
            m[y as usize][x as usize] = Field::Box;
            return (mx, my);
        }
        x += dx;
        y += dy;
    }
}

fn gps(m: &Vec<Vec<Field>>) -> i64 {
    let mut ret = 0;
    for y in 0..m.len() {
        for x in 0..m[0].len() {
            if m[y][x] == Field::Box {
                ret += 100 * y + x;
            }
        }
    }
    return ret as i64;
}

fn print_m(m: &Vec<Vec<Field>>) {
    for l in m {
        for f in l {
            match f {
                Field::Box => print!("O"),
                Field::Empty => print!("."),
                Field::Wall => print!("#"),
            }
        }
        println!();
    }
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename = if args.len() == 1 {
        "in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl"
    } else {
        args[1].clone()
    };
    let mut f = File::open(filename).expect("cannot open file");
    let mut content = String::new();
    f.read_to_string(&mut content).expect("cannot read file");
    let (para1, para2) = content.trim_end().split_once("\n\n").unwrap();

    let mut rx = 0;
    let mut ry = 0;
    let mut m: Vec<Vec<Field>> = Vec::new();
    for (y, line) in para1.split("\n").enumerate() {
        let mut l: Vec<Field> = Vec::new();
        for (x, c) in line.chars().enumerate() {
            let x = x as i64;
            let y = y as i64;
            if c == '@' {
                rx = x;
                ry = y;
            }

            if c == 'O' {
                l.push(Field::Box);
            } else if c == '#' {
                l.push(Field::Wall);
            } else {
                l.push(Field::Empty);
            }
        }
        m.push(l);
    }

    let mut dirs: Vec<(i64, i64)> = Vec::new();
    for line in para2.split("\n") {
        for c in line.chars() {
            let d = match c {
                '<' => (-1, 0),
                '>' => (1, 0),
                '^' => (0, -1),
                'v' => (0, 1),
                _ => panic!(),
            };
            dirs.push(d);
        }
    }
    //println!("{:?}", dirs);

    for dir in dirs {
        (rx, ry) = sim(rx, ry, dir.0, dir.1, &mut m);
        //print_m(&m);
    }

    let res1 = gps(&m);

    let mut res2 = 0;

    println!("res1: {}", res1);
    println!("res2: {}", res2);
    //assert_eq!(res1, 1414416);
    //assert_eq!(res2, );
}