Solve year 2022 day 05
This commit is contained in:
parent
11ac320683
commit
01f5df4774
@ -12,10 +12,26 @@ impl<'a> Iterator for U32s<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct I32s<'a> {
|
||||||
|
bytes: Bytes<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for I32s<'a> {
|
||||||
|
type Item = i32;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
try_parse_i32(&mut self.bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait ParseExt {
|
pub trait ParseExt {
|
||||||
fn parse_u32(&self) -> u32;
|
fn parse_u32(&self) -> u32;
|
||||||
|
|
||||||
fn u32s(&self) -> U32s<'_>;
|
fn u32s(&self) -> U32s<'_>;
|
||||||
|
|
||||||
|
fn parse_i32(&self) -> i32;
|
||||||
|
|
||||||
|
fn i32s(&self) -> I32s<'_>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParseExt for &str {
|
impl ParseExt for &str {
|
||||||
@ -31,6 +47,19 @@ impl ParseExt for &str {
|
|||||||
bytes: self.bytes(),
|
bytes: self.bytes(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_i32(&self) -> i32 {
|
||||||
|
match try_parse_i32(&mut self.bytes()) {
|
||||||
|
Some(num) => num,
|
||||||
|
None => panic!("unable to parse i32: {self}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn i32s(&self) -> I32s<'_> {
|
||||||
|
I32s {
|
||||||
|
bytes: self.bytes(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_parse_u32(bytes: &mut Bytes<'_>) -> Option<u32> {
|
fn try_parse_u32(bytes: &mut Bytes<'_>) -> Option<u32> {
|
||||||
@ -56,3 +85,36 @@ fn try_parse_u32(bytes: &mut Bytes<'_>) -> Option<u32> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn try_parse_i32(bytes: &mut Bytes<'_>) -> Option<i32> {
|
||||||
|
let mut fac = 1;
|
||||||
|
let mut n = loop {
|
||||||
|
let byte = bytes.next()?;
|
||||||
|
if byte == b'-' {
|
||||||
|
let byte = bytes.next()?;
|
||||||
|
let digit = byte.wrapping_sub(b'0');
|
||||||
|
if digit < 10 {
|
||||||
|
fac = -1;
|
||||||
|
break digit as i32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let digit = byte.wrapping_sub(b'0');
|
||||||
|
|
||||||
|
if digit < 10 {
|
||||||
|
break digit as i32;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let Some(byte) = bytes.next() else {
|
||||||
|
break Some(n * fac);
|
||||||
|
};
|
||||||
|
let digit = byte.wrapping_sub(b'0');
|
||||||
|
|
||||||
|
if digit < 10 {
|
||||||
|
n = 10 * n + digit as i32;
|
||||||
|
} else {
|
||||||
|
break Some(n * fac);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
use crate::util::parse::ParseExt;
|
use crate::util::parse::ParseExt;
|
||||||
|
|
||||||
fn parse(input: &str) -> (BTreeMap<usize, Vec<char>>, Vec<[u32; 3]>) {
|
fn parse(input: &str) -> Option<(Vec<Vec<char>>, impl Iterator<Item = [u32; 3]> + '_)> {
|
||||||
let mut crates: BTreeMap<usize, Vec<char>> = BTreeMap::new();
|
let (board, moveset) = input.split_once("\n\n")?;
|
||||||
let mut moves = vec![];
|
let size = board.lines().last()?.u32s().count();
|
||||||
let (board, moveset) = input.split_once("\n\n\n").expect("must be there");
|
let mut crates: Vec<Vec<char>> = vec![vec![]; size + 1];
|
||||||
|
|
||||||
board.lines().for_each(|line| {
|
board.lines().for_each(|line| {
|
||||||
if !line.contains('[') {
|
if !line.contains('[') {
|
||||||
@ -16,35 +14,66 @@ fn parse(input: &str) -> (BTreeMap<usize, Vec<char>>, Vec<[u32; 3]>) {
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(_idx, c)| c.is_ascii_uppercase())
|
.filter(|(_idx, c)| c.is_ascii_uppercase())
|
||||||
.for_each(|(idx, c)| {
|
.for_each(|(idx, c)| {
|
||||||
crates
|
crates[((idx + 2) / 4) + 1].push(c);
|
||||||
.entry(((idx - 4) / 4) + 1)
|
|
||||||
.and_modify(|v| v.push(c))
|
|
||||||
.or_insert(vec![]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
crates.values_mut().for_each(|v| v.reverse());
|
for v in crates.iter_mut() {
|
||||||
|
v.reverse();
|
||||||
|
}
|
||||||
|
|
||||||
let moves = moveset
|
let moves = moveset.lines().filter_map(|line| {
|
||||||
.lines()
|
|
||||||
.map(|line| {
|
|
||||||
let mut nums = line.u32s();
|
let mut nums = line.u32s();
|
||||||
[
|
Some([nums.next()?, nums.next()?, nums.next()?])
|
||||||
nums.next().expect("first num"),
|
});
|
||||||
nums.next().expect("second num"),
|
|
||||||
nums.next().expect("third num"),
|
|
||||||
]
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
(crates, moves)
|
Some((crates, moves))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &str) -> impl std::fmt::Display {
|
pub fn part1(input: &str) -> impl std::fmt::Display {
|
||||||
""
|
let (mut crates, moves) = parse(input).expect("parsing failed");
|
||||||
|
let mut out: Vec<String> = vec![String::new(); crates.len()];
|
||||||
|
|
||||||
|
for m in moves {
|
||||||
|
let mut items = {
|
||||||
|
let mut items = Vec::with_capacity(m[0] as usize);
|
||||||
|
let from: &mut Vec<char> = crates[m[1] as usize].as_mut();
|
||||||
|
for _ in 0..m[0] {
|
||||||
|
items.push(from.pop().expect("AOC please don't hate us"));
|
||||||
|
}
|
||||||
|
items
|
||||||
|
};
|
||||||
|
crates[m[2] as usize].append(&mut items);
|
||||||
|
}
|
||||||
|
|
||||||
|
for v in crates.iter_mut().skip(1) {
|
||||||
|
out.push(v.pop().expect("yes").to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
out.join("")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &str) -> impl std::fmt::Display {
|
pub fn part2(input: &str) -> impl std::fmt::Display {
|
||||||
""
|
let (mut crates, moves) = parse(input).expect("parsing failed");
|
||||||
|
let mut out: Vec<String> = vec![String::new(); crates.len()];
|
||||||
|
|
||||||
|
for m in moves {
|
||||||
|
let mut items = {
|
||||||
|
let mut items = Vec::with_capacity(m[0] as usize);
|
||||||
|
let from: &mut Vec<char> = crates[m[1] as usize].as_mut();
|
||||||
|
for _ in 0..m[0] {
|
||||||
|
items.push(from.pop().expect("AOC please don't hate us"));
|
||||||
|
}
|
||||||
|
items.reverse();
|
||||||
|
items
|
||||||
|
};
|
||||||
|
crates[m[2] as usize].append(&mut items);
|
||||||
|
}
|
||||||
|
|
||||||
|
for v in crates.iter_mut().skip(1) {
|
||||||
|
out.push(v.pop().expect("yes").to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
out.join("")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user