diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 154 |
1 files changed, 73 insertions, 81 deletions
diff --git a/src/main.rs b/src/main.rs index 7c490af..d62c695 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ struct Line { } impl Point { - fn distance_origin(&self) -> i32 { + fn distance_origin(self) -> i32 { self.x.abs() + self.y.abs() } } @@ -25,28 +25,26 @@ impl Line { } fn length(&self) -> i32 { - match self.horizontal() { - true => (self.p1.x - self.p2.x).abs(), - false => (self.p1.y - self.p2.y).abs(), + if self.horizontal() { + (self.p1.x - self.p2.x).abs() + } else { + (self.p1.y - self.p2.y).abs() } } - fn contains_point(&self, point : &Point) -> bool { - if self.horizontal() && self.p1.y != point.y { - return false; - } else if !self.horizontal() && self.p1.x != point.x { - return false; - } - - match self.horizontal() { - true => { - self.p1.x <= point.x && self.p2.x >= point.x - || self.p2.x <= point.x && self.p1.x >= point.x + fn contains_point(&self, point : Point) -> bool { + if self.horizontal() { + if self.p1.y != point.y { + return false; } - false => { - self.p1.y <= point.y && self.p2.y >= point.y - || self.p2.y <= point.y && self.p1.y >= point.y + self.p1.x <= point.x && self.p2.x >= point.x + || self.p2.x <= point.x && self.p1.x >= point.x + } else { + if self.p1.x != point.x { + return false; } + self.p1.y <= point.y && self.p2.y >= point.y + || self.p2.y <= point.y && self.p1.y >= point.y } } @@ -55,12 +53,13 @@ impl Line { return None; } - let point = match self.horizontal() { - true => Point { x : other.p1.x, y : self.p1.y }, - false => Point { x : self.p1.x, y : other.p1.y }, + let point = if self.horizontal() { + Point { x : other.p1.x, y : self.p1.y } + } else { + Point { x : self.p1.x, y : other.p1.y } }; - if self.contains_point(&point) && other.contains_point(&point) { + if self.contains_point(point) && other.contains_point(point) { Some(point) } else { None @@ -92,38 +91,38 @@ fn required_fuel_with_fuel(mass : u32) -> u32 { fn day1() { let input = read_file("input1"); let sum : u32 = input.split('\n') - .filter(|x| !x.is_empty()) - .map(|x| x.parse::<u32>().unwrap()) - .map(|x| required_fuel(x)) - .sum(); - println!("1a: {}", sum); - - let sum2 : u32 = input.split('\n') .filter(|x| !x.is_empty()) .map(|x| x.parse::<u32>().unwrap()) - .map(|x| required_fuel_with_fuel(x)) + .map(required_fuel) .sum(); + println!("1a: {}", sum); + + let sum2 : u32 = input.split('\n') + .filter(|x| !x.is_empty()) + .map(|x| x.parse::<u32>().unwrap()) + .map(required_fuel_with_fuel) + .sum(); println!("1b: {}", sum2); } -fn get_operands(mem : &Vec<i32>, inputs : Vec<i32>, mode : i32) -> Vec<i32> { +fn get_operands(mem : &[i32], inputs : &[i32], mode : i32) -> Vec<i32> { let mut tmp = mode; let mut ops = Vec::new(); for i in inputs { let m = tmp % 10; tmp /= 10; match m { - 0 => ops.push(mem[i as usize]), - 1 => ops.push(i), + 0 => ops.push(mem[*i as usize]), + 1 => ops.push(*i), _ => panic!("invalid mode") } } ops } -fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) -> Vec<i32> { +fn run_program_io(input : &[i32], stdin : &[&str], stdout : &mut Vec<i32>) -> Vec<i32> { let mut stdin_iter = stdin.iter(); - let mut mem = input.clone(); + let mut mem = input.to_vec(); let mut ip = 0; loop { let instruction = mem[ip]; @@ -132,13 +131,13 @@ fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) match opcode { 1 => { /* add */ - let ops = get_operands(&mem, mem[ip+1..ip+3].to_vec(), mode); + let ops = get_operands(&mem, &mem[ip+1..=ip+2], mode); let dst = mem[ip+3] as usize; mem[dst] = ops[0] + ops[1]; ip += 4; } 2 => { /* multiply */ - let ops = get_operands(&mem, mem[ip+1..ip+3].to_vec(), mode); + let ops = get_operands(&mem, &mem[ip+1..=ip+2], mode); let dst = mem[ip+3] as usize; mem[dst] = ops[0] * ops[1]; ip += 4; @@ -150,12 +149,12 @@ fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) ip += 2; } 4 => { /* write stdout */ - let ops = get_operands(&mem, mem[ip+1..ip+2].to_vec(), mode); + let ops = get_operands(&mem, &[mem[ip+1]], mode); stdout.push(ops[0]); ip += 2; } 5 => { /* jump-if-true */ - let ops = get_operands(&mem, mem[ip+1..ip+3].to_vec(), mode); + let ops = get_operands(&mem, &mem[ip+1..=ip+2], mode); if ops[0] != 0 { ip = ops[1] as usize; } else { @@ -163,7 +162,7 @@ fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) } } 6 => { /* jump-if-false */ - let ops = get_operands(&mem, mem[ip+1..ip+3].to_vec(), mode); + let ops = get_operands(&mem, &mem[ip+1..=ip+2], mode); if ops[0] == 0 { ip = ops[1] as usize; } else { @@ -171,7 +170,7 @@ fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) } } 7 => { /* less-than */ - let ops = get_operands(&mem, mem[ip+1..ip+3].to_vec(), mode); + let ops = get_operands(&mem, &mem[ip+1..=ip+2], mode); let dst = mem[ip+3] as usize; if ops[0] < ops[1] { mem[dst] = 1; @@ -181,7 +180,7 @@ fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) ip += 4; } 8 => { /* equals */ - let ops = get_operands(&mem, mem[ip+1..ip+3].to_vec(), mode); + let ops = get_operands(&mem, &mem[ip+1..=ip+2], mode); let dst = mem[ip+3] as usize; if ops[0] == ops[1] { mem[dst] = 1; @@ -197,15 +196,13 @@ fn run_program_io(input : Vec<i32>, stdin : &Vec<&str>, stdout : &mut Vec<i32>) mem } -fn run_program(input : Vec<i32>) -> Vec<i32> { - let stdin = Vec::new(); - let mut stdout = Vec::new(); - run_program_io(input, &stdin, &mut stdout) +fn run_program(input : &[i32]) -> Vec<i32> { + run_program_io(input, &[], &mut Vec::new()) } fn day2() { - let mut input = read_file("input2"); - input.pop(); + let input = read_file("input2"); + let input = input.trim_end(); let mut program : Vec<i32> = input.split(',') .map(|x| x.parse::<i32>().unwrap()) .collect(); @@ -213,16 +210,16 @@ fn day2() { program[1] = 12; program[2] = 2; - let mem = run_program(program.clone()); + let mem = run_program(&program); println!("2a: {}", mem[0]); - for noun in 0..99 { - for verb in 0..99 { + for noun in 0..=99 { + for verb in 0..=99 { program[1] = noun; program[2] = verb; - let mem = run_program(program.clone()); + let mem = run_program(&program); - if mem[0] == 19690720 { + if mem[0] == 19_690_720 { println!("2b: {}", 100 * noun + verb); return; } @@ -252,7 +249,7 @@ fn parse_lines(wire : &str) -> Vec<Line> { lines } -fn intersections(lines1 : &Vec<Line>, lines2 : &Vec<Line>) -> Vec<Point> { +fn intersections(lines1 : &[Line], lines2 : &[Line]) -> Vec<Point> { let mut points = Vec::new(); for line1 in lines1 { for line2 in lines2 { @@ -270,14 +267,14 @@ fn closest_intersection(wire1 : &str, wire2 : &str) -> i32 { let lines2 = parse_lines(wire2); intersections(&lines1, &lines2).iter() .map(|p| p.distance_origin()) - .filter(|&x| x > 0) + .filter(|x| x.is_positive()) .min().unwrap() } -fn delay_to_point(lines : &Vec<Line>, p : &Point) -> i32 { +fn delay_to_point(lines : &[Line], p : Point) -> i32 { let mut delay = 0; for line in lines { - if line.contains_point(&p) { + if line.contains_point(p) { delay += (line.p1.distance_origin() - p.distance_origin()).abs(); break; } @@ -293,14 +290,14 @@ fn min_delay(wire1 : &str, wire2 : &str) -> i32 { let points = intersections(&lines1, &lines2); points.iter() - .map(|p| delay_to_point(&lines1, &p) + delay_to_point(&lines2, &p)) - .filter(|&d| d > 0) + .map(|p| delay_to_point(&lines1, *p) + delay_to_point(&lines2, *p)) + .filter(|d| d.is_positive()) .min().unwrap() } fn day3() { - let mut input = read_file("input3"); - input.pop(); + let input = read_file("input3"); + let input = input.trim_end(); let wires : Vec<&str> = input.split('\n').collect(); let distance = closest_intersection(wires[0], wires[1]); @@ -367,33 +364,31 @@ fn valid_password2(input : u32) -> bool { } fn day4() { - let min = 264360; - let max = 746325; + let min = 264_360; + let max = 746_325; - let count = (min..max+1).filter(|&pw| valid_password(pw)).count(); + let count = (min..=max).filter(|&pw| valid_password(pw)).count(); println!("4a: {}", count); - let count = (min..max+1).filter(|&pw| valid_password2(pw)).count(); + let count = (min..=max).filter(|&pw| valid_password2(pw)).count(); println!("4a: {}", count); } fn day5() { - let mut input = read_file("input5"); - input.pop(); + let input = read_file("input5"); + let input = input.trim_end(); let program : Vec<i32> = input.split(',') .map(|x| x.parse::<i32>().unwrap()) .collect(); let mut stdout = Vec::new(); - let stdin = vec!["1"]; - run_program_io(program.clone(), &stdin, &mut stdout); + run_program_io(&program, &["1"], &mut stdout); println!("5a:"); for val in stdout { println!("{}", val); } let mut stdout = Vec::new(); - let stdin = vec!["5"]; - run_program_io(program.clone(), &stdin, &mut stdout); + run_program_io(&program, &["5"], &mut stdout); println!("5b:"); for val in stdout { println!("{}", val); @@ -422,10 +417,10 @@ mod tests { #[test] fn test_day2() { - assert_eq!(run_program(vec!(1,0,0,0,99)), vec!(2,0,0,0,99)); - assert_eq!(run_program(vec!(2,3,0,3,99)), vec!(2,3,0,6,99)); - assert_eq!(run_program(vec!(2,4,4,5,99,0)), vec!(2,4,4,5,99,9801)); - assert_eq!(run_program(vec!(1,1,1,4,99,5,6,0,99)), vec!(30,1,1,4,2,5,6,0,99)); + assert_eq!(run_program(&[1,0,0,0,99]), vec!(2,0,0,0,99)); + assert_eq!(run_program(&[2,3,0,3,99]), vec!(2,3,0,6,99)); + assert_eq!(run_program(&[2,4,4,5,99,0]), vec!(2,4,4,5,99,9801)); + assert_eq!(run_program(&[1,1,1,4,99,5,6,0,99]), vec!(30,1,1,4,2,5,6,0,99)); } #[test] @@ -461,19 +456,16 @@ mod tests { fn test_day5() { let program = vec![3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]; - let stdin = vec!["4"]; let mut stdout = Vec::new(); - run_program_io(program.clone(), &stdin, &mut stdout); + run_program_io(&program, &["4"], &mut stdout); assert_eq!(stdout[0], 999); - let stdin = vec!["8"]; let mut stdout = Vec::new(); - run_program_io(program.clone(), &stdin, &mut stdout); + run_program_io(&program, &["8"], &mut stdout); assert_eq!(stdout[0], 1000); - let stdin = vec!["42"]; let mut stdout = Vec::new(); - run_program_io(program.clone(), &stdin, &mut stdout); + run_program_io(&program, &["42"], &mut stdout); assert_eq!(stdout[0], 1001); } } |
