Made everything a bit more rustic
This commit is contained in:
parent
7f5b6e03f9
commit
a5f19ecae1
22 changed files with 376 additions and 274 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use super::template::{DayTrait, ResultType};
|
||||
use crate::common::pos::Pos;
|
||||
use crate::common::{file::split_lines, pos::Pos};
|
||||
use itertools::Itertools;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
|
|
@ -15,14 +15,22 @@ impl DayTrait for Day {
|
|||
DAY_NUMBER
|
||||
}
|
||||
|
||||
fn part1(&self, lines: &[String]) -> anyhow::Result<ResultType> {
|
||||
fn part1(&self, lines: &str) -> anyhow::Result<ResultType> {
|
||||
let lines = split_lines(lines).collect_vec();
|
||||
if lines.len() < 2 {
|
||||
Err(SensorError::ToFewLines)?;
|
||||
}
|
||||
let row = lines[0].parse()?;
|
||||
let (sensors, beacons) = Day::parse_all(&lines[1..])?;
|
||||
let result = Day::count_coverage_at(&sensors, &beacons, row);
|
||||
Ok(ResultType::Integer(result))
|
||||
}
|
||||
|
||||
fn part2(&self, lines: &[String]) -> anyhow::Result<ResultType> {
|
||||
fn part2(&self, lines: &str) -> anyhow::Result<ResultType> {
|
||||
let lines = split_lines(lines).collect_vec();
|
||||
if lines.len() < 2 {
|
||||
Err(SensorError::ToFewLines)?;
|
||||
}
|
||||
let (sensors, _) = Day::parse_all(&lines[1..])?;
|
||||
let mid_lines = Day::dividing_lines(&sensors);
|
||||
let points = mid_lines
|
||||
|
|
@ -43,7 +51,7 @@ impl DayTrait for Day {
|
|||
}
|
||||
|
||||
impl Day {
|
||||
fn parse_all(lines: &[String]) -> Result<(HashSet<Sensor>, HashSet<Pos<i64>>), SensorError> {
|
||||
fn parse_all(lines: &[&str]) -> Result<(HashSet<Sensor>, HashSet<Pos<i64>>), SensorError> {
|
||||
let mut sensors = HashSet::new();
|
||||
let mut beacons = HashSet::new();
|
||||
for line in lines {
|
||||
|
|
@ -91,6 +99,9 @@ enum SensorError {
|
|||
|
||||
#[error("Did not find exactly one point for the sensor: {0}")]
|
||||
NotExactlyOneResult(usize),
|
||||
|
||||
#[error("Too few lines in input.txt")]
|
||||
ToFewLines,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -256,7 +267,7 @@ impl Sensor {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::common::file::read_lines;
|
||||
use crate::common::file::read_string;
|
||||
use anyhow::Result;
|
||||
#[test]
|
||||
fn test_parse() -> Result<()> {
|
||||
|
|
@ -295,7 +306,7 @@ mod test {
|
|||
#[test]
|
||||
fn test_part1() -> Result<()> {
|
||||
let day = Day {};
|
||||
let lines = read_lines(day.get_day_number(), "example01.txt")?;
|
||||
let lines = read_string(day.get_day_number(), "example01.txt")?;
|
||||
let expected = ResultType::Integer(26);
|
||||
let result = day.part1(&lines)?;
|
||||
assert_eq!(result, expected);
|
||||
|
|
@ -306,7 +317,7 @@ mod test {
|
|||
#[test]
|
||||
fn test_part2() -> Result<()> {
|
||||
let day = Day {};
|
||||
let lines = read_lines(day.get_day_number(), "example01.txt")?;
|
||||
let lines = read_string(day.get_day_number(), "example01.txt")?;
|
||||
let expected = ResultType::Integer(56000011);
|
||||
let result = day.part2(&lines)?;
|
||||
assert_eq!(result, expected);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue