added 2015 day5
This commit is contained in:
parent
db8561b472
commit
c5d876a05c
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -7,6 +7,7 @@ name = "advent_of_code"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"md5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -14,3 +15,9 @@ name = "anyhow"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
|
||||
|
||||
[[package]]
|
||||
name = "md5"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
|
||||
|
||||
@ -6,3 +6,5 @@ edition = "2021"
|
||||
[dependencies]
|
||||
|
||||
anyhow = "1.0"
|
||||
#2015 - day3
|
||||
md5 = "0.7.0"
|
||||
1000
ressources/2015/day5
Normal file
1000
ressources/2015/day5
Normal file
File diff suppressed because it is too large
Load Diff
49
src/advent2015/day4.rs
Normal file
49
src/advent2015/day4.rs
Normal file
@ -0,0 +1,49 @@
|
||||
pub fn run() -> (usize, usize) {
|
||||
let input = "iwrupvqb";
|
||||
(
|
||||
find_witz_leading_zeros(input, 5),
|
||||
find_witz_leading_zeros(input, 6),
|
||||
)
|
||||
}
|
||||
|
||||
fn find_witz_leading_zeros(input: &str, zeros: usize) -> usize {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
i += 1;
|
||||
|
||||
if i % 100000 == 0 {
|
||||
println!("Done: {}", i);
|
||||
}
|
||||
|
||||
let hash = format!("{:x}", md5::compute(format!("{}{}", input, i)));
|
||||
if hash.starts_with(&"0".repeat(zeros)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
--- Day 4: The Ideal Stocking Stuffer ---
|
||||
|
||||
Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.
|
||||
|
||||
To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash.
|
||||
|
||||
For example:
|
||||
|
||||
If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so.
|
||||
If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
|
||||
|
||||
Your puzzle answer was 346386.
|
||||
--- Part Two ---
|
||||
|
||||
Now find one that starts with six zeroes.
|
||||
|
||||
Your puzzle answer was 9958218.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your Advent calendar and try another puzzle.
|
||||
|
||||
Your puzzle input was iwrupvqb.
|
||||
*/
|
||||
113
src/advent2015/day5.rs
Normal file
113
src/advent2015/day5.rs
Normal file
@ -0,0 +1,113 @@
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
use crate::helper::file::read_file;
|
||||
|
||||
pub fn run() -> (usize, usize) {
|
||||
test();
|
||||
let path = PathBuf::from("ressources/2015/day5");
|
||||
let input = read_file(path).unwrap();
|
||||
let nice_word_cnt = input.lines().filter(|x| is_nice(x)).count();
|
||||
let new_nice_word_cnt = input.lines().filter(|x| is_new_nice(x)).count();
|
||||
(nice_word_cnt, new_nice_word_cnt)
|
||||
}
|
||||
|
||||
fn test() {
|
||||
let input = r#"ugknbfddgicrmopn
|
||||
haegwjzuvuyypxyu"#;
|
||||
for line in input.lines() {
|
||||
println!("{} is nice: {}", line, is_nice(line));
|
||||
println!("{} is new nice: {}", line, is_new_nice(line));
|
||||
}
|
||||
}
|
||||
|
||||
fn is_nice(input: &str) -> bool {
|
||||
let vowels = vec!['a', 'e', 'i', 'o', 'u'];
|
||||
let naughty_strings = vec!["ab", "cd", "pq", "xy"];
|
||||
|
||||
if input.chars().filter(|c| vowels.contains(c)).count() < 3 {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !input
|
||||
.chars()
|
||||
.zip(input.chars().skip(1))
|
||||
.any(|(a, b)| a == b)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if input.chars().zip(input.chars().skip(1)).any(|(a, b)| {
|
||||
let pair = &format!("{}{}", a, b);
|
||||
naughty_strings.contains(&&pair[..])
|
||||
}) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn is_new_nice(input: &str) -> bool {
|
||||
if !input
|
||||
.chars()
|
||||
.zip(input.chars().skip(1))
|
||||
.zip(input.chars().skip(2))
|
||||
.any(|((a, _), c)| a == c)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut map = HashMap::new();
|
||||
for (idx, (c1, c2)) in input.chars().zip(input.chars().skip(1)).enumerate() {
|
||||
if let Some(prev_idx) = map.get(&(c1, c2)) {
|
||||
if idx - prev_idx > 1 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
map.insert((c1, c2), idx);
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/*
|
||||
--- Day 5: Doesn't He Have Intern-Elves For This? ---
|
||||
|
||||
Santa needs help figuring out which strings in his text file are naughty or nice.
|
||||
|
||||
A nice string is one with all of the following properties:
|
||||
|
||||
It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou.
|
||||
It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd).
|
||||
It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
|
||||
|
||||
For example:
|
||||
|
||||
ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...), a double letter (...dd...), and none of the disallowed substrings.
|
||||
aaa is nice because it has at least three vowels and a double letter, even though the letters used by different rules overlap.
|
||||
jchzalrnumimnmhp is naughty because it has no double letter.
|
||||
haegwjzuvuyypxyu is naughty because it contains the string xy.
|
||||
dvszwmarrgswjxmb is naughty because it contains only one vowel.
|
||||
|
||||
How many strings are nice?
|
||||
|
||||
Your puzzle answer was 258.
|
||||
--- Part Two ---
|
||||
|
||||
Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
|
||||
|
||||
Now, a nice string is one with all of the following properties:
|
||||
|
||||
It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
|
||||
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
|
||||
|
||||
For example:
|
||||
|
||||
qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz).
|
||||
xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap.
|
||||
uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them.
|
||||
ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
|
||||
|
||||
How many strings are nice under these new rules?
|
||||
|
||||
Your puzzle answer was 53.
|
||||
*/
|
||||
@ -1,3 +1,5 @@
|
||||
pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
pub mod day4;
|
||||
pub mod day5;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#![feature(random)]
|
||||
use std::time::Instant;
|
||||
|
||||
use advent_of_code::advent2015::day3::run;
|
||||
use advent_of_code::advent2015::day5::run;
|
||||
|
||||
fn main() {
|
||||
let now = Instant::now();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user