Added day 5 part 1
This commit is contained in:
parent
e04bf59dd3
commit
fbdcc46626
1515
inputs/input.txt
1515
inputs/input.txt
File diff suppressed because it is too large
Load Diff
|
@ -1,7 +1,10 @@
|
|||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
[D]
|
||||
[N] [C]
|
||||
[Z] [M] [P]
|
||||
1 2 3
|
||||
|
||||
move 1 from 2 to 1
|
||||
move 3 from 1 to 3
|
||||
move 2 from 2 to 1
|
||||
move 1 from 1 to 2
|
||||
|
||||
|
|
128
src/main.rs
128
src/main.rs
|
@ -1,44 +1,85 @@
|
|||
#![feature(test)]
|
||||
extern crate test;
|
||||
|
||||
const INPUTS: [&[u8]; 2] = [
|
||||
include_bytes!("../inputs/sample.txt"),
|
||||
include_bytes!("../inputs/input.txt"),
|
||||
const INPUTS: [&str; 2] = [
|
||||
include_str!("../inputs/sample.txt"),
|
||||
include_str!("../inputs/input.txt"),
|
||||
];
|
||||
|
||||
fn parse(input: &[u8]) -> Vec<((u8, u8), (u8, u8))> {
|
||||
input
|
||||
.split(|&c| c == b'\n')
|
||||
.filter(|line| !line.is_empty())
|
||||
.map(|line| {
|
||||
let mut line = line.splitn(2, |&c| c == b'-');
|
||||
let e1 = line.next().unwrap();
|
||||
#[derive(Debug)]
|
||||
struct Stack {
|
||||
stacks: Vec<Vec<char>>,
|
||||
instructions: Vec<Instruction>,
|
||||
}
|
||||
|
||||
let rest = line.next().unwrap();
|
||||
let mut line = rest.split(|&c| c == b',');
|
||||
let e2 = line.next().unwrap();
|
||||
#[derive(Debug)]
|
||||
struct Instruction {
|
||||
count: usize,
|
||||
from: usize,
|
||||
to: usize,
|
||||
}
|
||||
|
||||
let rest = line.next().unwrap();
|
||||
let mut line = rest.splitn(2, |&x| x == b'-');
|
||||
let e3 = line.next().unwrap();
|
||||
let e4 = line.next().unwrap();
|
||||
fn parse(input: &'static str) -> Stack {
|
||||
let mut v = input.split("\n\n");
|
||||
|
||||
(
|
||||
(
|
||||
e1.get(1)
|
||||
.map_or(e1[0] - b'0', |c| ((e1[0] - b'0') * 10 + c - b'0')),
|
||||
e2.get(1)
|
||||
.map_or(e2[0] - b'0', |c| ((e2[0] - b'0') * 10 + c - b'0')),
|
||||
),
|
||||
(
|
||||
e3.get(1)
|
||||
.map_or(e3[0] - b'0', |c| ((e3[0] - b'0') * 10 + c - b'0')),
|
||||
e4.get(1)
|
||||
.map_or(e4[0] - b'0', |c| ((e4[0] - b'0') * 10 + c - b'0')),
|
||||
),
|
||||
)
|
||||
let mut stack_input = v
|
||||
.next()
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|c| c.chars().collect::<Vec<char>>())
|
||||
.rev();
|
||||
let mut stacks: Vec<Vec<char>> = vec![];
|
||||
|
||||
let stack_names = stack_input.next().unwrap();
|
||||
|
||||
let containers: Vec<Vec<char>> = stack_input.collect();
|
||||
|
||||
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() {
|
||||
|
@ -46,21 +87,24 @@ fn main() {
|
|||
let output = parse(input);
|
||||
let score = solution(output);
|
||||
|
||||
println!("{:?}", score);
|
||||
println!("{}", score);
|
||||
}
|
||||
}
|
||||
|
||||
fn solution(input: Vec<((u8, u8), (u8, u8))>) -> usize {
|
||||
let mut score = 0;
|
||||
|
||||
for ((a0, a1), (b0, b1)) in input {
|
||||
score += ((a0 <= b0 && a1 >= b1)
|
||||
|| (b0 <= a0 && b1 >= a1)
|
||||
|| (b0 >= a0 && b0 <= a1)
|
||||
|| (a0 >= b0 && a0 <= b1)) as usize;
|
||||
fn solution(mut input: Stack) -> String {
|
||||
for ins in input.instructions {
|
||||
for _ in 0..ins.count {
|
||||
if let Some(v) = input.stacks[ins.from - 1].pop() {
|
||||
input.stacks[ins.to - 1].push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
score
|
||||
input
|
||||
.stacks
|
||||
.into_iter()
|
||||
.map(|c| *c.last().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
|
Loading…
Reference in New Issue
Block a user