[TASK] Solved Day 1 (but not very pretty)
This commit is contained in:
63
src/day1.rs
63
src/day1.rs
@@ -2,6 +2,19 @@ use crate::day_solver::DaySolver;
|
|||||||
|
|
||||||
use super::util;
|
use super::util;
|
||||||
|
|
||||||
|
const NUMBERS: [[&'static str; 2]; 10] = [
|
||||||
|
["0", "zero"],
|
||||||
|
["1", "one"],
|
||||||
|
["2", "two"],
|
||||||
|
["3", "three"],
|
||||||
|
["4", "four"],
|
||||||
|
["5", "five"],
|
||||||
|
["6", "six"],
|
||||||
|
["7", "seven"],
|
||||||
|
["8", "eight"],
|
||||||
|
["9", "nine"]
|
||||||
|
];
|
||||||
|
|
||||||
pub struct Day1 {
|
pub struct Day1 {
|
||||||
lines: Vec<String>
|
lines: Vec<String>
|
||||||
}
|
}
|
||||||
@@ -9,7 +22,7 @@ pub struct Day1 {
|
|||||||
impl Day1 {
|
impl Day1 {
|
||||||
|
|
||||||
pub fn create() -> Self {
|
pub fn create() -> Self {
|
||||||
let lines = util::read_file("input/day1_example2.txt");
|
let lines = util::read_file("input/day1.txt");
|
||||||
// let lines = util::read_file("input/dayX.txt");
|
// let lines = util::read_file("input/dayX.txt");
|
||||||
|
|
||||||
// Put the input into the day struct
|
// Put the input into the day struct
|
||||||
@@ -20,6 +33,7 @@ impl Day1 {
|
|||||||
|
|
||||||
fn solve(lines: &Vec<String>) -> u32 {
|
fn solve(lines: &Vec<String>) -> u32 {
|
||||||
lines.iter()
|
lines.iter()
|
||||||
|
.filter(|l| l.chars().any(|c| c.is_numeric()))
|
||||||
.map(|l| {
|
.map(|l| {
|
||||||
let nrs: Vec<u8> = l.chars()
|
let nrs: Vec<u8> = l.chars()
|
||||||
.filter(|c| c.is_numeric())
|
.filter(|c| c.is_numeric())
|
||||||
@@ -28,6 +42,38 @@ impl Day1 {
|
|||||||
u32::from(nrs.first().unwrap().to_owned()) * 10 + u32::from(nrs.last().unwrap().to_owned())
|
u32::from(nrs.first().unwrap().to_owned()) * 10 + u32::from(nrs.last().unwrap().to_owned())
|
||||||
}).sum::<u32>()
|
}).sum::<u32>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_first_number(line: &String) -> u32 {
|
||||||
|
let mut min_idx = line.len();
|
||||||
|
let mut num_at_idx: u32 = 0;
|
||||||
|
for (n, nr_strings) in NUMBERS.iter().enumerate() {
|
||||||
|
for nr_string in nr_strings {
|
||||||
|
if let Some(i) = line.find(nr_string) {
|
||||||
|
if i < min_idx {
|
||||||
|
num_at_idx = n as u32;
|
||||||
|
min_idx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
num_at_idx
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_last_number(line: &String) -> u32 {
|
||||||
|
let mut max_idx = 0usize;
|
||||||
|
let mut num_at_idx = 0u32;
|
||||||
|
for (n, nr_strings) in NUMBERS.iter().enumerate() {
|
||||||
|
for nr_string in nr_strings {
|
||||||
|
if let Some(i) = line.rfind(nr_string) {
|
||||||
|
if i >= max_idx {
|
||||||
|
num_at_idx = n as u32;
|
||||||
|
max_idx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
num_at_idx
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DaySolver for Day1 {
|
impl DaySolver for Day1 {
|
||||||
@@ -38,17 +84,8 @@ impl DaySolver for Day1 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn solve_part2(&mut self) -> String {
|
fn solve_part2(&mut self) -> String {
|
||||||
return Self::solve(&self.lines.iter().map(
|
self.lines.iter().map(
|
||||||
|l| l
|
|l| Self::find_first_number(l) * 10 + Self::find_last_number(l)
|
||||||
.replace("one", "1")
|
).sum::<u32>().to_string()
|
||||||
.replace("two", "2")
|
|
||||||
.replace("three", "3")
|
|
||||||
.replace("four", "4")
|
|
||||||
.replace("five", "5")
|
|
||||||
.replace("six", "6")
|
|
||||||
.replace("seven", "7")
|
|
||||||
.replace("eight", "8")
|
|
||||||
.replace("nine", "9")
|
|
||||||
).collect()).to_string()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/main.rs
13
src/main.rs
@@ -87,13 +87,12 @@ fn solve(day: u8, silent: bool) -> AocBenchResult {
|
|||||||
match solver {
|
match solver {
|
||||||
Some(mut s) => {
|
Some(mut s) => {
|
||||||
|
|
||||||
// let(part1, pt1_time) = bench(|| s.solve_part1());
|
let(part1, pt1_time) = bench(|| s.solve_part1());
|
||||||
// part1_time = pt1_time;
|
part1_time = pt1_time;
|
||||||
//
|
|
||||||
// if !silent {
|
if !silent {
|
||||||
// println!("Day {} Part 1: {}", day, part1);
|
println!("Day {} Part 1: {}", day, part1);
|
||||||
// }
|
}
|
||||||
part1_time = 0;
|
|
||||||
let (part2, pt2_time) = bench(|| s.solve_part2());
|
let (part2, pt2_time) = bench(|| s.solve_part2());
|
||||||
part2_time = pt2_time;
|
part2_time = pt2_time;
|
||||||
if !silent {
|
if !silent {
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ impl<T: Clone + Sized> Grid<T> {
|
|||||||
&self.data[idx]
|
&self.data[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn set(&mut self, x: &usize, y: &usize, v: T) {
|
pub fn set(&mut self, x: &usize, y: &usize, v: T) {
|
||||||
let idx = self.idx(x, y);
|
let idx = self.idx(x, y);
|
||||||
self.data[idx] = v;
|
self.data[idx] = v;
|
||||||
|
|||||||
Reference in New Issue
Block a user