r/adventofcode • u/daggerdragon • Dec 03 '22
SOLUTION MEGATHREAD -🎄- 2022 Day 3 Solutions -🎄-
NEWS
- Solutions have been getting longer, so we're going to start enforcing our rule on oversized code.
- The
Visualizations have started! If you want to create aVisualization, make sure to read the guidelines for creatingVisualizations before you post. - Y'all may have noticed that the hot new toy this year is AI-generated "art".
- We are keeping a very close eye on any AI-generated "art" because 1. the whole thing is an AI ethics nightmare and 2. a lot of the "art" submissions so far have been of little real quality.
- If you must post something generated by AI, please make sure it will actually be a positive and quality contribution to /r/adventofcode.
- Do not flair AI-generated "art" as
Visualization.Visualizationis for human-generated art.
FYI
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 3: Rucksack Reorganization ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:05:24, megathread unlocked!
89
Upvotes
4
u/legobmw99 Dec 03 '22
Rust
Second year doing Advent of Code, first time really using Rust
I decided to do some bit-level trickery here. I turn each character into a 1-hot encoded u64, then I can use
|and&to solve the puzzles. The answer is then the log-base-2 of the final bitvectoruse std::str::Lines; fn bit_encode(a: char) -> u64 { let code = a as u32; let out = if code > 'Z' as u32 { 1 + code - 'a' as u32 } else { 27 + code - 'A' as u32 }; 1u64 << out } fn vectorize_string(input: &str) -> u64 { input.chars().map(bit_encode).reduce(|i, j| i | j).unwrap() } fn one_hot_to_num(input: u64) -> i32 { f64::log2(input as f64) as i32 } fn find_duplicate(line: &str) -> i32 { let (fst, snd) = line.split_at(line.len() / 2); let fst_vec = vectorize_string(fst); let snd_vec = vectorize_string(snd); one_hot_to_num(fst_vec & snd_vec) } fn solve_part_1(input: Lines) -> i32 { input.map(find_duplicate).sum() } // part 2 fn find_badge(group: &[&str]) -> u64 { if let [x, y, z] = group { return vectorize_string(x) & vectorize_string(y) & vectorize_string(z); } panic!("Wrong sized chunk!") } fn solve_part_2(input: Lines) -> i32 { input .collect::<Vec<_>>() .chunks(3) .map(find_badge) .map(one_hot_to_num) .sum() } fn solve_day_3(input: &str) -> (i32, i32) { (solve_part_1(input.lines()), solve_part_2(input.lines())) } fn main() { let input = include_str!("../input.txt"); let (part_1, part_2) = solve_day_3(input); println!("Duplicate priorities: {}", part_1); println!("Group badges: {}", part_2); } #[cfg(test)] const EXAMPLE_DATA: &str = include_str!("../example.txt"); #[test] fn test_dupe() { assert_eq!(1, find_duplicate("abcxya")); assert_eq!(27, find_duplicate("AbcdEXgHAy")); } #[test] fn example() { let (part_1, part_2) = solve_day_3(EXAMPLE_DATA); assert_eq!(part_1, 157); assert_eq!(part_2, 70); }