-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.rs
188 lines (170 loc) · 5.57 KB
/
day24.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// vi: set shiftwidth=4 tabstop=4 expandtab:
use common::input::check_answer;
use common::input::collect_from_lines;
use common::input::get_answers;
use common::input::get_file_content;
use std::collections::HashMap;
use std::str::FromStr;
use std::time::Instant;
const INPUT_FILEPATH: &str = "../resources/year2021_day24_input.txt";
const ANSWERS_FILEPATH: &str = "../resources/year2021_day24_answer.txt";
type Int = i64;
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Clone)]
enum Instruction {
Input(String),
BinaryOp(String, String, fn(Int, Int) -> Int),
}
type InputContent = Vec<Instruction>;
impl FromStr for Instruction {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let chunks: Vec<&str> = s.split(' ').collect();
match chunks.first() {
None => (),
Some(&cmd) => match chunks.len() {
2 => {
let arg1 = chunks[1].to_owned();
if cmd == "inp" {
return Ok(Self::Input(arg1));
}
}
3 => {
let arg1 = chunks[1].to_owned();
let arg2 = chunks[2].to_owned();
match cmd {
"add" => {
return Ok(Self::BinaryOp(arg1, arg2, |a1, a2| a1 + a2));
}
"mul" => {
return Ok(Self::BinaryOp(arg1, arg2, |a1, a2| a1 * a2));
}
"div" => {
return Ok(Self::BinaryOp(arg1, arg2, |a1, a2| a1 / a2));
}
"mod" => {
return Ok(Self::BinaryOp(arg1, arg2, |a1, a2| a1 % a2));
}
"eql" => {
return Ok(Self::BinaryOp(arg1, arg2, |a1, a2| Int::from(a1 == a2)));
}
_ => (),
}
}
_ => (),
},
}
Err(())
}
}
fn eval_string(s: &str, env: &HashMap<String, Int>) -> Int {
s.parse::<Int>().unwrap_or_else(|_| *env.get(s).unwrap())
}
#[expect(dead_code, reason = "Only for testing")]
fn run_instructions_on_nb(instructions: &InputContent, input: u64) -> (Int, Int, Int, Int) {
if input == 0 {
return run_instructions(instructions, &[0]);
}
//dbg!(&input);
let mut input = input;
let mut digits: Vec<Int> = Vec::new();
while input > 0 {
digits.push((input % 10).try_into().unwrap());
input /= 10;
}
digits.reverse();
//dbg!(&digits);
run_instructions(instructions, &digits)
}
fn run_instructions(instructions: &InputContent, input: &[Int]) -> (Int, Int, Int, Int) {
let mut env: HashMap<String, Int> = HashMap::from([
("w".to_owned(), 0),
("x".to_owned(), 0),
("y".to_owned(), 0),
("z".to_owned(), 0),
]);
let mut input_iter = input.iter();
for ins in instructions {
match ins {
Instruction::Input(s1) => {
env.insert(s1.clone(), *input_iter.next().unwrap());
}
Instruction::BinaryOp(s1, s2, f) => {
let res = f(eval_string(s1, &env), eval_string(s2, &env));
env.insert(s1.clone(), res);
}
}
}
(env["w"], env["x"], env["y"], env["z"])
}
fn get_input_from_str(string: &str) -> InputContent {
collect_from_lines(string)
}
#[expect(clippy::missing_const_for_fn, reason = "Not implemented yet")]
fn part1(_instructions: &InputContent) -> Int {
// This may actually be an exercice of reverse engineering
0
}
#[expect(clippy::missing_const_for_fn, reason = "Not implemented yet")]
fn part2(_arg: &InputContent) -> Int {
0
}
fn main() {
let before = Instant::now();
let data = get_input_from_str(&get_file_content(INPUT_FILEPATH));
let (ans, ans2) = get_answers(ANSWERS_FILEPATH);
let solved = true;
let res = part1(&data);
check_answer(&res.to_string(), ans, solved);
let res2 = part2(&data);
check_answer(&res2.to_string(), ans2, solved);
println!("Elapsed time: {:.2?}", before.elapsed());
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE1: &str = "inp x
mul x -1";
const EXAMPLE2: &str = "inp z
inp x
mul z 3
eql z x";
const EXAMPLE3: &str = "inp w
add z w
mod z 2
div w 2
add y w
mod y 2
div w 2
add x w
mod x 2
div w 2
mod w 2";
#[test]
fn test_instruction_from_str() {
get_input_from_str(EXAMPLE1);
get_input_from_str(EXAMPLE2);
get_input_from_str(EXAMPLE3);
}
#[test]
fn test_run_instructions() {
let input1 = get_input_from_str(EXAMPLE1);
assert_eq!(run_instructions(&input1, &[3]), (0, -3, 0, 0));
assert_eq!(run_instructions_on_nb(&input1, 3), (0, -3, 0, 0));
let input2 = get_input_from_str(EXAMPLE2);
assert_eq!(run_instructions(&input2, &[3, 9]), (0, 9, 0, 1));
assert_eq!(run_instructions_on_nb(&input2, 39), (0, 9, 0, 1));
assert_eq!(run_instructions(&input2, &[3, 8]), (0, 8, 0, 0));
assert_eq!(run_instructions_on_nb(&input2, 38), (0, 8, 0, 0));
let input3 = get_input_from_str(EXAMPLE3);
assert_eq!(run_instructions(&input3, &[7]), (0, 1, 1, 1));
assert_eq!(run_instructions_on_nb(&input3, 7), (0, 1, 1, 1));
}
#[test]
fn test_part1() {
assert_eq!(part1(&get_input_from_str(EXAMPLE1)), 0);
}
#[test]
fn test_part2() {
assert_eq!(part2(&get_input_from_str(EXAMPLE2)), 0);
}
}