1
0

Added day 5 part 1

This commit is contained in:
Ishan Jain 2022-12-05 11:00:13 +05:30
parent e04bf59dd3
commit fbdcc46626
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
3 changed files with 610 additions and 1048 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,10 @@
2-4,6-8 [D]
2-3,4-5 [N] [C]
5-7,7-9 [Z] [M] [P]
2-8,3-7 1 2 3
6-6,4-6
2-6,4-8 move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

View File

@ -1,44 +1,85 @@
#![feature(test)] #![feature(test)]
extern crate test; extern crate test;
const INPUTS: [&[u8]; 2] = [ const INPUTS: [&str; 2] = [
include_bytes!("../inputs/sample.txt"), include_str!("../inputs/sample.txt"),
include_bytes!("../inputs/input.txt"), include_str!("../inputs/input.txt"),
]; ];
fn parse(input: &[u8]) -> Vec<((u8, u8), (u8, u8))> { #[derive(Debug)]
input struct Stack {
.split(|&c| c == b'\n') stacks: Vec<Vec<char>>,
.filter(|line| !line.is_empty()) instructions: Vec<Instruction>,
.map(|line| { }
let mut line = line.splitn(2, |&c| c == b'-');
let e1 = line.next().unwrap();
let rest = line.next().unwrap(); #[derive(Debug)]
let mut line = rest.split(|&c| c == b','); struct Instruction {
let e2 = line.next().unwrap(); count: usize,
from: usize,
to: usize,
}
let rest = line.next().unwrap(); fn parse(input: &'static str) -> Stack {
let mut line = rest.splitn(2, |&x| x == b'-'); let mut v = input.split("\n\n");
let e3 = line.next().unwrap();
let e4 = line.next().unwrap();
( let mut stack_input = v
( .next()
e1.get(1) .unwrap()
.map_or(e1[0] - b'0', |c| ((e1[0] - b'0') * 10 + c - b'0')), .lines()
e2.get(1) .map(|c| c.chars().collect::<Vec<char>>())
.map_or(e2[0] - b'0', |c| ((e2[0] - b'0') * 10 + c - b'0')), .rev();
), let mut stacks: Vec<Vec<char>> = vec![];
(
e3.get(1) let stack_names = stack_input.next().unwrap();
.map_or(e3[0] - b'0', |c| ((e3[0] - b'0') * 10 + c - b'0')),
e4.get(1) let containers: Vec<Vec<char>> = stack_input.collect();
.map_or(e4[0] - b'0', |c| ((e4[0] - b'0') * 10 + c - b'0')),
), for (i, v) in stack_names.into_iter().enumerate() {
) if v == ' ' {
continue;
}
let v: usize = (v as u8 - b'0') as usize;
if stacks.len() <= v - 1 {
stacks.push(vec![]);
}
for container in containers.iter() {
if container[i] == ' ' {
continue;
}
stacks[v - 1].push(container[i]);
}
}
let instructions: Vec<Instruction> = v
.next()
.unwrap()
.lines()
.map(|line: &str| {
let line: String = line
.chars()
.filter(|c| c.is_numeric() | c.is_whitespace())
.collect();
let numbers: Vec<u8> = line
.split(' ')
.filter(|c| !c.is_empty())
.map(|c| c.parse::<u8>().unwrap())
.collect();
Instruction {
count: numbers[0] as usize,
from: numbers[1] as usize,
to: numbers[2] as usize,
}
}) })
.collect() .collect();
Stack {
instructions,
stacks,
}
} }
fn main() { fn main() {
@ -46,21 +87,24 @@ fn main() {
let output = parse(input); let output = parse(input);
let score = solution(output); let score = solution(output);
println!("{:?}", score); println!("{}", score);
} }
} }
fn solution(input: Vec<((u8, u8), (u8, u8))>) -> usize { fn solution(mut input: Stack) -> String {
let mut score = 0; for ins in input.instructions {
for _ in 0..ins.count {
for ((a0, a1), (b0, b1)) in input { if let Some(v) = input.stacks[ins.from - 1].pop() {
score += ((a0 <= b0 && a1 >= b1) input.stacks[ins.to - 1].push(v);
|| (b0 <= a0 && b1 >= a1) }
|| (b0 >= a0 && b0 <= a1) }
|| (a0 >= b0 && a0 <= b1)) as usize;
} }
score input
.stacks
.into_iter()
.map(|c| *c.last().unwrap())
.collect()
} }
#[bench] #[bench]