Day 4 Part 1 complete
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::Solution;
|
||||
|
||||
pub struct Day04 {}
|
||||
@ -5,9 +7,42 @@ pub struct Day04 {}
|
||||
impl Solution for Day04 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
// Remove Card XXX: at start
|
||||
let cards: Vec<&str> = input
|
||||
.iter()
|
||||
.map(|c| c.split(":").into_iter().last().unwrap())
|
||||
.collect();
|
||||
let mut ans = 0;
|
||||
|
||||
for cards in cards {
|
||||
let mut winning_numbers: HashMap<u32, bool> = HashMap::new();
|
||||
let mut sep_found = false;
|
||||
let mut card_nums = vec![];
|
||||
|
||||
for num in cards.split_whitespace() {
|
||||
// Are we after the |
|
||||
if num == "|" {
|
||||
sep_found = true;
|
||||
continue;
|
||||
}
|
||||
let num = num.parse::<u32>()?;
|
||||
|
||||
if sep_found {
|
||||
if winning_numbers.contains_key(&num) {
|
||||
card_nums.push(num);
|
||||
}
|
||||
} else {
|
||||
winning_numbers.insert(num, true);
|
||||
}
|
||||
}
|
||||
if !card_nums.is_empty() {
|
||||
ans += 2u32.pow((card_nums.len() - 1) as u32);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Box::new(ans))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
@ -22,7 +57,31 @@ impl Solution for Day04 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Day04 {}
|
||||
impl Day04 {
|
||||
fn get_count_winning_numbers(card: &str) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
let mut winning_numbers: HashMap<u32, bool> = HashMap::new();
|
||||
let mut sep_found = false;
|
||||
let mut card_nums = vec![];
|
||||
|
||||
for num in card.split_whitespace() {
|
||||
// Are we after the |
|
||||
if num == "|" {
|
||||
sep_found = true;
|
||||
continue;
|
||||
}
|
||||
let num = num.parse::<u32>()?;
|
||||
|
||||
if sep_found {
|
||||
if winning_numbers.contains_key(&num) {
|
||||
card_nums.push(num);
|
||||
}
|
||||
} else {
|
||||
winning_numbers.insert(num, true);
|
||||
}
|
||||
}
|
||||
Ok(card_nums.len())
|
||||
}
|
||||
}
|
||||
|
||||
/// Test from puzzle input
|
||||
#[cfg(test)]
|
||||
@ -44,7 +103,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "13");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
66
src/solutions/day05.rs
Normal file
66
src/solutions/day05.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use super::Solution;
|
||||
|
||||
pub struct Day05 {}
|
||||
|
||||
impl Solution for Day05 {
|
||||
fn part1(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
}
|
||||
|
||||
fn get_day(&self) -> u8 {
|
||||
5
|
||||
}
|
||||
}
|
||||
|
||||
impl Day05 {}
|
||||
|
||||
/// Test from puzzle input
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::*;
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let challenge = day05::Day05 {};
|
||||
|
||||
//Complete the Challenge
|
||||
let answer = challenge
|
||||
.part1(
|
||||
utils::get_input(challenge.get_day(), utils::InputType::Test1)
|
||||
.unwrap()
|
||||
.as_mut(),
|
||||
)
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2() {
|
||||
let challenge = day05::Day05 {};
|
||||
|
||||
//Complete the Challenge
|
||||
let answer = challenge
|
||||
.part2(
|
||||
utils::get_input(challenge.get_day(), utils::InputType::Test2)
|
||||
.unwrap()
|
||||
.as_mut(),
|
||||
)
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ pub mod day01;
|
||||
pub mod day02;
|
||||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod day05;
|
||||
|
||||
use crate::utils::{self, get_input};
|
||||
use std::{error::Error, fmt::Display, time::SystemTime};
|
||||
|
Reference in New Issue
Block a user