From 3f9da45a71e715fd7fec4c1b73fbbda55fba74c6 Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Fri, 14 Dec 2018 00:41:09 -0500 Subject: [PATCH] day 11 solution --- 2018/day11/Cargo.lock | 4 ++ 2018/day11/Cargo.toml | 7 ++++ 2018/day11/input | 1 + 2018/day11/src/main.rs | 85 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 2018/day11/Cargo.lock create mode 100644 2018/day11/Cargo.toml create mode 100644 2018/day11/input create mode 100644 2018/day11/src/main.rs diff --git a/2018/day11/Cargo.lock b/2018/day11/Cargo.lock new file mode 100644 index 0000000..3436c4e --- /dev/null +++ b/2018/day11/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "day11" +version = "0.1.0" + diff --git a/2018/day11/Cargo.toml b/2018/day11/Cargo.toml new file mode 100644 index 0000000..d090de7 --- /dev/null +++ b/2018/day11/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day11" +version = "0.1.0" +authors = ["Andrew Coleman "] +edition = "2018" + +[dependencies] diff --git a/2018/day11/input b/2018/day11/input new file mode 100644 index 0000000..9d3c219 --- /dev/null +++ b/2018/day11/input @@ -0,0 +1 @@ +5468 diff --git a/2018/day11/src/main.rs b/2018/day11/src/main.rs new file mode 100644 index 0000000..7cd146a --- /dev/null +++ b/2018/day11/src/main.rs @@ -0,0 +1,85 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::iter::Iterator; + +fn calc_map_from_serial_number(serial_number: i32) -> [[i32; 300]; 300] { + let mut result = [[0i32; 300]; 300]; + for i in 0..300 { + for j in 0..300 { + let rack_id = i + 10; + let power = ((rack_id * j) + serial_number) * rack_id; + let hundreds = power.to_string().as_str().chars().rev().nth(2).unwrap(); + result[i as usize][j as usize] = (hundreds as i32) - 48 - 5; + } + } + result +} + +fn find_max_sum(map: &[[i32; 300]; 300], subsize: usize) -> (usize, usize, i32) { + let presums = precalculate_sums(&map, subsize); + let mut x = 0; + let mut y = 0; + let mut sum = 0; + for i in 0..(300 - subsize) { + for j in 0..(300 - subsize) { + let mut new_sum = 0; + for s in 0..subsize { + new_sum += presums[i + s][j]; + } + if new_sum > sum { + sum = new_sum; + x = i; + y = j; + } + } + } + + (x, y, sum) +} + +fn precalculate_sums(map: &[[i32; 300]; 300], subsize: usize) -> [[i32; 300]; 300] { + let mut result = [[0i32; 300]; 300]; + for i in 0..300 { + for j in 0..(300 - subsize) { + for s in 0..subsize { + result[i][j] += map[i][j + s]; + } + } + } + + result +} + +fn find_variable_max_sum(map: &[[i32; 300]; 300]) -> (usize, usize, i32, usize) { + let mut x = 0; + let mut y = 0; + let mut size = 0; + let mut sum = 0; + for i in 1..=300 { + let (new_x, new_y, new_sum) = find_max_sum(map, i); + if new_sum == 0 { break; } + if new_sum > sum { + size = i; + sum = new_sum; + x = new_x; + y = new_y; + } + } + + (x, y, sum, size) +} + +fn main() { + let mut line = String::new(); + BufReader::new(File::open("input").unwrap()) + .read_line(&mut line) + .unwrap(); + let serial_number = line.trim().parse::().unwrap(); + + let map = calc_map_from_serial_number(serial_number); + let (x, y, sum) = find_max_sum(&map, 3); + println!("maximum sum coordinate of ({}, {}) with sum of {}", x, y, sum); + + let (x, y, sum, size) = find_variable_max_sum(&map); + println!("maximum variable sum found at ({}, {}) sum {} with size of {}", x, y, sum, size); +}