day-02.rs (1276B)
1 fn main() {
2 let input = std::fs::read_to_string("input.txt")
3 .unwrap()
4 .trim()
5 .split(",")
6 .map(|op| op.parse::<usize>().unwrap())
7 .collect::<Vec<usize>>();
8 println!("Rust:");
9 println!("Part 1: {}", part_1(&input));
10 println!("Part 2: {}", part_2(&input));
11 }
12
13 fn computer(input: &Vec<usize>, noun: usize, verb: usize) -> usize {
14 let mut input: Vec<usize> = input.clone();
15 input[1] = noun;
16 input[2] = verb;
17
18 let mut pc = 0;
19 loop {
20 match input[pc] {
21 1 => {
22 let res_loc = input[pc + 3];
23 input[res_loc] = input[input[pc + 1]] + input[input[pc + 2]];
24 pc += 4;
25 }
26 2 => {
27 let res_loc = input[pc + 3];
28 input[res_loc] = input[input[pc + 1]] * input[input[pc + 2]];
29 pc += 4;
30 }
31 99 => break,
32 _ => {}
33 }
34 }
35
36 input[0]
37 }
38
39 fn part_1(input: &Vec<usize>) -> usize {
40 computer(&input, 12, 2)
41 }
42
43 fn part_2(input: &Vec<usize>) -> usize {
44 for noun in 0..=99 {
45 for verb in 0..=99 {
46 if computer(&input, noun, verb) == 19690720 {
47 return noun * 100 + verb;
48 }
49 }
50 }
51 0
52 }