day 17 task 1

This commit is contained in:
Johannes
2019-12-25 21:50:38 +01:00
parent c726b9090f
commit d590b1ad1a
4 changed files with 60 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
mod tasks;
fn main() {
tasks::day18::run();
tasks::day17::run();
}

57
src/tasks/day17.rs Normal file
View File

@@ -0,0 +1,57 @@
use super::day05::*;
pub fn run() {
let input: RAM = std::fs::read_to_string("input/day17.txt")
.unwrap()
.split(",")
.enumerate()
.map(|(i, it)| (i, it.parse::<i128>().unwrap()))
.collect();
task1(input.clone());
}
fn task1(ram: RAM) {
let mut pc = IntCodeComputer::new(vec![], ram);
pc.run_until_end();
let map: Vec<Vec<char>> = pc
.get_output()
.iter()
.map(|it| (*it as u8) as char)
.collect::<String>()
.lines()
.map(|line| line.chars().collect())
.collect();
let height = map.len();
let width = map[0].len();
let alignment_sum = (1..height - 1).fold(0, |sum, y| {
sum + (1..width - 1).fold(0, |sum, x| {
let pos = Pos(x, y);
if map[y][x] == '#'
&& pos
.neighbors()
.iter()
.all(|Pos(xp, yp)| map[*yp][*xp] == '#')
{
sum + pos.0 * pos.1
} else {
sum
}
})
});
println!("Task 1: sum of alignment parameters is {}", alignment_sum);
}
struct Pos(usize, usize);
impl Pos {
fn neighbors(&self) -> Vec<Pos> {
vec![
Pos(self.0 - 1, self.1),
Pos(self.0 + 1, self.1),
Pos(self.0, self.1 - 1),
Pos(self.0, self.1 - 1),
]
}
}

View File

@@ -13,5 +13,6 @@ pub mod day13;
pub mod day14;
pub mod day15;
pub mod day16;
pub mod day17;
pub mod day18;
pub mod day21;