[TASK] Solved day 2
This commit is contained in:
52
Cargo.lock
generated
52
Cargo.lock
generated
@@ -3,3 +3,55 @@
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "advent-of-code-2020-rust"
|
name = "advent-of-code-2020-rust"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "0.7.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
"thread_local",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thread_local"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
]
|
||||||
|
|||||||
@@ -7,3 +7,5 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
1000
input/day2.txt
Normal file
1000
input/day2.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
input/day2_example.txt
Normal file
3
input/day2_example.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
1-3 a: abcde
|
||||||
|
1-3 b: cdefg
|
||||||
|
2-9 c: ccccccccc
|
||||||
@@ -35,8 +35,6 @@ fn find_pairs_summing_to(nums: &Vec<u32>, target: u32) -> (u32, u32) {
|
|||||||
|
|
||||||
fn find_triplet_summing_to(nums: &Vec<u32>, target: u32) -> (u32, u32, u32){
|
fn find_triplet_summing_to(nums: &Vec<u32>, target: u32) -> (u32, u32, u32){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for i in 0..nums.len() {
|
for i in 0..nums.len() {
|
||||||
for j in (0..nums.len()).rev() {
|
for j in (0..nums.len()).rev() {
|
||||||
|
|
||||||
|
|||||||
64
src/day2.rs
Normal file
64
src/day2.rs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
use super::util;
|
||||||
|
use regex::{Regex, Captures};
|
||||||
|
use std::ops::BitXor;
|
||||||
|
|
||||||
|
pub fn solve() {
|
||||||
|
let lines = util::read_file("input/day2.txt");
|
||||||
|
let passwords: Vec<Password> = lines.iter().map(|p| parse(p)).collect();
|
||||||
|
|
||||||
|
let part1 = passwords.iter().filter(|p| p.is_valid()).count();
|
||||||
|
println!("Day 2 Part 1: {}", part1);
|
||||||
|
|
||||||
|
let part2 = passwords.iter().filter(|p| p.is_valid2()).count();
|
||||||
|
println!("Day 2 Part 2: {}", part2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fn parse(line: &String) -> Password {
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref RE: Regex = Regex::new(r"^(\d+)-(\d+) ([a-zA-Z]): ([a-zA-Z]+)$").unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
if !RE.is_match(line.as_str()) {
|
||||||
|
panic!("Invalid line: {}", line);
|
||||||
|
}
|
||||||
|
|
||||||
|
let caps = RE.captures(line).unwrap();
|
||||||
|
return Password {
|
||||||
|
min: get_capture(&caps, 1).parse::<u32>().unwrap(),
|
||||||
|
max: get_capture(&caps, 2).parse::<u32>().unwrap(),
|
||||||
|
c: get_capture(&caps, 3).chars().next().unwrap(),
|
||||||
|
password: String::from(get_capture(&caps, 4))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_capture(caps: &Captures, id: usize) -> String {
|
||||||
|
return caps.get(id).map_or(String::from(""), |m| String::from(m.as_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[derive(Debug)]
|
||||||
|
struct Password {
|
||||||
|
pub min: u32,
|
||||||
|
pub max: u32,
|
||||||
|
pub c: char,
|
||||||
|
pub password: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Password {
|
||||||
|
|
||||||
|
fn is_valid(&self) -> bool {
|
||||||
|
let occ = self.password.chars().filter(|pc| pc == &self.c).count() as u32;
|
||||||
|
return occ >= self.min && occ <= self.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid2(&self) -> bool {
|
||||||
|
|
||||||
|
return (self.password.chars().nth((self.min - 1) as usize) == Option::from(self.c)).bitxor(
|
||||||
|
self.password.chars().nth((self.max - 1) as usize) == Option::from(self.c));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/dayX.rs
Normal file
5
src/dayX.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
use super::util;
|
||||||
|
|
||||||
|
pub fn solve() {
|
||||||
|
let lines = util::read_file("input/dayX.txt");
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
#[macro_use] extern crate lazy_static;
|
||||||
|
|
||||||
mod util;
|
mod util;
|
||||||
mod day1;
|
mod day1;
|
||||||
|
mod day2;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
@@ -13,11 +15,13 @@ fn main() {
|
|||||||
if day_arg_idx.is_some() {
|
if day_arg_idx.is_some() {
|
||||||
match args[day_arg_idx.unwrap() + 1].parse::<u8>().unwrap() {
|
match args[day_arg_idx.unwrap() + 1].parse::<u8>().unwrap() {
|
||||||
1 => day1::solve(),
|
1 => day1::solve(),
|
||||||
|
2 => day2::solve(),
|
||||||
_ => println!("This day is not yet implemented")
|
_ => println!("This day is not yet implemented")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Solve all days:
|
// Solve all days:
|
||||||
day1::solve();
|
day1::solve();
|
||||||
|
day2::solve();
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Execution took {} μs", now.elapsed().as_micros());
|
println!("Execution took {} μs", now.elapsed().as_micros());
|
||||||
|
|||||||
Reference in New Issue
Block a user