day 13 task 1
This commit is contained in:
1
input/day13.txt
Normal file
1
input/day13.txt
Normal file
File diff suppressed because one or more lines are too long
@@ -1,5 +1,5 @@
|
|||||||
mod tasks;
|
mod tasks;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tasks::day12::run();
|
tasks::day13::run();
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/tasks/day13.rs
Normal file
53
src/tasks/day13.rs
Normal 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);
|
||||||
|
}
|
||||||
@@ -9,3 +9,4 @@ pub mod day09;
|
|||||||
pub mod day10;
|
pub mod day10;
|
||||||
pub mod day11;
|
pub mod day11;
|
||||||
pub mod day12;
|
pub mod day12;
|
||||||
|
pub mod day13;
|
||||||
|
|||||||
Reference in New Issue
Block a user