diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-12-06 21:44:56 +0100 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-12-06 21:44:56 +0100 |
commit | c474e4c4e8008cec27b6cd696f2a47fe25e0a739 (patch) | |
tree | 53bf05af93fad30d1d90c4e2c714b8ea5d2d0f87 /2018/src | |
parent | d38746c3b68f139fb2f6df78a219c2577fc0338c (diff) |
Day 6
Diffstat (limited to '2018/src')
-rw-r--r-- | 2018/src/day6.rs | 42 | ||||
-rw-r--r-- | 2018/src/main.rs | 6 |
2 files changed, 46 insertions, 2 deletions
diff --git a/2018/src/day6.rs b/2018/src/day6.rs new file mode 100644 index 0000000..58fa733 --- /dev/null +++ b/2018/src/day6.rs @@ -0,0 +1,42 @@ +use std::io; +use std::io::BufRead; + +const W: i32 = 800; + +fn dist(a: (i32, i32), b: (i32, i32)) -> i32 { + (b.0 - a.0).abs() + (b.1 - a.1).abs() +} + +pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> { + let pts: Vec<(i32, i32)> = reader.lines().map(|l| l.unwrap()).map(|line| { + let mut spl = line.split(", ").map(|s| s.parse().unwrap()); + let x = spl.next().unwrap(); + let y = spl.next().unwrap(); + (x, y) + }).collect(); + + let mut borderpts = vec![false; pts.len()]; + let mut size = vec![0; pts.len()]; + let mut nsafe = 0; + + for y in -W..W+1 { + for x in -W..W+1 { + let closest = (0..pts.len()).min_by_key(|&i| dist(pts[i], (x, y))).unwrap(); + if x == -W || x == W || y == -W || y == W { + borderpts[closest] = true; + } else { + size[closest] += 1; + } + + let distsum: i32 = pts.iter().map(|&p| dist(p, (x, y))).sum(); + if distsum < 10000 { + nsafe += 1; + } + } + } + + let part1 = size[(0..pts.len()).max_by_key(|&i| if borderpts[i] { -1 } else { size[i] }).unwrap()].to_string(); + let part2 = nsafe.to_string(); + + Ok((part1, part2)) +} diff --git a/2018/src/main.rs b/2018/src/main.rs index f9098eb..c35e142 100644 --- a/2018/src/main.rs +++ b/2018/src/main.rs @@ -14,8 +14,9 @@ mod day2; mod day3; mod day4; mod day5; +mod day6; -static NUM_DAYS: i32 = 5; +static NUM_DAYS: i32 = 6; fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> { match day { @@ -24,6 +25,7 @@ fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> { 3 => day3::main(reader), 4 => day4::main(reader), 5 => day5::main(reader), + 6 => day6::main(reader), _ => Err(Error::new(ErrorKind::Other, "Invalid day")) } } @@ -89,7 +91,7 @@ fn run_all_days(opts: &Options) -> io::Result<()> { assert!(!opts.use_stdin); benchmark_all_days() } else { - for day in 1..6 { + for day in 1..NUM_DAYS + 1 { run_day(day, &opts)?; } Ok(()) |