Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2019/day04_secure_container/Cargo.toml
Normal file
8
2019/day04_secure_container/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day04_secure_container"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
39
2019/day04_secure_container/challenge.txt
Normal file
39
2019/day04_secure_container/challenge.txt
Normal file
|
@ -0,0 +1,39 @@
|
|||
You arrive at the Venus fuel depot only to discover it's protected by a password. The Elves had written the password on a sticky note, but someone threw it out.
|
||||
|
||||
However, they do remember a few key facts about the password:
|
||||
|
||||
* It is a six-digit number.
|
||||
* The value is within the range given in your puzzle input.
|
||||
* Two adjacent digits are the same (like `22` in `1*22*345`).
|
||||
* Going from left to right, the digits *never decrease*; they only ever increase or stay the same (like `111123` or `135679`).
|
||||
|
||||
Other than the range rule, the following are true:
|
||||
|
||||
* `111111` meets these criteria (double `11`, never decreases).
|
||||
* `2234*50*` does not meet these criteria (decreasing pair of digits `50`).
|
||||
* `123789` does not meet these criteria (no double).
|
||||
|
||||
*How many different passwords* within the range given in your puzzle input meet these criteria?
|
||||
|
||||
Your puzzle answer was `921`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
An Elf just remembered one more important detail: the two adjacent matching digits *are not part of a larger group of matching digits*.
|
||||
|
||||
Given this additional criterion, but still ignoring the range rule, the following are now true:
|
||||
|
||||
* `112233` meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
|
||||
* `123*444*` no longer meets the criteria (the repeated `44` is part of a larger group of `444`).
|
||||
* `111122` meets the criteria (even though `1` is repeated more than twice, it still contains a double `22`).
|
||||
|
||||
*How many different passwords* within the range given in your puzzle input meet all of the criteria?
|
||||
|
||||
Your puzzle answer was `603`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2019) and try another puzzle.
|
||||
|
||||
Your puzzle input was `278384-824795`.
|
66
2019/day04_secure_container/src/lib.rs
Normal file
66
2019/day04_secure_container/src/lib.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let range: Vec<_> = input.split('-').map(|n| n.parse::<usize>().unwrap()).collect();
|
||||
let valid_1: Vec<_> = (range[0]..=range[1]).filter(is_valid_1).collect();
|
||||
let first = valid_1.len();
|
||||
let second = valid_1.into_iter().filter(is_valid_2).count();
|
||||
(first, second)
|
||||
}
|
||||
|
||||
fn is_valid_1(password: &usize) -> bool {
|
||||
let mut double_found = false;
|
||||
let mut last_digit = password % 10;
|
||||
let mut remaining = password / 10;
|
||||
while remaining > 0 {
|
||||
let this_digit = remaining % 10;
|
||||
if this_digit > last_digit {
|
||||
return false;
|
||||
}
|
||||
if this_digit == last_digit {
|
||||
double_found = true;
|
||||
}
|
||||
last_digit = this_digit;
|
||||
remaining /= 10;
|
||||
}
|
||||
double_found
|
||||
}
|
||||
|
||||
fn is_valid_2(password: &usize) -> bool {
|
||||
let mut current_group_len = 1;
|
||||
let mut last_digit = password % 10;
|
||||
let mut remaining = password / 10;
|
||||
while remaining > 0 {
|
||||
let this_digit = remaining % 10;
|
||||
if this_digit == last_digit {
|
||||
current_group_len += 1;
|
||||
} else if current_group_len == 2 {
|
||||
return true;
|
||||
} else {
|
||||
current_group_len = 1;
|
||||
}
|
||||
last_digit = this_digit;
|
||||
remaining /= 10;
|
||||
}
|
||||
current_group_len == 2
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), (36, 14));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), (921, 603));
|
||||
}
|
||||
}
|
1
2019/day04_secure_container/tests/challenge_input
Normal file
1
2019/day04_secure_container/tests/challenge_input
Normal file
|
@ -0,0 +1 @@
|
|||
278384-824795
|
1
2019/day04_secure_container/tests/sample_input
Normal file
1
2019/day04_secure_container/tests/sample_input
Normal file
|
@ -0,0 +1 @@
|
|||
111200-111300
|
Loading…
Add table
Add a link
Reference in a new issue