-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.rs
114 lines (104 loc) · 4.23 KB
/
day23.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
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
use std::collections::HashMap;
use std::fs;
pub(crate) fn day23() {
println!("{}", solve(fs::read_to_string("input/2016/day23/input.txt").unwrap(), 7));
println!("{}", solve(fs::read_to_string("input/2016/day23/input.txt").unwrap(), 12));
}
fn solve(input: String, a_initial: i32) -> i32 {
let mut registers: HashMap<String, i32> = HashMap::new();
registers.insert("a".to_string(), a_initial);
registers.insert("b".to_string(), 0);
registers.insert("c".to_string(), 0);
registers.insert("d".to_string(), 0);
let mut instructions: Vec<String> = input.lines().map(|line| line.to_string()).collect();
let mut i = 0;
while i < instructions.len() as i32 {
// println!("a:{:3} b:{:3} c:{:3} d:{:3}", registers.get(&"a".to_string()).unwrap(), registers.get(&"b".to_string()).unwrap(), registers.get(&"c".to_string()).unwrap(), registers.get(&"d".to_string()).unwrap());
let line = instructions[i as usize].clone();
let (instr, args) = line.split_once(" ").unwrap();
match instr {
"cpy" => {
let (what, reg) = args.split_once(" ").unwrap();
let val = if let Some(x) = registers.get(what) {
*x
} else {
what.parse::<i32>().unwrap()
};
registers.insert(reg.to_string(), val);
i += 1;
}
"inc" => {
*registers.entry(args.to_string()).or_insert(0) += 1;
i += 1;
}
"dec" => {
*registers.entry(args.to_string()).or_insert(0) -= 1;
i += 1;
}
"jnz" => {
let (test, jump) = args.split_once(" ").unwrap();
let test_val = if let Some(x) = registers.get(test) {
*x
} else {
test.parse::<i32>().unwrap()
};
if test_val != 0 {
let jump_val = if let Some(x) = registers.get(jump) {
*x
} else {
jump.parse::<i32>().unwrap()
};
i += jump_val;
} else {
i += 1;
}
}
"tgl" => {
let what = *registers.entry(args.to_string()).or_insert(0);
if i + what < 0 || i + what >= instructions.len() as i32 {
i += 1;
continue
}
let mut new_instructions = instructions.clone();
let to_toggle = instructions[(i + what) as usize].clone();
let (to_toggle_instr, to_toggle_args) = to_toggle.split_once(" ").unwrap();
match to_toggle_instr {
"cpy" => {
new_instructions[(i + what) as usize] = format!("jnz {}", to_toggle_args);
}
"inc" => {
new_instructions[(i + what) as usize] = format!("dec {}", to_toggle_args);
}
"dec" => {
new_instructions[(i + what) as usize] = format!("inc {}", to_toggle_args);
}
"jnz" => {
new_instructions[(i + what) as usize] = format!("cpy {}", to_toggle_args);
}
"tgl" => {
new_instructions[(i + what) as usize] = format!("inc {}", to_toggle_args);
}
_ => { panic!("unknown instruction {}", instr) }
}
instructions = new_instructions;
i += 1;
}
_ => { panic!("unknown instruction {}", instr) }
}
}
*registers.get("a").unwrap()
}
#[cfg(test)]
mod day23_tests {
use std::fs;
use crate::y2016::day23::{solve};
#[test]
fn test_works() {
assert_eq!(3, solve(fs::read_to_string("input/2016/day23/test.txt").unwrap(), 7));
}
#[test]
fn input_works() {
assert_eq!(14065, solve(fs::read_to_string("input/2016/day23/input.txt").unwrap(), 7));
assert_eq!(0, solve(fs::read_to_string("input/2016/day23/input.txt").unwrap(), 12));
}
}