Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day01-inverse_captcha"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,50 @@
\--- Day 1: Inverse Captcha ---
----------
The night before Christmas, one of Santa's Elves calls you in a panic. "The printer's broken! We can't print the *Naughty or Nice List*!" By the time you make it to sub-basement 17, there are only a few minutes until midnight. "We have a big problem," she says; "there must be almost *fifty* bugs in this system, but nothing else can print The List. Stand in this square, quick! There's no time to explain; if you can convince them to pay you in *stars*, you'll be able to--" She pulls a lever and the world goes blurry.
When your eyes can focus again, everything seems a lot more pixelated than before. She must have sent you inside the computer! You check the system clock: *25 milliseconds* until midnight. With that much time, you should be able to collect all *fifty stars* by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each ~~day~~ millisecond in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants *one star*. Good luck!
You're standing in a room with "digitization quarantine" written in LEDs along one wall. The only door is locked, but it includes a small interface. "Restricted Area - Strictly No Digitized Users Allowed."
It goes on to explain that you may only leave by solving a [captcha](https://en.wikipedia.org/wiki/CAPTCHA) to prove you're *not* a human. Apparently, you only get one millisecond to solve the captcha: too fast for a normal human, but it feels like hours to you.
The captcha requires you to review a sequence of digits (your puzzle input) and find the *sum* of all digits that match the *next* digit in the list. The list is circular, so the digit after the last digit is the *first* digit in the list.
For example:
* `1122` produces a sum of `3` (`1` + `2`) because the first digit (`1`) matches the second digit and the third digit (`2`) matches the fourth digit.
* `1111` produces `4` because each digit (all `1`) matches the next.
* `1234` produces `0` because no digit matches the next.
* `91212129` produces `9` because the only digit that matches the next one is the last digit, `9`.
*What is the solution* to your captcha?
Your puzzle answer was `1182`.
\--- Part Two ---
----------
You notice a progress bar that jumps to 50% completion. Apparently, the door isn't yet satisfied, but it did emit a *star* as encouragement. The instructions change:
Now, instead of considering the *next* digit, it wants you to consider the digit *halfway around* the circular list. That is, if your list contains `10` items, only include a digit in your sum if the digit `10/2 = 5` steps forward matches it. Fortunately, your list has an even number of elements.
For example:
* `1212` produces `6`: the list contains `4` items, and all four digits match the digit `2` items ahead.
* `1221` produces `0`, because every comparison is between a `1` and a `2`.
* `123425` produces `4`, because both `2`s match each other, but no other digit has a match.
* `123123` produces `12`.
* `12131415` produces `4`.
*What is the solution* to your new captcha?
Your puzzle answer was `1152`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to [admire your Advent calendar](/2017).
If you still want to see it, you can [get your puzzle input](1/input).

View file

@ -0,0 +1,63 @@
pub fn run(input: &str) -> (usize, usize) {
let in_bytes = input.as_bytes();
let first = in_bytes.windows(2)
.filter(|b| b[0] == b[1])
.map(|b| (b[0] - b'0') as usize)
.sum::<usize>() +
if in_bytes.first().unwrap() == in_bytes.last().unwrap() {
(*in_bytes.first().unwrap() - b'0') as usize
} else {
0
};
let half = in_bytes.len()/2;
let second = (0..half)
.filter(|i| in_bytes[*i] == in_bytes[*i+half])
.map(|i| (in_bytes[i] - b'0') as usize)
.sum::<usize>() * 2;
(first, second)
}
#[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_inputs = [
"1122",
"1111",
"1234",
"91212129",
"1212",
"1221",
"123425",
"123123",
"12131415",
];
let expected = [
(3,0),
(4,4),
(0,0),
(9,6),
(0,6),
(3,0),
(0,4),
(0,12),
(0,4),
];
for (idx, sample_input) in sample_inputs.iter().enumerate() {
assert_eq!(run(sample_input), expected[idx]);
}
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (1182, 1152));
}
}

View file

@ -0,0 +1 @@
61697637962276641366442297247367117738114719863473648131982449728688116728695866572989524473392982963976411147683588415878214189996163533584547175794158118148724298832798898333399786561459152644144669959887341481968319172987357989785791366732849932788343772112176614723858474959919713855398876956427631354172668133549845585632211935573662181331613137869866693259374322169811683635325321597242889358147123358117774914653787371368574784376721652181792371635288376729784967526824915192526744935187989571347746222113625577963476141923187534658445615596987614385911513939292257263723518774888174635963254624769684533531443745729344341973746469326838186248448483587477563285867499956446218775232374383433921835993136463383628861115573142854358943291148766299653633195582135934544964657663198387794442443531964615169655243652696782443394639169687847463721585527947839992182415393199964893658322757634675274422993237955354185194868638454891442893935694454324235968155913963282642649968153284626154111478389914316765783434365458352785868895582488312334931317935669453447478936938533669921165437373741448378477391812779971528975478298688754939216421429251727555596481943322266289527996672856387648674166997731342558986575258793261986817177487197512282162964167151259485744835854547513341322647732662443512251886771887651614177679229984271191292374755915457372775856178539965131319568278252326242615151412772254257847413799811417287481321745372879513766235745347872632946776538173667371228977212143996391617974367923439923774388523845589769341351167311398787797583543434725374343611724379399566197432154146881344528319826434554239373666962546271299717743591225567564655511353255197516515213963862383762258959957474789718564758843367325794589886852413314713698911855183778978722558742329429867239261464773646389484318446574375323674136638452173815176732385468675215264736786242866295648997365412637499692817747937982628518926381939279935993712418938567488289246779458432179335139731952167527521377546376518126276