Compare commits

...

1 Commits

Author SHA1 Message Date
f498013915 cleanup 2020-12-02 10:56:56 -08:00

View File

@ -1,73 +1,82 @@
use aoc_runner_derive::{aoc_lib, aoc}; use aoc_runner_derive::{aoc, aoc_generator, aoc_lib};
#[aoc(day1, part1)] #[aoc_generator(day1)]
pub fn solve_d1_p1(input: &str) -> u64 { pub fn d1_input(input: &str) -> Vec<u64> {
let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect(); let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect();
entries.sort(); entries.sort();
for entry in &entries { entries
}
#[aoc(day1, part1)]
pub fn solve_d1_p1(input: &[u64]) -> u64 {
for entry in input {
let needed = 2020 - entry; let needed = 2020 - entry;
if let Ok(idx) = entries.binary_search(&needed) { if let Ok(idx) = input.binary_search(&needed) {
return entry * entries[idx]; return entry * input[idx];
} }
} }
panic!("not found"); panic!("not found");
} }
#[aoc(day1, part2)] #[aoc(day1, part2)]
pub fn solve_d1_p2(input: &str) -> Option<u64> { pub fn solve_d1_p2(input: &[u64]) -> Option<u64> {
let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect(); for i in 0..input.len() - 2 {
entries.sort(); let entry1 = input[i];
for i in 0 .. entries.len()-2 { for j in i + 1..input.len() - 1 {
let entry1 = entries[i]; let entry2 = input[j];
for j in i+1 .. entries.len()-1 {
let entry2 = entries[j];
if entry1 + entry2 > 2020 { if entry1 + entry2 > 2020 {
break; break;
} }
for k in j+1 .. entries.len() { for k in j + 1..input.len() {
let entry3 = entries[k]; let entry3 = input[k];
if entry1 + entry2 + entry3 == 2020 { if entry1 + entry2 + entry3 == 2020 {
return Some(entry1 * entry2 * entry3) return Some(entry1 * entry2 * entry3);
} }
} }
} }
} }
return None return None;
}
struct PasswdEntry<'a> {
lower_bound: usize,
upper_bound: usize,
policy_char: u8,
passwd: &'a [u8],
}
impl<'a> PasswdEntry<'a> {
fn new(mut input: &'a str) -> Option<Self> {
let lb_idx = input.find('-')?;
let lower_bound: usize = (&input[..lb_idx]).parse().ok()?;
input = &input[lb_idx + 1..];
let ub_idx = input.find(' ')?;
let upper_bound: usize = (&input[..ub_idx]).parse().ok()?;
input = &input[ub_idx + 1..];
let policy_char = input.as_bytes()[0];
let passwd = &input[3..].as_bytes();
Some(PasswdEntry { lower_bound, upper_bound, policy_char, passwd })
}
} }
#[aoc(day2, part1)] #[aoc(day2, part1)]
pub fn solve_d2_p1(input: &str) -> usize { pub fn solve_d2_p1(input: &str) -> usize {
fn line_is_valid(mut line: &str) -> bool { fn line_is_valid(line: &str) -> bool {
let lb_idx = line.find('-').unwrap(); let entry = PasswdEntry::new(line).unwrap();
let lb: usize = (&line[..lb_idx]).parse().unwrap(); let count = entry.passwd.iter().filter(|&&b| b == entry.policy_char).count();
line = &line[lb_idx+1..]; (count >= entry.lower_bound) && (count <= entry.upper_bound)
let ub_idx = line.find(' ').unwrap();
let ub: usize = (&line[..ub_idx]).parse().unwrap();
line = &line[ub_idx+1..];
let letter = line.as_bytes()[0];
let passwd = &line[3..];
let count = passwd.bytes().filter(|&b| b == letter).count();
count >= lb && count <= ub
} }
input.split('\n').filter(|x| line_is_valid(x)).count() input.split('\n').filter(|x| line_is_valid(x)).count()
} }
#[aoc(day2, part2)] #[aoc(day2, part2)]
pub fn solve_d2_p2(input: &str) -> usize { pub fn solve_d2_p2(input: &str) -> usize {
fn line_is_valid(mut line: &str) -> bool { fn line_is_valid(line: &str) -> bool {
let lb_idx = line.find('-').unwrap(); let entry = PasswdEntry::new(line).unwrap();
let lb: usize = (&line[..lb_idx]).parse().unwrap(); (entry.passwd[entry.lower_bound - 1] == entry.policy_char)
line = &line[lb_idx+1..]; ^ (entry.passwd[entry.upper_bound - 1] == entry.policy_char)
let ub_idx = line.find(' ').unwrap();
let ub: usize = (&line[..ub_idx]).parse().unwrap();
line = &line[ub_idx+1..];
let letter = line.as_bytes()[0];
let passwd = &line[3..].as_bytes();
(passwd[lb-1] == letter) ^ (passwd[ub-1] == letter)
} }
input.split('\n').filter(|x| line_is_valid(x)).count() input.split('\n').filter(|x| line_is_valid(x)).count()
} }