day 13 task 1

This commit is contained in:
Johannes
2019-12-14 16:53:42 +01:00
parent eb7467f615
commit ad3b71b159
4 changed files with 56 additions and 1 deletions

53
src/tasks/day13.rs Normal file
View File

@@ -0,0 +1,53 @@
use super::day05::{IntCodeComputer, RAM};
use itertools::Itertools;
use std::collections::HashMap;
#[allow(dead_code)]
pub fn run() {
let program: RAM = std::fs::read_to_string("input/day13.txt")
.unwrap()
.split(",")
.map(|it| it.parse::<i128>().unwrap())
.enumerate()
.collect();
task1(program.clone());
}
#[derive(PartialEq)]
enum FieldType {
Empty,
Wall,
Block,
HorizontalPaddle,
Ball,
}
impl FieldType {
fn from(input: i128) -> Self {
match input {
0 => Self::Empty,
1 => Self::Wall,
2 => Self::Block,
3 => Self::HorizontalPaddle,
4 => Self::Ball,
_ => unreachable!("unexpected field type {}", input),
}
}
}
fn task1(program: RAM) {
let mut pc = IntCodeComputer::new(vec![], program);
pc.run_until_end();
let field = pc.get_output().iter().tuples().fold(
HashMap::<(i128, i128), FieldType>::new(),
|mut map, (x, y, t)| {
map.insert((*x, *y), FieldType::from(*t));
map
},
);
let block_count = field
.iter()
.filter(|(_, ftype)| **ftype == FieldType::Block)
.count();
println!("Task 1: There are {} blocks in the field", block_count);
}