mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Refactor computer to own its program
This commit is contained in:
parent
21c50bd8e8
commit
af694da6ce
3 changed files with 29 additions and 18 deletions
|
@ -5,13 +5,11 @@ fn main() -> io::Result<()> {
|
|||
let input = fs::read_to_string("input/day5.txt")?;
|
||||
let data = input::read_separated_line(',', &input)?;
|
||||
|
||||
let mut program = data.clone();
|
||||
let mut computer = computer::Computer::new(&mut program, vec![1]);
|
||||
let mut computer = computer::Computer::new(data.clone(), vec![1]);
|
||||
computer.run(None, None);
|
||||
println!("Part 1: {:?}", computer.output());
|
||||
|
||||
let mut program = data.clone();
|
||||
let mut computer = computer::Computer::new(&mut program, vec![5]);
|
||||
let mut computer = computer::Computer::new(data.clone(), vec![5]);
|
||||
computer.run(None, None);
|
||||
println!("Part 2: {:?}", computer.output());
|
||||
|
||||
|
|
|
@ -8,14 +8,24 @@ fn main() -> io::Result<()> {
|
|||
let source = fs::read_to_string("input/day7.txt")?;
|
||||
let data = input::read_separated_line(',', &source)?;
|
||||
|
||||
let max = phase_settings()
|
||||
part1(data.clone());
|
||||
// part2(data);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn part1(data: Vec<i32>) {
|
||||
let mut elements = [0i32; AMPLIFIERS];
|
||||
for i in 0i32..=4 {
|
||||
elements[i as usize] = i;
|
||||
}
|
||||
let max = phase_settings(elements)
|
||||
.iter()
|
||||
.map(|settings| {
|
||||
let mut output = 0;
|
||||
for phase_setting in settings.iter() {
|
||||
let input = vec![output, *phase_setting];
|
||||
let mut program = data.clone();
|
||||
let mut computer = computer::Computer::new(&mut program, input);
|
||||
let mut computer = computer::Computer::new(data.clone(), input);
|
||||
computer.run(None, None);
|
||||
output = computer.output()[0];
|
||||
}
|
||||
|
@ -24,17 +34,20 @@ fn main() -> io::Result<()> {
|
|||
.max()
|
||||
.unwrap();
|
||||
println!("Part 1: {}", max);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn phase_settings() -> Vec<[i32; AMPLIFIERS]> {
|
||||
fn part2(data: Vec<i32>) {
|
||||
let mut elements = [0i32; AMPLIFIERS];
|
||||
for i in 5i32..=9 {
|
||||
elements[i as usize - 5] = i;
|
||||
}
|
||||
|
||||
// println!("Part 2: {}", max);
|
||||
}
|
||||
|
||||
fn phase_settings(mut elements: [i32; AMPLIFIERS]) -> Vec<[i32; AMPLIFIERS]> {
|
||||
let mut permutations = Vec::new();
|
||||
let mut indexes = [0i32; AMPLIFIERS];
|
||||
let mut elements = [0i32; AMPLIFIERS];
|
||||
for i in 0i32..=4 {
|
||||
elements[i as usize] = i;
|
||||
}
|
||||
|
||||
permutations.push(elements);
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@ enum Mode {
|
|||
Address,
|
||||
}
|
||||
|
||||
pub struct Computer<'a> {
|
||||
memory: &'a mut [i32],
|
||||
pub struct Computer {
|
||||
memory: Vec<i32>,
|
||||
input: Vec<i32>,
|
||||
output: Vec<i32>,
|
||||
}
|
||||
|
@ -64,8 +64,8 @@ fn decode(mut instruction: i32) -> Instruction {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Computer<'a> {
|
||||
pub fn new(memory: &'a mut [i32], input: Vec<i32>) -> Self {
|
||||
impl Computer {
|
||||
pub fn new(memory: Vec<i32>, input: Vec<i32>) -> Self {
|
||||
Computer {
|
||||
memory,
|
||||
input,
|
||||
|
|
Loading…
Reference in a new issue