From 71168b6401b709fb9960630331669393ece86e25 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 11 Dec 2018 10:05:44 +0100 Subject: Day 11 --- 2018/input/11.txt | 1 + 2018/src/day11.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2018/src/main.rs | 4 +++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 2018/input/11.txt create mode 100644 2018/src/day11.rs 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) { + 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::()).sum(); + + if tot > maxval { + maxval = tot; + maxat = (x, y, s); + } + } + } + } + + maxat +} + +pub fn main(reader: T) -> io::Result<(String, String)> { + let serial = reader.lines().next().unwrap().unwrap().parse::().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(day: i32, reader: T) -> io::Result<(String, String)> { match day { @@ -30,6 +31,7 @@ fn day_switch(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")) } } -- cgit v1.2.3-70-g09d2