summaryrefslogtreecommitdiff
path: root/2018
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-12-11 10:05:44 +0100
committerTom Smeding <tom.smeding@gmail.com>2018-12-11 10:05:44 +0100
commit71168b6401b709fb9960630331669393ece86e25 (patch)
tree9881f59f01031e6df5344910eca51718eed582d8 /2018
parenta4f874cf72d1c8522fb31caaa840b455d50c394c (diff)
Day 11
Diffstat (limited to '2018')
-rw-r--r--2018/input/11.txt1
-rw-r--r--2018/src/day11.rs45
-rw-r--r--2018/src/main.rs4
3 files changed, 49 insertions, 1 deletions
diff --git a/2018/input/11.txt b/2018/input/11.txt
new file mode 100644
index 0000000..9b5f360
--- /dev/null
+++ b/2018/input/11.txt
@@ -0,0 +1 @@
+1723
diff --git a/2018/src/day11.rs b/2018/src/day11.rs
new file mode 100644
index 0000000..4503561
--- /dev/null
+++ b/2018/src/day11.rs
@@ -0,0 +1,45 @@
+use std::io;
+use std::io::BufRead;
+use std::ops::Range;
+
+fn power(x: i32, y: i32, serial: i32) -> i32 {
+ (y * (x + 10) + serial) * (x + 10) / 100 % 10 - 5
+}
+
+fn optimise(grid: [[i32; 300]; 300], srange: Range<usize>) -> (usize, usize, usize) {
+ let mut maxval = i32::min_value();
+ let mut maxat = (0, 0, 0);
+ for s in srange {
+ for y in 0..301-s {
+ for x in 0..301-s {
+ let tot = (0..s).map(|dy| (0..s).map(|dx| grid[y+dy][x+dx]).sum::<i32>()).sum();
+
+ if tot > maxval {
+ maxval = tot;
+ maxat = (x, y, s);
+ }
+ }
+ }
+ }
+
+ maxat
+}
+
+pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> {
+ let serial = reader.lines().next().unwrap().unwrap().parse::<i32>().unwrap();
+
+ let mut grid = [[0; 300]; 300];
+ for y in 0..300 {
+ for x in 0..300 {
+ grid[y][x] = power(x as i32, y as i32, serial);
+ }
+ }
+
+ let maxat = optimise(grid, 3..4);
+ let part1 = format!("{},{}", maxat.0, maxat.1);
+
+ let maxat = optimise(grid, 1..301);
+ let part2 = format!("{},{},{}", maxat.0, maxat.1, maxat.2);
+
+ Ok((part1, part2))
+}
diff --git a/2018/src/main.rs b/2018/src/main.rs
index 3e9756b..e3622a3 100644
--- a/2018/src/main.rs
+++ b/2018/src/main.rs
@@ -15,8 +15,9 @@ mod day7;
mod day8;
mod day9;
mod day10;
+mod day11;
-static NUM_DAYS: i32 = 10;
+static NUM_DAYS: i32 = 11;
fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> {
match day {
@@ -30,6 +31,7 @@ fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> {
8 => day8::main(reader),
9 => day9::main(reader),
10 => day10::main(reader),
+ 11 => day11::main(reader),
_ => Err(Error::new(ErrorKind::Other, "Invalid day"))
}
}