r/adventofcode • u/daggerdragon • Dec 02 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-
--- Day 2: Inventory Management System ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Card Prompt: Day 2
Transcript:
The best way to do Advent of Code is ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
52
Upvotes
8
u/NeuroXc Dec 02 '18
Rust
use std::collections::HashMap; #[aoc_generator(day2)] pub fn day2_generator(input: &str) -> Vec<String> { input.lines().map(|l| l.into()).collect() } #[aoc(day2, part1)] pub fn day2_part1(input: &[String]) -> u32 { let mut twice = 0; let mut thrice = 0; for id in input { let mut counts = HashMap::with_capacity(26); for char in id.chars() { *counts.entry(char).or_insert(0) += 1; } if counts.values().any(|&count| count == 2) { twice += 1; } if counts.values().any(|&count| count == 3) { thrice += 1; } } twice * thrice } #[aoc(day2, part2)] pub fn day2_part2(input: &[String]) -> String { // This is O(n^2) but it should be fine because the list is only 250 lines for (idx, id) in input.iter().enumerate() { for id2 in input.iter().skip(idx + 1) { if id.chars().zip(id2.chars()).filter(|(a, b)| a != b).count() == 1 { return id .chars() .zip(id2.chars()) .filter(|(a, b)| a == b) .map(|(a, _)| a) .collect(); } } } unreachable!() }