Always start at 0.
This commit is contained in:
@@ -83,7 +83,7 @@ impl Tile {
|
|||||||
|
|
||||||
impl Arcade {
|
impl Arcade {
|
||||||
pub fn new(width: usize, height: usize, cheat: bool, logic_file: &str) -> Arcade {
|
pub fn new(width: usize, height: usize, cheat: bool, logic_file: &str) -> Arcade {
|
||||||
let mut logic = Computer::load(logic_file, 0);
|
let mut logic = Computer::load(logic_file);
|
||||||
let ( mysend, mut corecv) = channel();
|
let ( mysend, mut corecv) = channel();
|
||||||
let (mut cosend, mut myrecv) = channel();
|
let (mut cosend, mut myrecv) = channel();
|
||||||
let (mut upsend, uprecv) = channel();
|
let (mut upsend, uprecv) = channel();
|
||||||
|
|||||||
12
src/args.rs
12
src/args.rs
@@ -57,12 +57,6 @@ impl Command {
|
|||||||
)
|
)
|
||||||
.subcommand(SubCommand::with_name("compute")
|
.subcommand(SubCommand::with_name("compute")
|
||||||
.about("run the given computer")
|
.about("run the given computer")
|
||||||
.arg(Arg::with_name("START_POSITION")
|
|
||||||
.short("p")
|
|
||||||
.long("start-position")
|
|
||||||
.help("The starting position to execute from.")
|
|
||||||
.default_value("0")
|
|
||||||
.validator(is_number))
|
|
||||||
.arg(Arg::with_name("COMPUTER")
|
.arg(Arg::with_name("COMPUTER")
|
||||||
.index(1)
|
.index(1)
|
||||||
.help("The computer to run.")
|
.help("The computer to run.")
|
||||||
@@ -140,9 +134,7 @@ impl Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(problem2) = matches.subcommand_matches("compute") {
|
if let Some(problem2) = matches.subcommand_matches("compute") {
|
||||||
let start_pos_str = problem2.value_of("START_POSITION").unwrap();
|
let computer = Computer::load(problem2.value_of("COMPUTER").unwrap());
|
||||||
let start_pos = usize::from_str_radix(&start_pos_str, 10).unwrap();
|
|
||||||
let computer = Computer::load(problem2.value_of("COMPUTER").unwrap(), start_pos);
|
|
||||||
return Command::RunComputer(computer);
|
return Command::RunComputer(computer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,7 +171,7 @@ impl Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(problem6) = matches.subcommand_matches("amplify") {
|
if let Some(problem6) = matches.subcommand_matches("amplify") {
|
||||||
let computer = Computer::load(problem6.value_of("COMPUTER").unwrap(), 0);
|
let computer = Computer::load(problem6.value_of("COMPUTER").unwrap());
|
||||||
return Command::Amplify(computer);
|
return Command::Amplify(computer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,13 +43,13 @@ pub struct Computer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Computer {
|
impl Computer {
|
||||||
pub fn load(path: &str, position: usize) -> Computer {
|
pub fn load(path: &str) -> Computer {
|
||||||
let byte_buffer = fs::read(path).unwrap();
|
let byte_buffer = fs::read(path).unwrap();
|
||||||
let char_buffer = str::from_utf8(&byte_buffer).unwrap();
|
let char_buffer = str::from_utf8(&byte_buffer).unwrap();
|
||||||
Computer::from_string(&char_buffer, position)
|
Computer::from_string(&char_buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_string(char_buffer: &str, position: usize) -> Computer {
|
fn from_string(char_buffer: &str) -> Computer {
|
||||||
let mut memory = vec![];
|
let mut memory = vec![];
|
||||||
let mut char_iter = char_buffer.chars().peekable();
|
let mut char_iter = char_buffer.chars().peekable();
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ impl Computer {
|
|||||||
memory.push(next);
|
memory.push(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
Computer{ memory, position, relative_base: 0, done: false }
|
Computer{ memory, position: 0, relative_base: 0, done: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show(&self) {
|
pub fn show(&self) {
|
||||||
@@ -320,40 +320,40 @@ fn run_computer(mut computer: Computer, inputs: Vec<i64>, targets: Vec<i64>) {
|
|||||||
fn test_examples() {
|
fn test_examples() {
|
||||||
let (mut deadsend, mut deadrecv) = channel();
|
let (mut deadsend, mut deadrecv) = channel();
|
||||||
|
|
||||||
let mut example1 = Computer::from_string("1,0,0,0,99", 0);
|
let mut example1 = Computer::from_string("1,0,0,0,99");
|
||||||
let answer1 = Computer{ memory: vec![2,0,0,0,99], position: 4, relative_base: 0, done: false };
|
let answer1 = Computer{ memory: vec![2,0,0,0,99], position: 4, relative_base: 0, done: false };
|
||||||
example1.step(&mut deadrecv, &mut deadsend);
|
example1.step(&mut deadrecv, &mut deadsend);
|
||||||
assert_eq!(example1, answer1);
|
assert_eq!(example1, answer1);
|
||||||
|
|
||||||
let mut example2 = Computer::from_string("2,3,0,3,99", 0);
|
let mut example2 = Computer::from_string("2,3,0,3,99");
|
||||||
let answer2 = Computer{ memory: vec![2,3,0,6,99], position: 4, relative_base: 0, done: false };
|
let answer2 = Computer{ memory: vec![2,3,0,6,99], position: 4, relative_base: 0, done: false };
|
||||||
example2.step(&mut deadrecv, &mut deadsend);
|
example2.step(&mut deadrecv, &mut deadsend);
|
||||||
assert_eq!(example2, answer2);
|
assert_eq!(example2, answer2);
|
||||||
|
|
||||||
let mut example3 = Computer::from_string("2,4,4,5,99,0", 0);
|
let mut example3 = Computer::from_string("2,4,4,5,99,0");
|
||||||
let answer3 = Computer{ memory: vec![2,4,4,5,99,9801], position: 4, relative_base: 0, done: false };
|
let answer3 = Computer{ memory: vec![2,4,4,5,99,9801], position: 4, relative_base: 0, done: false };
|
||||||
example3.step(&mut deadrecv, &mut deadsend);
|
example3.step(&mut deadrecv, &mut deadsend);
|
||||||
assert_eq!(example3, answer3);
|
assert_eq!(example3, answer3);
|
||||||
|
|
||||||
let mut example4 = Computer::from_string("1,1,1,4,99,5,6,0,99", 0);
|
let mut example4 = Computer::from_string("1,1,1,4,99,5,6,0,99");
|
||||||
let answer4 = Computer{ memory: vec![30,1,1,4,2,5,6,0,99], position: 8, relative_base: 0, done: true };
|
let answer4 = Computer{ memory: vec![30,1,1,4,2,5,6,0,99], position: 8, relative_base: 0, done: true };
|
||||||
example4.run(&mut deadrecv, &mut deadsend);
|
example4.run(&mut deadrecv, &mut deadsend);
|
||||||
assert_eq!(example4, answer4);
|
assert_eq!(example4, answer4);
|
||||||
assert!(example4.halted());
|
assert!(example4.halted());
|
||||||
|
|
||||||
let mut example5 = Computer::from_string("1002,4,3,4,33", 0);
|
let mut example5 = Computer::from_string("1002,4,3,4,33");
|
||||||
let answer5 = Computer{ memory: vec![1002,4,3,4,99], position: 4, relative_base: 0, done: true };
|
let answer5 = Computer{ memory: vec![1002,4,3,4,99], position: 4, relative_base: 0, done: true };
|
||||||
example5.run(&mut deadrecv, &mut deadsend);
|
example5.run(&mut deadrecv, &mut deadsend);
|
||||||
assert_eq!(example5, answer5);
|
assert_eq!(example5, answer5);
|
||||||
assert!(example5.halted());
|
assert!(example5.halted());
|
||||||
|
|
||||||
let mut example6 = Computer::from_string("1101,100,-1,4,0", 0);
|
let mut example6 = Computer::from_string("1101,100,-1,4,0");
|
||||||
let answer6 = Computer{ memory: vec![1101,100,-1,4,99], position: 4, relative_base: 0, done: true };
|
let answer6 = Computer{ memory: vec![1101,100,-1,4,99], position: 4, relative_base: 0, done: true };
|
||||||
example6.run(&mut deadrecv, &mut deadsend);
|
example6.run(&mut deadrecv, &mut deadsend);
|
||||||
assert_eq!(example6, answer6);
|
assert_eq!(example6, answer6);
|
||||||
assert!(example6.halted());
|
assert!(example6.halted());
|
||||||
|
|
||||||
let mut day5a = Computer::load("inputs/day5", 0);
|
let mut day5a = Computer::load("inputs/day5");
|
||||||
let target = vec![0,0,0,0,0,0,0,0,0,7_259_358];
|
let target = vec![0,0,0,0,0,0,0,0,0,7_259_358];
|
||||||
let ( mysend, mut corecv) = channel();
|
let ( mysend, mut corecv) = channel();
|
||||||
let (mut cosend, myrecv) = channel();
|
let (mut cosend, myrecv) = channel();
|
||||||
@@ -385,13 +385,13 @@ fn test_examples() {
|
|||||||
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
|
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
|
||||||
vec![192], vec![1001]);
|
vec![192], vec![1001]);
|
||||||
|
|
||||||
let example7a = Computer::from_string("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0", 0);
|
let example7a = Computer::from_string("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0");
|
||||||
let result7a = example7a.serialize(vec![4,3,2,1,0]);
|
let result7a = example7a.serialize(vec![4,3,2,1,0]);
|
||||||
assert_eq!(43210, result7a);
|
assert_eq!(43210, result7a);
|
||||||
let example7b = Computer::from_string("3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0", 0);
|
let example7b = Computer::from_string("3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0");
|
||||||
let result7b = example7b.serialize(vec![0,1,2,3,4]);
|
let result7b = example7b.serialize(vec![0,1,2,3,4]);
|
||||||
assert_eq!(54321, result7b);
|
assert_eq!(54321, result7b);
|
||||||
let example7c = Computer::from_string("3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0", 0);
|
let example7c = Computer::from_string("3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0");
|
||||||
let target7c = 65210;
|
let target7c = 65210;
|
||||||
let result7c = example7c.serialize(vec![1,0,4,3,2]);
|
let result7c = example7c.serialize(vec![1,0,4,3,2]);
|
||||||
assert_eq!(target7c, result7c);
|
assert_eq!(target7c, result7c);
|
||||||
@@ -399,17 +399,17 @@ fn test_examples() {
|
|||||||
assert_eq!(target7c, result7c2);
|
assert_eq!(target7c, result7c2);
|
||||||
assert_eq!(result7c2, 65210);
|
assert_eq!(result7c2, 65210);
|
||||||
assert_eq!(example7c.find_best_signal(0..5, |x| example7c.serialize(x)).1, vec![1,0,4,3,2]);
|
assert_eq!(example7c.find_best_signal(0..5, |x| example7c.serialize(x)).1, vec![1,0,4,3,2]);
|
||||||
let day7a = Computer::load("inputs/day7", 0);
|
let day7a = Computer::load("inputs/day7");
|
||||||
let (day7score, day7settings) = day7a.find_best_signal(0..5, |x| day7a.serialize(x));
|
let (day7score, day7settings) = day7a.find_best_signal(0..5, |x| day7a.serialize(x));
|
||||||
assert_eq!(day7score, 206580);
|
assert_eq!(day7score, 206580);
|
||||||
assert_eq!(day7settings, vec![2,0,1,4,3]);
|
assert_eq!(day7settings, vec![2,0,1,4,3]);
|
||||||
|
|
||||||
let example7e = Computer::from_string("3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5", 0);
|
let example7e = Computer::from_string("3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5");
|
||||||
assert_eq!(139629729, example7e.amplifier(vec![9,8,7,6,5]));
|
assert_eq!(139629729, example7e.amplifier(vec![9,8,7,6,5]));
|
||||||
let (example7es, example7et) = example7e.find_best_signal(5..10, |x| example7e.amplifier(x));
|
let (example7es, example7et) = example7e.find_best_signal(5..10, |x| example7e.amplifier(x));
|
||||||
assert_eq!(139629729, example7es);
|
assert_eq!(139629729, example7es);
|
||||||
assert_eq!(vec![9,8,7,6,5], example7et);
|
assert_eq!(vec![9,8,7,6,5], example7et);
|
||||||
let example7f = Computer::from_string("3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10", 0);
|
let example7f = Computer::from_string("3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10");
|
||||||
assert_eq!(18216, example7f.amplifier(vec![9,7,8,5,6]));
|
assert_eq!(18216, example7f.amplifier(vec![9,7,8,5,6]));
|
||||||
let (example7fs, example7ft) = example7f.find_best_signal(5..10, |x| example7f.amplifier(x));
|
let (example7fs, example7ft) = example7f.find_best_signal(5..10, |x| example7f.amplifier(x));
|
||||||
assert_eq!(18216, example7fs);
|
assert_eq!(18216, example7fs);
|
||||||
@@ -425,6 +425,6 @@ fn test_examples() {
|
|||||||
vec![],
|
vec![],
|
||||||
vec![1125899906842624]);
|
vec![1125899906842624]);
|
||||||
|
|
||||||
run_computer(Computer::load("inputs/day9", 0), vec![1], vec![3063082071]);
|
run_computer(Computer::load("inputs/day9"), vec![1], vec![3063082071]);
|
||||||
run_computer(Computer::load("inputs/day9", 0), vec![2], vec![81348]);
|
run_computer(Computer::load("inputs/day9"), vec![2], vec![81348]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ impl MoveResult {
|
|||||||
|
|
||||||
impl RepairSearch {
|
impl RepairSearch {
|
||||||
fn new(f: &str) -> RepairSearch {
|
fn new(f: &str) -> RepairSearch {
|
||||||
let computer = Computer::load(f, 0);
|
let computer = Computer::load(f);
|
||||||
RepairSearch { computer }
|
RepairSearch { computer }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +213,7 @@ impl Room {
|
|||||||
fn new(width: usize, height: usize, f: &str) -> Room {
|
fn new(width: usize, height: usize, f: &str) -> Room {
|
||||||
let (mut mysend, mut corecv) = channel();
|
let (mut mysend, mut corecv) = channel();
|
||||||
let (mut cosend, mut myrecv) = channel();
|
let (mut cosend, mut myrecv) = channel();
|
||||||
let mut my_computer = Computer::load(f, 0);
|
let mut my_computer = Computer::load(f);
|
||||||
let mut layout = Vec::with_capacity(width * height);
|
let mut layout = Vec::with_capacity(width * height);
|
||||||
|
|
||||||
layout.resize(width * height, Tile::Unknown);
|
layout.resize(width * height, Tile::Unknown);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ struct HullGrid {
|
|||||||
|
|
||||||
impl HullGrid {
|
impl HullGrid {
|
||||||
fn new(width: u32, height: u32, computer_path: &str) -> HullGrid {
|
fn new(width: u32, height: u32, computer_path: &str) -> HullGrid {
|
||||||
let mut init_computer = Computer::load(computer_path, 0);
|
let mut init_computer = Computer::load(computer_path);
|
||||||
let ( mysend, mut corecv) = channel();
|
let ( mysend, mut corecv) = channel();
|
||||||
let (mut cosend, myrecv) = channel();
|
let (mut cosend, myrecv) = channel();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user