Skip to content

Instantly share code, notes, and snippets.

@Steffo99
Last active December 5, 2018 14:48
Show Gist options
  • Save Steffo99/fcde8b824aaea3d3b1bfa44c3794e745 to your computer and use it in GitHub Desktop.
Save Steffo99/fcde8b824aaea3d3b1bfa44c3794e745 to your computer and use it in GitHub Desktop.
extern crate regex;
#[macro_use] extern crate lazy_static;
struct Claim {
i: usize,
x: usize,
y: usize,
l: usize,
h: usize
}
fn parse(s: &str) -> Claim {
lazy_static! {
static ref REGEX: regex::Regex = regex::Regex::new(r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)").unwrap();
}
let findings = REGEX.captures(s).unwrap();
Claim {
i: findings.get(1).unwrap().as_str().parse::<usize>().unwrap(),
x: findings.get(2).unwrap().as_str().parse::<usize>().unwrap(),
y: findings.get(3).unwrap().as_str().parse::<usize>().unwrap(),
l: findings.get(4).unwrap().as_str().parse::<usize>().unwrap(),
h: findings.get(5).unwrap().as_str().parse::<usize>().unwrap()
}
}
fn claim(c: Claim, canvas: &mut[[u8; 1000]; 1000]) {
for x in c.x..(c.x+c.l) {
for y in c.y..(c.y+c.h) {
canvas[x][y] += 1;
}
}
}
fn main() {
//Create an empty 1000x1000 canvas
let mut canvas: [[u8; 1000]; 1000] = [[0; 1000]; 1000];
//Read the file in the string
let contents = std::fs::read_to_string("input.txt").unwrap();
//Split the string into multiple rows
let rows = contents.split('\n');
//For each row
for row in rows {
let c: Claim = parse(&row);
claim(c, &mut canvas);
}
let mut count: usize = 0;
for x in 0..1000 {
for y in 0..1000 {
if canvas[x][y] >= 2 {
count += 1;
}
}
}
println!("{}", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment