From e717232ec1d01c01e8ce99d0fb2a4e7c74ca3999 Mon Sep 17 00:00:00 2001 From: maneatingape <44142177+maneatingape@users.noreply.github.com> Date: Sun, 29 Dec 2024 10:05:41 +0000 Subject: [PATCH] Year 2019 Day 13 fix different sized game play area --- README.md | 2 +- src/year2019/day13.rs | 36 ++++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 972cf79..d3fec3b 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro | 10 | [Monitoring Station](https://adventofcode.com/2019/day/10) | [Source](src/year2019/day10.rs) | 1092 | | 11 | [Space Police](https://adventofcode.com/2019/day/11) | [Source](src/year2019/day11.rs) | 341 | | 12 | [The N-Body Problem](https://adventofcode.com/2019/day/12) | [Source](src/year2019/day12.rs) | 1309 | -| 13 | [Care Package](https://adventofcode.com/2019/day/13) | [Source](src/year2019/day13.rs) | 2510 | +| 13 | [Care Package](https://adventofcode.com/2019/day/13) | [Source](src/year2019/day13.rs) | 2527 | | 14 | [Space Stoichiometry](https://adventofcode.com/2019/day/14) | [Source](src/year2019/day14.rs) | 17 | | 15 | [Oxygen System](https://adventofcode.com/2019/day/15) | [Source](src/year2019/day15.rs) | 360 | | 16 | [Flawed Frequency Transmission](https://adventofcode.com/2019/day/16) | [Source](src/year2019/day16.rs) | 1956 | diff --git a/src/year2019/day13.rs b/src/year2019/day13.rs index 4171ff5..a2932e1 100644 --- a/src/year2019/day13.rs +++ b/src/year2019/day13.rs @@ -16,22 +16,24 @@ pub fn parse(input: &str) -> Vec { pub fn part1(input: &[i64]) -> usize { let mut computer = Computer::new(input); - let mut tiles = [0; 44 * 22]; + let mut blocks = 0; loop { - let State::Output(x) = computer.run() else { + let State::Output(_) = computer.run() else { break; }; - let State::Output(y) = computer.run() else { + let State::Output(_) = computer.run() else { break; }; let State::Output(t) = computer.run() else { break; }; - tiles[(44 * y + x) as usize] = t; + if t == 2 { + blocks += 1; + } } - tiles.iter().filter(|&&t| t == 2).count() + blocks } pub fn part2(input: &[i64]) -> i64 { @@ -39,7 +41,8 @@ pub fn part2(input: &[i64]) -> i64 { modified[0] = 2; let mut computer = Computer::new(&modified); - let mut tiles = [0; 44 * 22]; + let mut tiles = Vec::with_capacity(1_000); + let mut stride = 0; let mut score = 0; let mut blocks = score; let mut ball: i64 = 0; @@ -69,7 +72,13 @@ pub fn part2(input: &[i64]) -> i64 { break score; } } else { - let index = (44 * y + x) as usize; + if x >= stride { + stride = x + 1; + } + let index = (stride * y + x) as usize; + if index >= tiles.len() { + tiles.resize(index + 1, 0); + } match t { 0 if tiles[index] == 2 => blocks -= 1, @@ -85,12 +94,12 @@ pub fn part2(input: &[i64]) -> i64 { // Non essential but hilarious. Enable feature then run program in a command line // conosle to observe an animated game of breakout. #[cfg(feature = "frivolity")] - draw(&tiles, score, blocks); + draw(&tiles, stride, score, blocks); } } #[cfg(feature = "frivolity")] -fn draw(tiles: &[i64], score: i64, blocks: i64) { +fn draw(tiles: &[i64], stride: i64, score: i64, blocks: i64) { use crate::util::ansi::*; use std::fmt::Write as _; use std::thread::sleep; @@ -104,10 +113,12 @@ fn draw(tiles: &[i64], score: i64, blocks: i64) { let s = &mut String::new(); let _ = writeln!(s, "{WHITE}{BOLD}Blocks: {blocks}\tScore: {score} {RESET}"); + let mut y = 0; - for y in 0..22 { - for x in 0..44 { - let _unused = match tiles[44 * y + x] { + while stride * y < tiles.len() as i64 { + for x in 0..stride { + let index = (stride * y + x) as usize; + let _unused = match tiles[index] { 0 => write!(s, " "), 1 if y == 0 => write!(s, "{GREEN}_{RESET}"), 1 => write!(s, "{GREEN}|{RESET}"), @@ -118,6 +129,7 @@ fn draw(tiles: &[i64], score: i64, blocks: i64) { }; } s.push('\n'); + y += 1; } println!("{HOME}{CLEAR}{s}");