days 5 + 6

master
george 2 years ago
parent 70894fa3a8
commit e56f7c64ea

7
day05/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day05"
version = "0.1.0"

@ -0,0 +1,8 @@
[package]
name = "day05"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,75 @@
#![feature(iter_next_chunk)]
use std::collections::VecDeque;
const OFFSET: usize = 4;
enum MachineType {
Type9000,
Type9001,
}
fn main() {
let f = std::fs::read_to_string("/dev/stdin").unwrap();
let (crates, instructions) = f.split_once("\n\n").unwrap();
let (crates, instructions) = (crates.lines(), instructions.lines());
let mut stacks: Vec<VecDeque<&str>> =
vec![VecDeque::new(); (f.lines().next().unwrap().len() / 4) + 1];
for crate_str in crates {
for i in (1..crate_str.len()).step_by(OFFSET) {
if let Some(crate_label) = crate_str.get(i..i + 1) {
if !crate_label.chars().next().unwrap().is_alphabetic() {
continue;
}
let vee = stacks.get_mut(i / 4).unwrap();
vee.push_front(crate_label);
}
}
}
let (mut reset_stacks, reset_instructions) = (stacks.clone(), instructions.clone());
solve(instructions, &mut stacks, MachineType::Type9000);
solve(reset_instructions, &mut reset_stacks, MachineType::Type9001);
}
fn solve(instructions: core::str::Lines, stacks: &mut [VecDeque<&str>], machine_type: MachineType) {
for instruction in instructions {
if let Ok([amount, source, dest]) = instruction
.split_whitespace()
.filter_map(|n| n.parse::<usize>().ok())
.next_chunk()
{
let mut temp = VecDeque::new();
let source_stack = stacks.get_mut(source - 1).unwrap();
for _ in 0..amount {
temp.push_front(source_stack.pop_back().unwrap());
}
let dest_stack = stacks.get_mut(dest - 1).unwrap();
match machine_type {
MachineType::Type9000 => {
for _ in 0..amount {
dest_stack.push_back(temp.pop_back().unwrap());
}
}
MachineType::Type9001 => {
if amount == 1 {
dest_stack.push_back(temp.pop_back().unwrap());
} else {
dest_stack.append(&mut temp);
}
}
}
}
}
if matches!(machine_type, MachineType::Type9000) {
print!("P1: ");
} else {
print!("P2: ");
}
for stack in stacks.iter_mut() {
print!("{}", stack.pop_back().unwrap());
}
println!();
}

16
day06/Cargo.lock generated

@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day06"
version = "0.1.0"
dependencies = [
"rustc-hash",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"

@ -0,0 +1,9 @@
[package]
name = "day06"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rustc-hash = "1.1.0"

@ -0,0 +1,17 @@
use rustc_hash::FxHashSet;
const PACKET_WINDOW_SIZE: usize = 4;
const MESSAGE_WINDOW_SIZE: usize = 14;
fn main() {
let s = std::fs::read_to_string("/dev/stdin").unwrap();
println!("P1: {}, P2: {}", find_index(&s, PACKET_WINDOW_SIZE), find_index(&s, MESSAGE_WINDOW_SIZE));
}
fn find_index(s: &str, window_size: usize) -> usize {
for (count, window) in s.chars().collect::<Vec<char>>().windows(window_size).enumerate(){
if FxHashSet::from_iter(window).len() == window_size {
return count + window_size;
}
}
0
}
Loading…
Cancel
Save