Solved day 2
This commit is contained in:
1000
input/day02.txt
Normal file
1000
input/day02.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
input/day02_example.txt
Normal file
6
input/day02_example.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
7 6 4 2 1
|
||||||
|
1 2 7 8 9
|
||||||
|
9 7 6 2 1
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
|
1 3 6 7 9
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
use std::arch::x86_64::_addcarryx_u32;
|
|
||||||
use itertools::Itertools;
|
|
||||||
use crate::day_solver::DaySolver;
|
use crate::day_solver::DaySolver;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::util::read_file;
|
use crate::util::read_file;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
pub struct Day1 {
|
pub struct Day1 {
|
||||||
list1: Vec<u32>,
|
list1: Vec<u32>,
|
||||||
@@ -36,7 +35,7 @@ impl DaySolver for Day1 {
|
|||||||
fn solve_part1(&mut self) -> String {
|
fn solve_part1(&mut self) -> String {
|
||||||
let list1_sorted = Day1::sort_list(&self.list1);
|
let list1_sorted = Day1::sort_list(&self.list1);
|
||||||
let list2_sorted = Day1::sort_list(&self.list2);
|
let list2_sorted = Day1::sort_list(&self.list2);
|
||||||
(0..list1_sorted.len()).map(|i| u32::abs_diff(list1_sorted[i], list2_sorted[i]))
|
list1_sorted.iter().zip(list2_sorted).map(|(n1, n2)| u32::abs_diff(n1.to_owned(), n2.to_owned()))
|
||||||
.sum::<u32>().to_string()
|
.sum::<u32>().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,12 +47,12 @@ impl DaySolver for Day1 {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() {
|
||||||
let mut day = Day1::create(read_file("input/day1_example.txt"));
|
let mut day = Day1::create(read_file("input/day01_example.txt"));
|
||||||
assert_eq!("11", day.solve_part1());
|
assert_eq!("11", day.solve_part1());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2() {
|
||||||
let mut day = Day1::create(read_file("input/day1_example.txt"));
|
let mut day = Day1::create(read_file("input/day01_example.txt"));
|
||||||
assert_eq!("31", day.solve_part2());
|
assert_eq!("31", day.solve_part2());
|
||||||
}
|
}
|
||||||
|
|||||||
72
src/day2.rs
Normal file
72
src/day2.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
use crate::day_solver::DaySolver;
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::util::read_file;
|
||||||
|
|
||||||
|
pub struct Day2 {
|
||||||
|
reports: Vec<Vec<u32>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Day2 {
|
||||||
|
|
||||||
|
pub fn create(input: String) -> Self {
|
||||||
|
// Put the input into the day struct
|
||||||
|
let reports = input.lines()
|
||||||
|
.map(|l| l.split_whitespace().map(|n| n.parse::<u32>().unwrap()).collect::<Vec<u32>>())
|
||||||
|
.collect::<Vec<Vec<u32>>>();
|
||||||
|
|
||||||
|
Day2 {
|
||||||
|
reports
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid(report: &[u32]) -> bool {
|
||||||
|
let is_increasing = report[1] > report[0];
|
||||||
|
for i in 0..report.len() - 1 {
|
||||||
|
let n1 = report[i];
|
||||||
|
let n2 = report[i+1];
|
||||||
|
if (is_increasing && n2 <= n1) ||
|
||||||
|
(!is_increasing && n2 >= n1) ||
|
||||||
|
u32::abs_diff(n1, n2) > 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mutation_is_valid(report: &[u32]) -> bool {
|
||||||
|
for i in 0..report.len() {
|
||||||
|
if Self::is_valid(&report.iter().enumerate().filter(|(j, _)| j != &i)
|
||||||
|
.map(|(_, n)| n.to_owned())
|
||||||
|
.collect::<Vec<u32>>()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DaySolver for Day2 {
|
||||||
|
|
||||||
|
|
||||||
|
fn solve_part1(&mut self) -> String {
|
||||||
|
self.reports.iter().filter(|r| Self::is_valid(r)).count().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_part2(&mut self) -> String {
|
||||||
|
self.reports.iter().filter(|r|
|
||||||
|
Self::is_valid(r) || Self::mutation_is_valid(r)
|
||||||
|
).count().to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part1() {
|
||||||
|
let mut day = Day2::create(read_file("input/day02_example.txt"));
|
||||||
|
assert_eq!("2", day.solve_part1());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part2() {
|
||||||
|
let mut day = Day2::create(read_file("input/day02_example.txt"));
|
||||||
|
assert_eq!("4", day.solve_part2());
|
||||||
|
}
|
||||||
@@ -2,12 +2,14 @@ extern crate core;
|
|||||||
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crate::day1::Day1;
|
use crate::day1::Day1;
|
||||||
|
use crate::day2::Day2;
|
||||||
use crate::day_solver::DaySolver;
|
use crate::day_solver::DaySolver;
|
||||||
use crate::util::read_file;
|
use crate::util::read_file;
|
||||||
|
|
||||||
mod util;
|
mod util;
|
||||||
mod day_solver;
|
mod day_solver;
|
||||||
mod day1;
|
mod day1;
|
||||||
|
mod day2;
|
||||||
|
|
||||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||||
|
|
||||||
@@ -80,7 +82,7 @@ fn build_day_solver(day: u8, input: String) -> Option<Box<dyn DaySolver>> {
|
|||||||
|
|
||||||
match day {
|
match day {
|
||||||
1 => Some(Box::new(Day1::create(input))),
|
1 => Some(Box::new(Day1::create(input))),
|
||||||
// 2 => Some(Box::new(Day2::create(input))),
|
2 => Some(Box::new(Day2::create(input))),
|
||||||
// 3 => Some(Box::new(Day3::create(input))),
|
// 3 => Some(Box::new(Day3::create(input))),
|
||||||
// 4 => Some(Box::new(Day4::create(input))),
|
// 4 => Some(Box::new(Day4::create(input))),
|
||||||
// 5 => Some(Box::new(Day5::create(input))),
|
// 5 => Some(Box::new(Day5::create(input))),
|
||||||
|
|||||||
Reference in New Issue
Block a user