Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b67ed69f7d | ||
|
|
ad3b71b159 |
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
1,380,379,385,1008,2663,456801,381,1005,381,12,99,109,2664,1101,0,0,383,1101,0,0,382,20101,0,382,1,20102,1,383,2,21102,37,1,0,1105,1,578,4,382,4,383,204,1,1001,382,1,382,1007,382,44,381,1005,381,22,1001,383,1,383,1007,383,23,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1106,0,161,107,1,392,381,1006,381,161,1101,-1,0,384,1106,0,119,1007,392,42,381,1006,381,161,1102,1,1,384,21001,392,0,1,21102,1,21,2,21102,1,0,3,21102,138,1,0,1105,1,549,1,392,384,392,21001,392,0,1,21101,0,21,2,21102,1,3,3,21101,0,161,0,1106,0,549,1101,0,0,384,20001,388,390,1,20101,0,389,2,21102,1,180,0,1105,1,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21001,389,0,2,21101,0,205,0,1106,0,393,1002,390,-1,390,1102,1,1,384,20102,1,388,1,20001,389,391,2,21102,1,228,0,1105,1,578,1206,1,261,1208,1,2,381,1006,381,253,20101,0,388,1,20001,389,391,2,21102,1,253,0,1105,1,393,1002,391,-1,391,1101,1,0,384,1005,384,161,20001,388,390,1,20001,389,391,2,21102,1,279,0,1105,1,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21101,0,304,0,1105,1,393,1002,390,-1,390,1002,391,-1,391,1102,1,1,384,1005,384,161,21001,388,0,1,20102,1,389,2,21102,1,0,3,21101,338,0,0,1105,1,549,1,388,390,388,1,389,391,389,20102,1,388,1,20102,1,389,2,21102,1,4,3,21101,0,365,0,1106,0,549,1007,389,22,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,315,20,18,1,1,22,109,3,22101,0,-2,1,21202,-1,1,2,21102,0,1,3,21101,0,414,0,1106,0,549,22102,1,-2,1,21202,-1,1,2,21101,429,0,0,1106,0,601,1202,1,1,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2105,1,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,21202,-3,1,-7,109,-8,2106,0,0,109,4,1202,-2,44,566,201,-3,566,566,101,639,566,566,2102,1,-1,0,204,-3,204,-2,204,-1,109,-4,2106,0,0,109,3,1202,-1,44,594,201,-2,594,594,101,639,594,594,20101,0,0,-2,109,-3,2106,0,0,109,3,22102,23,-2,1,22201,1,-1,1,21102,1,509,2,21101,264,0,3,21102,1012,1,4,21102,1,630,0,1106,0,456,21201,1,1651,-2,109,-3,2106,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,2,0,0,2,0,2,0,0,2,0,2,0,0,2,2,0,2,0,2,2,0,0,0,0,2,0,0,2,2,2,0,2,0,2,0,2,2,2,0,1,1,0,0,0,2,2,0,0,0,0,2,2,2,0,0,2,0,2,0,0,0,0,2,2,2,2,0,0,2,0,2,2,2,2,0,0,0,2,0,2,0,0,0,1,1,0,2,0,0,0,2,0,2,2,2,0,0,2,0,2,0,0,0,0,2,2,0,0,2,2,0,0,2,0,0,0,0,2,2,0,2,0,0,2,0,0,0,1,1,0,0,2,0,2,2,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2,2,2,0,0,2,2,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,78,97,10,89,31,40,53,97,63,60,92,10,54,27,53,42,36,34,79,30,8,70,22,20,18,67,79,30,81,50,67,46,39,15,72,26,35,61,6,36,2,26,65,94,82,27,37,6,71,66,84,19,69,5,62,89,57,49,1,9,59,67,30,74,71,37,66,77,43,4,59,42,85,4,87,1,24,64,85,25,29,67,97,15,22,6,34,97,97,47,22,19,40,89,45,36,93,77,26,85,30,40,65,21,45,91,18,77,45,13,74,18,47,67,79,1,31,22,1,96,94,60,44,56,79,64,74,56,91,79,41,23,9,57,9,86,63,82,55,92,63,63,94,73,76,40,88,18,26,66,29,27,20,1,94,90,43,11,67,33,27,47,34,73,65,67,77,54,92,84,6,29,41,8,8,38,83,36,74,29,26,70,68,57,54,38,75,37,24,64,30,89,43,61,6,4,65,81,39,85,91,22,28,17,47,95,52,40,76,77,81,52,59,19,37,90,23,33,5,82,3,64,46,70,22,24,9,96,97,69,48,66,58,97,51,15,8Line truncated
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
mod tasks;
|
mod tasks;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tasks::day12::run();
|
tasks::day13::run();
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
use super::day05::{IntCodeComputer, RAM};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[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());
|
||||||
|
task2_ai(program.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum FieldType {
|
||||||
|
Empty,
|
||||||
|
Wall,
|
||||||
|
Block,
|
||||||
|
HorizontalPaddle,
|
||||||
|
Ball,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for FieldType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
match self {
|
||||||
|
Self::Empty => " ",
|
||||||
|
Self::Wall => "X",
|
||||||
|
Self::Block => "B",
|
||||||
|
Self::HorizontalPaddle => "_",
|
||||||
|
Self::Ball => "O",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Game {
|
||||||
|
pc: IntCodeComputer,
|
||||||
|
field: HashMap<(i128, i128), FieldType>,
|
||||||
|
score: i128,
|
||||||
|
show_output: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Game {
|
||||||
|
fn new(coins: Option<i128>, mut ram: RAM, show_output: bool) -> Self {
|
||||||
|
if let Some(c) = coins {
|
||||||
|
ram.insert(0, c);
|
||||||
|
}
|
||||||
|
let pc = IntCodeComputer::new(vec![], ram);
|
||||||
|
Game {
|
||||||
|
pc,
|
||||||
|
field: HashMap::new(),
|
||||||
|
score: 0,
|
||||||
|
show_output,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* true means the game is over, false means input is awaited
|
||||||
|
*/
|
||||||
|
fn run(&mut self) -> bool {
|
||||||
|
self.pc.clear_output();
|
||||||
|
let done = self.pc.run_until_input_empty();
|
||||||
|
let output = self.pc.get_output().into_iter().map(|it| *it).collect_vec();
|
||||||
|
let mut score = 0;
|
||||||
|
output
|
||||||
|
.iter()
|
||||||
|
.tuples()
|
||||||
|
.scan(&mut self.field, |map, (x, y, t)| {
|
||||||
|
if *x == -1 && *y == 0 {
|
||||||
|
score = *t;
|
||||||
|
} else {
|
||||||
|
map.insert((*x, *y), FieldType::from(*t));
|
||||||
|
}
|
||||||
|
Some(())
|
||||||
|
})
|
||||||
|
.any(|_| false);
|
||||||
|
self.score = score;
|
||||||
|
if self.show_output {
|
||||||
|
self.print();
|
||||||
|
}
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_input(&mut self, input: Input) {
|
||||||
|
self.pc.set_input(&[input.int()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print(&self) {
|
||||||
|
let xmin = *self.field.iter().map(|((x, _), _)| x).min().unwrap();
|
||||||
|
let xmax = *self.field.iter().map(|((x, _), _)| x).max().unwrap();
|
||||||
|
let ymin = *self.field.iter().map(|((_, y), _)| y).min().unwrap();
|
||||||
|
let ymax = *self.field.iter().map(|((_, y), _)| y).max().unwrap();
|
||||||
|
for y in ymin..=ymax {
|
||||||
|
for x in xmin..=xmax {
|
||||||
|
if let Some(f) = self.field.get(&(x, y)) {
|
||||||
|
print!("{}", f);
|
||||||
|
} else {
|
||||||
|
panic!("There is an unspecified tile in the field");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
println!("Score: {}", self.score);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum Input {
|
||||||
|
Neutral,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Input {
|
||||||
|
fn int(&self) -> i128 {
|
||||||
|
match self {
|
||||||
|
Self::Neutral => 0,
|
||||||
|
Self::Left => -1,
|
||||||
|
Self::Right => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from(input: char) -> Self {
|
||||||
|
match input {
|
||||||
|
's' => Self::Neutral,
|
||||||
|
'a' => Self::Left,
|
||||||
|
'd' => Self::Right,
|
||||||
|
_ => Self::Neutral,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn task1(program: RAM) {
|
||||||
|
let mut game = Game::new(None, program, true);
|
||||||
|
if game.run() {
|
||||||
|
let block_count = game
|
||||||
|
.field
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, ftype)| **ftype == FieldType::Block)
|
||||||
|
.count();
|
||||||
|
println!("Task 1: There are {} blocks in the field", block_count);
|
||||||
|
} else {
|
||||||
|
unreachable!("In this task the game should end without any input.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn task2(program: RAM) {
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
let mut game = Game::new(Some(2), program, true);
|
||||||
|
let stdin = io::stdin();
|
||||||
|
let mut input_iter = stdin.lock().lines();
|
||||||
|
while !game.run() {
|
||||||
|
// read input
|
||||||
|
let next = input_iter.next().unwrap().unwrap().chars().next();
|
||||||
|
if let Some(s) = next {
|
||||||
|
game.set_input(Input::from(s));
|
||||||
|
} else {
|
||||||
|
game.set_input(Input::Neutral);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!(
|
||||||
|
"You won against the game! Your final score was {}",
|
||||||
|
game.score
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn task2_ai(program: RAM) {
|
||||||
|
let mut game = Game::new(Some(2), program, false);
|
||||||
|
let mut moves = 0;
|
||||||
|
while !game.run() {
|
||||||
|
// always keep the paddle under the ball
|
||||||
|
let x_ball = game
|
||||||
|
.field
|
||||||
|
.iter()
|
||||||
|
.find_map(|((x, _), t)| {
|
||||||
|
if *t == FieldType::Ball {
|
||||||
|
Some(*x)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let x_paddle = game
|
||||||
|
.field
|
||||||
|
.iter()
|
||||||
|
.find_map(|((x, _), t)| {
|
||||||
|
if *t == FieldType::HorizontalPaddle {
|
||||||
|
Some(*x)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let direction = if x_ball < x_paddle {
|
||||||
|
Input::Left
|
||||||
|
} else if x_ball > x_paddle {
|
||||||
|
Input::Right
|
||||||
|
} else {
|
||||||
|
Input::Neutral
|
||||||
|
};
|
||||||
|
game.set_input(direction);
|
||||||
|
moves += 1;
|
||||||
|
}
|
||||||
|
println!("Played for {} moves", moves);
|
||||||
|
println!(
|
||||||
|
"Game is over! There are {} blocks left. Your final score is {}",
|
||||||
|
game.field
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, t)| **t == FieldType::Block)
|
||||||
|
.count(),
|
||||||
|
game.score
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -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