-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
63 lines (60 loc) · 1.53 KB
/
main.rs
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
fn execute(cur: &mut Vec<usize>) -> usize {
let mut pos = 0;
let len = cur.len();
while pos < len {
match cur[pos] {
1 => {
let a = cur[pos + 1];
let b = cur[pos + 2];
let r = cur[pos + 3];
cur[r] = cur[a] + cur[b];
pos += 4;
}
2 => {
let a = cur[pos + 1];
let b = cur[pos + 2];
let r = cur[pos + 3];
cur[r] = cur[a] * cur[b];
pos += 4;
}
99 => {
break;
}
_ => {
panic!("UNKNOWN OP {}", cur[pos]);
}
}
}
return cur[0];
}
fn part1(input: &str) -> usize {
let mut program = input
.split(',')
.map(|s| s.parse::<usize>().unwrap())
.collect::<Vec<_>>();
program[1] = 12;
program[2] = 2;
let result = execute(&mut program);
return result;
}
fn part2(input: &str) -> usize {
let program = input
.split(',')
.map(|s| s.parse::<usize>().unwrap())
.collect::<Vec<_>>();
for noun in 0..100 {
for verb in 0..100 {
let mut cur = program.clone();
cur[1] = noun;
cur[2] = verb;
if execute(&mut cur) == 19690720 {
return 100 * noun + verb;
}
}
}
0
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}