Created
November 28, 2022 12:41
-
-
Save TannerRogalsky/2f36d7a00599dc658d2af1b3f254c053 to your computer and use it in GitHub Desktop.
AoC Problem01 2021 GBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![cfg_attr(not(test), no_std)] | |
#![cfg_attr(not(test), no_main)] | |
#[cfg(not(test))] | |
extern crate alloc; | |
#[cfg(not(test))] | |
type Vec<T> = alloc::vec::Vec<T>; | |
#[cfg(not(test))] | |
#[agb::entry] | |
fn main(mut gba: agb::Gba) -> ! { | |
use agb::{ | |
display::{ | |
tiled::{RegularBackgroundSize, TileSetting, TiledMap}, | |
Font, Priority, | |
}, | |
include_font, | |
}; | |
use core::fmt::Write; | |
const FONT: Font = include_font!("04b03.TTF", 12); | |
let mut bitmap = gba.display.video.bitmap3(); | |
const INPUT: &str = include_str!("../../input/01.txt"); | |
let input = parse(INPUT); | |
let r1 = part1(&input); | |
let r2 = part2(&input); | |
let (gfx, mut vram) = gba.display.video.tiled0(); | |
let vblank = agb::interrupt::VBlank::get(); | |
vram.set_background_palette_raw(&[ | |
0x0000, 0x0ff0, 0x00ff, 0xf00f, 0xf0f0, 0x0f0f, 0xaaaa, 0x5555, 0x0000, 0x0000, 0x0000, | |
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | |
]); | |
let background_tile = vram.new_dynamic_tile().fill_with(0); | |
let mut bg = gfx.background(Priority::P0, RegularBackgroundSize::Background32x32); | |
for y in 0..20u16 { | |
for x in 0..30u16 { | |
bg.set_tile( | |
&mut vram, | |
(x, y).into(), | |
&background_tile.tile_set(), | |
TileSetting::from_raw(background_tile.tile_index()), | |
); | |
} | |
} | |
vram.remove_dynamic_tile(background_tile); | |
let mut renderer = FONT.render_text((0u16, 3u16).into()); | |
let mut writer = renderer.writer(1, 2, &mut bg, &mut vram); | |
writeln!(&mut writer, "PART1: {}", r1).unwrap(); | |
writeln!(&mut writer, "PART2: {}", r2).unwrap(); | |
writer.commit(); | |
bg.commit(&mut vram); | |
bg.show(); | |
let mut frame = 0; | |
loop { | |
let mut renderer = FONT.render_text((4u16, 0u16).into()); | |
let mut writer = renderer.writer(1, 2, &mut bg, &mut vram); | |
writeln!(&mut writer, "Frame {}", frame).unwrap(); | |
writer.commit(); | |
frame += 1; | |
vblank.wait_for_vblank(); | |
bg.commit(&mut vram); | |
renderer.clear(&mut vram); | |
} | |
} | |
fn parse(input: &str) -> Vec<i32> { | |
input | |
.lines() | |
.map(|line| line.trim().parse().unwrap()) | |
.collect() | |
} | |
fn part1(input: &[i32]) -> u32 { | |
let mut iter = input.iter().copied().peekable(); | |
let mut count = 0; | |
while let Some(v) = iter.next() { | |
if let Some(next) = iter.peek() { | |
if *next > v { | |
count += 1; | |
} | |
} | |
} | |
count | |
} | |
fn part2(input: &[i32]) -> u32 { | |
let mut count = 0; | |
let mut prev = i32::MAX; | |
for index in 0..=(input.len().saturating_sub(3)) { | |
let v = input[index..(index + 3)].iter().sum::<i32>(); | |
if v > prev { | |
count += 1; | |
} | |
prev = v; | |
} | |
count | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn part1_test() { | |
let input = parse(INPUT); | |
let count = part1(&input); | |
assert_eq!(7, count); | |
} | |
#[test] | |
fn part2_test() { | |
let input = parse(INPUT); | |
let count = part2(&input); | |
assert_eq!(5, count); | |
} | |
const INPUT: &'static str = r#"199 | |
200 | |
208 | |
210 | |
200 | |
207 | |
240 | |
269 | |
260 | |
263"#; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment