summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2018/input/6.txt50
-rw-r--r--2018/src/day6.rs42
-rw-r--r--2018/src/main.rs6
3 files changed, 96 insertions, 2 deletions
diff --git a/2018/input/6.txt b/2018/input/6.txt
new file mode 100644
index 0000000..4ef0c20
--- /dev/null
+++ b/2018/input/6.txt
@@ -0,0 +1,50 @@
+192, 212
+294, 73
+153, 248
+238, 54
+354, 207
+269, 256
+155, 329
+132, 308
+211, 173
+261, 241
+300, 218
+143, 43
+226, 348
+148, 349
+114, 78
+77, 327
+140, 327
+202, 346
+174, 115
+86, 198
+132, 152
+167, 184
+146, 259
+277, 288
+330, 199
+98, 332
+290, 186
+322, 120
+295, 355
+346, 260
+305, 190
+294, 82
+156, 159
+114, 263
+340, 220
+353, 207
+220, 219
+152, 122
+223, 319
+236, 243
+358, 348
+174, 116
+306, 74
+70, 264
+352, 351
+194, 214
+153, 322
+225, 99
+237, 331
+279, 208
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(())