1
0
Fork 0
advent-of-code/2018/day3/src/main.rs

112 lines
3.2 KiB
Rust
Raw Normal View History

2018-12-04 15:46:29 -05:00
use std::fs::File;
use std::io::{BufRead, BufReader};
fn get_coords_from_line(line: &String) -> (usize, usize, usize, usize) {
let first_pieces: Vec<&str> = line.as_str().split(" @ ").collect();
let _piece_number = first_pieces.get(0).unwrap();
let piece_dims: Vec<&str> = first_pieces.get(1).unwrap().split(": ").collect();
let top_left_pieces: Vec<&str> = piece_dims.get(0).unwrap().split(",").collect();
let top_x = top_left_pieces.get(0).unwrap().parse::<usize>().unwrap();
let top_y = top_left_pieces.get(1).unwrap().parse::<usize>().unwrap();
let bottom_right_pieces: Vec<&str> = piece_dims.get(1).unwrap().split("x").collect();
2018-12-13 15:00:47 -05:00
let bottom_x = top_x
+ bottom_right_pieces
.get(0)
.unwrap()
.parse::<usize>()
.unwrap();
let bottom_y = top_y
+ bottom_right_pieces
.get(1)
.unwrap()
.parse::<usize>()
.unwrap();
2018-12-04 15:46:29 -05:00
(top_x, top_y, bottom_x, bottom_y)
}
fn build_map(lines: &Vec<String>) -> [[u8; 1000]; 1000] {
let mut map = [[0u8; 1000]; 1000];
for line in lines.iter() {
let (top_x, top_y, bottom_x, bottom_y) = get_coords_from_line(line);
for i in top_x..bottom_x {
for j in top_y..bottom_y {
map[i][j] += 1;
}
}
}
map
}
2019-01-03 22:06:21 -05:00
fn calc_overlap(map: &[[u8; 1000]; 1000]) -> i32 {
2018-12-04 15:46:29 -05:00
let mut squares = 0;
for row in map.iter() {
for col in row.iter() {
if *col > 1 {
squares += 1;
}
}
}
2019-01-03 22:06:21 -05:00
squares
2018-12-04 15:46:29 -05:00
}
2019-01-03 22:06:21 -05:00
fn find_non_overlapped(lines: &Vec<String>, map: &[[u8; 1000]; 1000]) -> Option<String> {
2018-12-04 15:46:29 -05:00
for line in lines.iter() {
let (top_x, top_y, bottom_x, bottom_y) = get_coords_from_line(line);
let mut all_ones = true;
for i in top_x..bottom_x {
for j in top_y..bottom_y {
if map[i][j] != 1 {
all_ones = false;
break;
}
}
if !all_ones {
break;
}
}
if all_ones {
2019-01-03 22:06:21 -05:00
return Some(line.to_string());
2018-12-04 15:46:29 -05:00
}
}
2019-01-03 22:06:21 -05:00
None
2018-12-04 15:46:29 -05:00
}
fn main() {
let lines: Vec<String> = BufReader::new(File::open("input").unwrap())
.lines()
.map(|line| line.unwrap())
.collect();
let map = build_map(&lines);
2019-01-03 22:06:21 -05:00
let squares = calc_overlap(&map);
println!("overlapped squares: {}", squares);
let line = find_non_overlapped(&lines, &map).unwrap();
println!("non overlapped line: {}", line);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let ex1 = vec![
String::from("#1 @ 1,3: 4x4"),
String::from("#2 @ 3,1: 4x4"),
String::from("#3 @ 5,5: 2x2"),
];
let map = build_map(&ex1);
assert_eq!(calc_overlap(&map), 4);
}
#[test]
fn test_part_two() {
let ex1 = vec![
String::from("#1 @ 1,3: 4x4"),
String::from("#2 @ 3,1: 4x4"),
String::from("#3 @ 5,5: 2x2"),
];
let map = build_map(&ex1);
assert_eq!(find_non_overlapped(&ex1, &map).unwrap(), ex1[2]);
}
2018-12-04 15:46:29 -05:00
}