Added day3
This commit is contained in:
parent
a744398d4b
commit
dac7cb61ff
|
@ -1,4 +1,3 @@
|
||||||
#![feature(iterator_try_collect)]
|
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
#![feature(iterator_try_collect)]
|
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
|
|
47
src/day3/1.rs
Normal file
47
src/day3/1.rs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#![feature(test)]
|
||||||
|
extern crate test;
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
||||||
|
|
||||||
|
fn parse(input: &'static str) -> Vec<char> {
|
||||||
|
input.trim().chars().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solution(input: Vec<char>) -> usize {
|
||||||
|
let mut set = HashSet::new();
|
||||||
|
let (mut sx, mut sy) = (0, 0);
|
||||||
|
|
||||||
|
for c in input {
|
||||||
|
match c {
|
||||||
|
'v' => sx += 1,
|
||||||
|
'>' => sy += 1,
|
||||||
|
'^' => sx -= 1,
|
||||||
|
'<' => sy -= 1,
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
set.insert((sx, sy));
|
||||||
|
}
|
||||||
|
|
||||||
|
set.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
for input in INPUTS.iter() {
|
||||||
|
let output = parse(input);
|
||||||
|
let output = solution(output);
|
||||||
|
println!("{output}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
|
b.iter(|| {
|
||||||
|
let input = parse(INPUTS[1]);
|
||||||
|
let result = solution(input);
|
||||||
|
test::black_box(result);
|
||||||
|
})
|
||||||
|
}
|
58
src/day3/2.rs
Normal file
58
src/day3/2.rs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#![feature(test)]
|
||||||
|
extern crate test;
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
||||||
|
|
||||||
|
fn parse(input: &'static str) -> Vec<char> {
|
||||||
|
input.trim().chars().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solution(input: Vec<char>) -> usize {
|
||||||
|
let mut set = HashSet::new();
|
||||||
|
let (mut sx, mut sy) = (0, 0);
|
||||||
|
let (mut rsx, mut rsy) = (0, 0);
|
||||||
|
|
||||||
|
set.insert((sx, sy));
|
||||||
|
|
||||||
|
for (i, c) in input.into_iter().enumerate() {
|
||||||
|
match (i % 2, c) {
|
||||||
|
(1, 'v') => rsx += 1,
|
||||||
|
(1, '>') => rsy += 1,
|
||||||
|
(1, '^') => rsx -= 1,
|
||||||
|
(1, '<') => rsy -= 1,
|
||||||
|
(0, 'v') => sx += 1,
|
||||||
|
(0, '>') => sy += 1,
|
||||||
|
(0, '^') => sx -= 1,
|
||||||
|
(0, '<') => sy -= 1,
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
if i % 2 == 0 {
|
||||||
|
set.insert((sx, sy));
|
||||||
|
} else {
|
||||||
|
set.insert((rsx, rsy));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
set.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
for input in INPUTS.iter() {
|
||||||
|
let output = parse(input);
|
||||||
|
let output = solution(output);
|
||||||
|
println!("{output}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
|
b.iter(|| {
|
||||||
|
let input = parse(INPUTS[1]);
|
||||||
|
let result = solution(input);
|
||||||
|
test::black_box(result);
|
||||||
|
})
|
||||||
|
}
|
1
src/day3/input.txt
Normal file
1
src/day3/input.txt
Normal file
File diff suppressed because one or more lines are too long
1
src/day3/sample.txt
Normal file
1
src/day3/sample.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
^>v<
|
Loading…
Reference in New Issue
Block a user