summaryrefslogtreecommitdiff
path: root/2024/day13.rs
blob: dd189237fbe69f541d3a96ce99b43cd1d01fa3ba (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
133
134
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::env;
use std::fs::File;
use std::io::Read;

#[derive(Debug, PartialEq, Clone)]
struct Machine {
    ax: i128,
    ay: i128,
    bx: i128,
    by: i128,
    px: i128,
    py: i128,
}

fn solve(m: &Machine) -> Option<i128> {
    let mut min_price = None;

    for a in 0..100 {
        for b in 0..100 {
            let x = a * m.ax + b * m.bx;
            let y = a * m.ay + b * m.by;
            if x == m.px && y == m.py {
                let price = a * 3 + b * 1;
                if min_price == None || price < min_price.unwrap() {
                    min_price = Some(price);
                }
            }
        }
    }

    min_price
}

fn solve2(m: &Machine) -> Option<i128> {
    let n1 = m.px * m.ay - m.py * m.ax;
    let n2 = m.bx * m.ay - m.by * m.ax;

    if n1 % n2 != 0 {
        return None;
    }
    let b = n1 / n2;

    let n1 = m.px - b * m.bx;
    let n2 = m.ax;
    if n1 % n2 != 0 {
        return None;
    }
    let a = n1 / n2;

    Some(a * 3 + b)
}

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 para = content.trim_end().split("\n\n");

    let mut mvec: Vec<Machine> = Vec::new();
    for lines in para {
        let mut m: Machine = Machine {
            ax: 0,
            ay: 0,
            bx: 0,
            by: 0,
            px: 0,
            py: 0,
        };

        let (s, rest) = lines.split_once("X+").unwrap();
        let (s, rest) = rest.split_once(",").unwrap();
        m.ax = s.parse().unwrap();

        let (s, rest) = rest.split_once("Y+").unwrap();
        let (s, rest) = rest.split_once("\n").unwrap();
        m.ay = s.parse().unwrap();

        let (s, rest) = rest.split_once("X+").unwrap();
        let (s, rest) = rest.split_once(",").unwrap();
        m.bx = s.parse().unwrap();

        let (s, rest) = rest.split_once("Y+").unwrap();
        let (s, rest) = rest.split_once("\n").unwrap();
        m.by = s.parse().unwrap();

        let (s, rest) = rest.split_once("X=").unwrap();
        let (s, rest) = rest.split_once(",").unwrap();
        m.px = s.parse().unwrap();

        let (s, rest) = rest.split_once("Y=").unwrap();
        m.py = rest.parse().unwrap();

        mvec.push(m);
    }

    let mut res1 = 0;
    let mut res2 = 0;

    for m in &mvec {
        let p1 = solve(m);
        if let Some(val) = p1 {
            //println!("{}", val);
            res1 += val;
        }
        let m2: Machine = Machine {
            ax: m.ax,
            ay: m.ay,
            bx: m.bx,
            by: m.by,
            px: m.px + 10000000000000,
            py: m.py + 10000000000000,
        };

        let p2 = solve2(&m2);
        if let Some(val) = p2 {
            //println!("{}", val);
            res2 += val;
        }
    }

    println!("res1: {}", res1);
    println!("res2: {}", res2);
    assert_eq!(res1, 29187);
    assert_eq!(res2, 99968222587852);
}