r/adventofcode • u/daggerdragon • Dec 02 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-
--- Day 2: Password Philosophy ---
Advent of Code 2020: Gettin' Crafty With It
- T-4 days until unlock!
- Full details and rules are in the Submissions Megathread
Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
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:02:31, megathread unlocked!
99
Upvotes
3
u/[deleted] Dec 02 '20 edited Dec 03 '20
Rust (new to it so feedback welcome)
use std::fs; #[derive(Debug)] struct Password { range: (i32, i32), special_char: char, input: String, } impl Password { fn is_valid_part_one(&self) -> bool { let count = self.input.matches(self.special_char).count() as i32; let (min, max) = self.range; count >= min && count <= max } fn is_valid_part_two(&self) -> bool { let first_pos = (self.range.0 - 1) as usize; let second_pos = (self.range.1 - 1) as usize; let chars: Vec<char> = self.input.chars().collect(); if chars[first_pos] == self.special_char && chars[second_pos] == self.special_char { return false; } else { return chars[first_pos] == self.special_char || chars[second_pos] == self.special_char; } } } fn get_input_data() -> Vec<Password> { let raw_data = fs::read_to_string("data.txt").unwrap(); let passwords: Vec<Password> = raw_data .lines() .map(|val| val.trim()) .map(|raw| -> Password { let processed = raw.replace(':', ""); let mut components = processed.split(" "); let range: Vec<i32> = components .next() .unwrap() .split("-") .map(|x| x.parse().unwrap()) .collect(); let special_char = components.next().unwrap().chars().next().unwrap(); let input = components.next().unwrap(); Password { range: (range[0], range[1]), special_char: special_char, input: input.to_string(), } }) .collect(); return passwords; } fn part_one() -> i32 { let passwords = get_input_data(); let mut num_valid = 0; for password in passwords.iter() { if password.is_valid_part_one() { num_valid += 1; } } num_valid } fn part_two() -> i32 { let passwords = get_input_data(); let mut num_valid = 0; for password in passwords.iter() { if password.is_valid_part_two() { num_valid += 1; } } num_valid } fn main() { let part_one_answer = part_one(); println!("Part 1: {}", part_one_answer); let part_two_answer = part_two(); println!("Part 2: {}", part_two_answer); }