Added day 2 pt 1
This commit is contained in:
parent
be1be5f25a
commit
f8ce19bf3c
|
@ -0,0 +1,8 @@
|
||||||
|
forward 5
|
||||||
|
down 5
|
||||||
|
forward 8
|
||||||
|
up 3
|
||||||
|
down 8
|
||||||
|
forward 2
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
55
src/main.rs
55
src/main.rs
|
@ -1,49 +1,58 @@
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
const INPUT: &'static str = include_str!("../inputs/day1.txt");
|
const INPUT: &'static str = include_str!("../inputs/day2.txt");
|
||||||
|
|
||||||
fn depths() -> Vec<u64> {
|
enum Moves {
|
||||||
INPUT
|
Forward(i32),
|
||||||
.lines()
|
Down(i32),
|
||||||
.filter(|&x| !x.is_empty())
|
Up(i32),
|
||||||
.map(|x| x.parse::<u64>().unwrap())
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn solution(depths: &[u64]) -> i32 {
|
fn cmds() -> impl Iterator<Item = Moves> {
|
||||||
let mut count = 0;
|
INPUT.lines().filter(|&x| !x.is_empty()).map(|x| {
|
||||||
|
let x: Vec<&str> = x.split_ascii_whitespace().collect();
|
||||||
|
|
||||||
// For a window size of 3, We can consider a window size of 4
|
match x[0] {
|
||||||
// A + B + C && B + C + D
|
"forward" => Moves::Forward(x[1].parse::<i32>().unwrap()),
|
||||||
// Since B, C are shared between the two consecutive sets,
|
"down" => Moves::Down(x[1].parse::<i32>().unwrap()),
|
||||||
// We only need to compare A and D
|
"up" => Moves::Up(x[1].parse::<i32>().unwrap()),
|
||||||
for set in depths.windows(4) {
|
_ => unreachable!(),
|
||||||
if set[0] < set[3] {
|
}
|
||||||
count += 1;
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solution(moves: impl Iterator<Item = Moves>) -> i32 {
|
||||||
|
let mut vertical = 0;
|
||||||
|
let mut horizontal = 0;
|
||||||
|
|
||||||
|
for mmove in moves {
|
||||||
|
match mmove {
|
||||||
|
Moves::Forward(v) => horizontal += v,
|
||||||
|
Moves::Down(v) => vertical += v,
|
||||||
|
Moves::Up(v) => vertical -= v,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
count
|
vertical * horizontal
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let depths = &depths();
|
let moves = cmds();
|
||||||
let count = solution(depths);
|
let count = solution(moves);
|
||||||
println!("increased {} times", count);
|
println!("increased {} times", count);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn solution_bench(b: &mut test::Bencher) {
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
let depths = depths();
|
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let v = solution(&depths);
|
let moves = cmds();
|
||||||
|
let v = solution(moves);
|
||||||
test::black_box(v);
|
test::black_box(v);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn solution_test() {
|
fn solution_test() {
|
||||||
assert_eq!(solution(&depths()), 1429);
|
assert_eq!(solution(cmds()), 1429);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue