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 = "day02_inventory_management_system"
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,45 @@
--- Day 2: Inventory Management System ---
You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.
Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"
"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs..." They walk too far away to hear any more.
Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).
To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.
For example, if you see the following box IDs:
abcdef contains no letters that appear exactly two or three times.
bababc contains two a and three b, so it counts for both.
abbcde contains two b, but no letter appears exactly three times.
abcccd contains three c, but no letter appears exactly two times.
aabcdd contains two a and two d, but it only counts once.
abcdee contains two e.
ababab contains three a and three b, but it only counts once.
Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.
What is the checksum for your list of box IDs?
Your puzzle answer was 5952.
--- Part Two ---
Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
abcde
fghij
klmno
pqrst
fguij
axcye
wvxyz
The IDs abcde and axcye are close, but they differ by two characters (the second and fourth). However, the IDs fghij and fguij differ by exactly one character, the third (h and u). Those must be the correct boxes.
What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)
Your puzzle answer was krdmtuqjgwfoevnaboxglzjph.
Both parts of this puzzle are complete! They provide two gold stars: **

View file

@ -0,0 +1,61 @@
use std::collections::HashMap;
pub fn run(input: &str) -> (usize, String) {
let ids: Vec<_> = input.lines().map(to_id).collect();
let first = ids.iter().filter(|id| id.values().any(|v| v == &2)).count() * ids.iter().filter(|id| id.values().any(|v| v == &3)).count();
let mut known_ids: Vec<Vec<u8>> = Vec::new();
let mut second = String::new();
input.lines().for_each(|line| {
for known in &known_ids {
if let Some(idx) = idx_for_one_off(line.as_bytes(), known) {
let mut res = line.as_bytes().to_vec();
res.remove(idx);
second = String::from_utf8(res).unwrap();
return;
}
}
known_ids.push(line.as_bytes().to_vec());
});
(first, second)
}
fn idx_for_one_off(lhs: &[u8], rhs: &[u8]) -> Option<usize> {
if lhs.len() == rhs.len() {
let offsets: Vec<_> = (0..lhs.len()).filter(|i| lhs[*i] != rhs[*i]).collect();
if offsets.len() == 1 {
return Some(offsets[0]);
}
}
None
}
fn to_id(line: &str) -> HashMap<&u8, usize> {
let mut id = HashMap::new();
line.as_bytes().iter().for_each(|byte| {
id.entry(byte).and_modify(|count| *count += 1).or_insert(1);
});
id
}
#[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), (12, "fgij".to_string()));
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (5952, "krdmtuqjgwfoevnaboxglzjph".to_string()));
}
}

View file

@ -0,0 +1,250 @@
krdmtuqjmwfoevnadixyclzspv
yrdmtuqjiwfoevnabfxyclzsph
kqjvtuqjgwfoevnabixyclzsph
krdmtuqjgwjoevnaolxyclzsph
krdmtnqjgwfoevnabiiyxlzsph
lrymtuqjgwhoevnabixyclzsph
krdmguqjgwfoevnabixkclzsah
krdmtuqjgwfoevnibinyclzdph
krdmtucjgwfoevnabhxyclzspv
krdmtuqjgwfoevtabixyulzsuh
krdmtuqqgwfoevnabixdblzsph
krdmtuqjawfsevnabiyyclzsph
krdmtuqjgwfoevnabzxccldsph
krdmtcqegwfhevnabixyclzsph
krdmtuqjgwforvnaxixycgzsph
krdmtuqjgwfoqvnaxixyclzskh
krdmtutjgwfoevyajixyclzsph
krdmtuqmgwfoevnabixycxzspc
krdptuqjgwhoevkabixyclzsph
krdttuqjhwfoevnabixyclzspa
krdmtuqjgwfoevnabibyhnzsph
krdmtuqjywfoevntbidyclzsph
krdmtojdgwfoevnabixyclzsph
krdmtuqjgpfuevnauixyclzsph
krdmtoqjgwfrevjabixyclzsph
krdmtuqjgwfoyvndbixyclzyph
krdmtxqjgwfomvnayixyclzsph
crdmtuqjgwfoevnabixyoxzsph
krdmtpqjgwfdevnabixycqzsph
krdmtuqjgwfoevuabfxsclzsph
krdmtuqjgwfoevnybixycdzskh
krdmtusjgwfoevnabixxclzdph
krdmtuqjgwfoevnaboxyglzjph
zrdmtuqjgrfoevnalixyclzsph
krdmtuqjclfoevnabixyclzsih
kqdmtlqjgwfoevnabtxyclzsph
krdmtuqggwpoevnabixyclzlph
krdmtuqjgwfobwnrbixyclzsph
krdmtuqjgwfoevwabkxycnzsph
kldmtuqjgwfogvyabixyclzsph
krdmtuqvgwfoevnabixtcrzsph
krdmtuqjgwroevnabixyrlzspw
krdmtuqjgjfoevnabixyelzrph
krdmtuqjgffoevnaaixyclzspa
krdmtuqjgwfoevxabifywlzsph
krdmtuqjgwfoevlabixycrzsrh
krdmtuqjgwfpevnabixocqzsph
krdmtuqjgwfoevdabixycnhsph
krdmtmqjgwfoevnajixyclvsph
krdmtuqjjvfoevnabgxyclzsph
krzmtuqjgwfoevnabioyckzsph
kodmtwqjgwfoevnabieyclzsph
ehdmthqjgwfoevnabixyclzsph
krdmtuqjxwioevnabixyclbsph
grdmkutjgwfoevnabixyclzsph
krdutuqjgwfoebnabixaclzsph
krdmtuqjgwfoebnabixyclcjph
krdmteqjgwfoevnlbixycizsph
krdmtegjgwhoevnabixyclzsph
krdmtuqjgwfdrvnabixbclzsph
krdmtuqjgyfoevidbixyclzsph
krdmtubjawfoevnabixyclzuph
krdmtuqjgwfoavjabixyclzssh
krdmtuqjgwfoeonabixyclzsvo
vrdmtuqjgwffevnabixpclzsph
krdmtuqonwfoevnabixycfzsph
krdmtumjgwfpevnubixyclzsph
krdmtutjgwfoevnaciyyclzsph
krdrtuqjgwfoevnwbixyglzsph
krdmtuqjgwfoevbabixyclesdh
krdmtuqcgwfoevnabixyqdzsph
krdmtuqjgwfogvnabrxycezsph
krdmujqkgwfoevnabixyclzsph
krdmtuqjgtooevnabixyclzzph
jrdntuqjgwfoevnabixyclrsph
krdmtuqjgzfoevkebixyclzsph
krdmtuqjgwfosvnaeixyclztph
krdmtuqjgwfoevzabixydlzaph
krdmtuqzgwfoavnabiqyclzsph
krdmtuqvgwfoevnabixycwzspv
krdmvuqjgwteevnabixyclzsph
krdmtujjgwfoevgybixyclzsph
kydmtuqjgwfoeunacixyclzsph
krdmtuqjgifoqvnabicyclzsph
krnltiqjgwfoevnabixyclzsph
krdmtuqjgwfoevnabhxyclzsgi
kfdmtuqjnwfowvnabixyclzsph
kmdmtuljgwfoevnabixycvzsph
krdmtxqjgwaoevvabixyclzsph
kramduqjgwfoevnabixyclzwph
krdutuqjgwfoennabixyclziph
krdmvuqfgwfoevnacixyclzsph
krdmtuqogwfoevnabmvyclzsph
krdmfuqjgwfoyvnabixyclzseh
krdmtuqjgweoelnabixyclzspd
krdmtumjgwfoevnabixyclzypo
krdmtuqjgkfoevhabixyclzsqh
kjdmtuqjgwfoevgabixyclzsah
krdmtuqjgwfoevnlbixyclzsbw
mrdmtxqjgwfoevnabgxyclzsph
krdmtuqpgwfoevnhbixycltsph
krdmtuqjgwfmqvnabixyclzslh
krqmtuqogwfoevnaqixyclzsph
krdmtusjggfoevnabicyclzsph
krcmtuljgwfoevlabixyclzsph
krdmtuojgwfoeknabixyclzsrh
krdmtuqjtwfoevnabiypclzsph
krvmtupjgwfoevnabixycldsph
krdmtuxjgwfoevaabxxyclzsph
krdmtvqlgwfoehnabixyclzsph
wrdmtuqjgwfoevnabixyclzdpy
krdatuqlgwfoevnabixyclzsjh
krdmtuqjgwfoevpabkxyclzsjh
krdmtuqjgwqvsvnabixyclzsph
krdmtwqjgwfoevnobixyclzspm
krdmtuqjgssoevnabixyclgsph
krdmtuqjgwfoevnafixyclzbpp
krdmtuqjowfoevxabiuyclzsph
krdmtuqrgwfoevntbixyclzspu
krdmtucjgwfoevnabixcnlzsph
krddtuojgwfoevnabixyclzzph
krdmtuqjgwuoevnabiqycldsph
kpdmpuqjgwfoevnabixyclzslh
krdmtuqjgwfoewnabixyzxzsph
krdmtuejswfoevhabixyclzsph
krdmtuqggwfoevntbikyclzsph
krdmtuqjgwfoevnabixydlhnph
krdmtcqjglfoevnaxixyclzsph
krumyuqjgwfoevnrbixyclzsph
kgdmmuqjgwooevnabixyclzsph
krdmteqjgwfqevwabixyclzsph
krdmfuqjgwfpevnabixyclzspq
erdmtycjgwfoevnabixyclzsph
krdmcuqjgwfoevnabixjglzsph
krdmtuqjgtfoeunabixiclzsph
krdmtuqjgwfoevmqbixyclzspu
krlmtuqjvwfoevnabikyclzsph
krdotuqjgwfoevnagrxyclzsph
krdmtuqbgwfoefnabixyclasph
kwdmtuqjgwfosjnabixyclzsph
kydmtuqjgwfoevcabixycezsph
crdmtuqjgwloevnabixkclzsph
krimtuqhgwfoevnbbixyclzsph
krdmjuqagwfoevnabicyclzsph
krdmtuqdgzfoevnabixydlzsph
krdmtuqjgwwoevnaqixyclzspf
krdmtuqjgwfoevnabdxyzvzsph
krdmtuqjgwaofvnabixyclzsnh
krdmturjgwfmevnabixyclzspn
krdmvuqjgwboevnabixyolzsph
krdmtuqjgwfomvnabijyclzspx
bedmtuqjgwfoevnabixyslzsph
krdmtenjgwfoevnabixyclzsqh
krdmtuqugwfoevnabixpcdzsph
krdmtuqjgiloevnabrxyclzsph
krdmtupjcwfoevnabixyclwsph
kremtuqjgwfoevnabixyyjzsph
krdmtuqjgwnoovnabixyclzshh
qrdmtuqjgwfoevnabixyciasph
krdituqjgbfoevnagixyclzsph
krdmnoqjgwfoqvnabixyclzsph
krdmtuqegwfoevhkbixyclzsph
krdmkucjgwfoevnabixnclzsph
krdmtuqbnwpoevnabixyclzsph
krdmttqjgwfoevnabixyclbspz
srdmtubjgwfrevnabixyclzsph
krdmruqjzwfoevnabixyclesph
ardmtuqfgwwoevnabixyclzsph
yrumtuqjgwhoevnabixyclzsph
rrdmtuqjgwfoevnabsxycwzsph
krpmtuqjgwfoevdabixyclzzph
krdmuuqjgwfoevnabixyclriph
krdmtuqjgwfobvnabixyvgzsph
krdmbuujgwfoevnabixycczsph
krhmtuwjgwfoeqnabixyclzsph
krdwtuqjgwfoevnkbixyclzzph
krdmtuqjgwkoeqnabixyvlzsph
kadmtuqjgwfoednabcxyclzsph
krdmtyqqgwfoevnabizyclzsph
krdmtuqjgnfoevnabiyycmzsph
krdmtuqjcwfouvnabixyclznph
krdmtuqjjwfcevnqbixyclzsph
krdmtuqfgbfoevgabixyclzsph
kkdmtuqjgwfoevnapixyclzsth
nrdmtuqjgwtoevnakixyclzsph
krdmtuqjglfoevlabixdclzsph
zrdmtuqjgwfoevndbixbclzsph
krdmeuqjgwfoeenabixyclrsph
krdmoaqjzwfoevnabixyclzsph
krsmtuqjgwfoevnwbixyclzsfh
kadmtuqjgwfoqdnabixyclzsph
krsmtuqjgofoevnabixkclzsph
krdmtuqjdwfoevnibixdclzsph
mrdmtuqjgwfouvnabixyclzfph
trdmtlqjgwfoevnabixyclzjph
trdmyuqjgwfozvnabixyclzsph
krdmtiqjgwroevnabixyclzspk
erdmtutjgwftevnabixyclzsph
krdwyuqjgwfoevnaaixyclzsph
krdmthqbgwfoevnabixyclksph
krdmttqjgwfoivnabixyclvsph
krdmtuqjgwfoefnabixyflgsph
khdmtuqjgwfoevnajixyvlzsph
krdmtuqvgwfoevnasixyclzspt
krdmtuqjgkwogvnabixyclzsph
krdmtuqjgwfoevnaboxpglzjph
kadmtuqjgwfoxvnabixyclziph
krdmtuqjfwfoevnabaxycbzsph
krdjtuqjgwfoevnabiryhlzsph
krdvtuqjgpfoevnabcxyclzsph
brdmtuqjgwfoevnafixyqlzsph
krdmtuqjgwfoevnavixxcllsph
krdhtuqjkwfoevfabixyclzsph
krdmtuqjgjfoevnawixyclzsuh
krddtuqjgwfoeqnabiwyclzsph
krhmtuqjgwfnevnabinyclzsph
kedmtuqjgzfmevnabixyclzsph
qrdmtuqjgwfoevntbixyclzxph
krdmtuqsgwfoevnabixvclzrph
scdmtuqjgwfoevnabixtclzsph
krymtuqjgjfolvnabixyclzsph
krdmtuqjgwfkevnablxyclzskh
krymtuqjswfoevnabixyclzvph
krdmtuqjhwfoevnabixycwzspd
krdmtuxjgwfoevnabyxyclzzph
krdmtlqjgwfovvnabilyclzsph
krdmtuqjgwfoevnaaijcclzsph
krdatrqjgwfokvnabixyclzsph
krdmtuqjgwfoevnaxifyclzkph
krddtuqjgwfoevnabixccozsph
krdmtuqngwfoevnabiyycxzsph
krdmtumdgwfoevnqbixyclzsph
krdmtuqjgwfoevnabixyxlmsch
krdmtudzgwfoevnabixtclzsph
krdmtuqjgwfoevnpbixyclhspl
krdmtqqjgwjoevnabexyclzsph
kydmtuqzgwfoevnabixyclwsph
krdmeucjgwqoevnabixyclzsph
krdmtuqjghfoevjabixyclzspp
krdmtuqjgjfwevnabixyclzskh
krdmkuhjgwfoevnabipyclzsph
krdytuqjgwfoevnabibyclztph
krdmtuqjgwfpevnabisyzlzsph
kmdmtgqjgwfsevnabixyclzsph
krdmtuqjgsfoevnabijyclzszh
krdmtuqjgwfoevnabivyclzuuh
krdstuqjgrfoevnabixyclzspu
jrdmtuqjgwfotvnabixyclzspj
krdmrumjgwfoevnabixeclzsph
krpmtusjgwfoevnabioyclzsph

View file

@ -0,0 +1,14 @@
abcdef
bababc
abbcde
abcccd
aabcdd
abcdee
ababab
abcde
fghij
klmno
pqrst
fguij
axcye
wvxyz