Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(2023: 1): add implementation for first part #89

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions aoc-2023/src/day_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use common::{Day, Puzzle, Solver};

mod part_1;
mod part_2;

pub fn puzzle() -> Puzzle {
Puzzle::new(
Day::from(1),
String::from("Trebuchet?!"),
Solver::new(part_1::solve),
Solver::new(part_2::solve),
)
}
41 changes: 41 additions & 0 deletions aoc-2023/src/day_1/part_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::io::BufRead;

use common::Input;

pub fn solve(input: &Input) -> i64 {
let lines = input.read().lines();

let mut sum = 0;

for line in lines {
let line = line.unwrap();

let first_digit = line.chars().find(|c| char::is_numeric(*c)).unwrap();
let last_digit = line.chars().rev().find(|c| char::is_numeric(*c)).unwrap();

let number = first_digit.to_digit(10).unwrap() * 10 + last_digit.to_digit(10).unwrap();

sum += number;
}

sum as i64
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn one_digit() {
let zero = String::from("zad0azdazd");

assert_eq!(solve(&Input::from(zero)), 0);
}

#[test]
fn two_digit() {
let digits = String::from("zad5azd9azd");

assert_eq!(solve(&Input::from(digits)), 59);
}
}
141 changes: 141 additions & 0 deletions aoc-2023/src/day_1/part_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use std::{collections::HashMap, io::BufRead};

use common::Input;

pub fn solve(input: &Input) -> i64 {
let lines = input.read().lines();

let mut sum = 0;

for line in lines {
let line = line.unwrap();

let first_digit = find_first_digit(&line);

let line_reversed: String = line.chars().rev().collect();
let last_digit = find_last_digit(&line_reversed);

let number = first_digit * 10 + last_digit;

sum += number;
}

sum
}

/// Find the first digit in the `line`.
fn find_first_digit(line: &str) -> i64 {
let str_to_number: HashMap<&str, i64> = [
("0", 0),
("1", 1),
("2", 2),
("3", 3),
("4", 4),
("5", 5),
("6", 6),
("7", 7),
("8", 8),
("9", 9),
("one", 1),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
]
.into();

for i in 0..line.len() {
let slice = &line[i..];

for key in str_to_number.keys() {
if slice.starts_with(key) {
return *str_to_number.get(key).unwrap();
}
}
}

unreachable!()
}

/// Find the last digit in the `line`.
fn find_last_digit(line: &str) -> i64 {
let str_to_number: HashMap<&str, i64> = [
("0", 0),
("1", 1),
("2", 2),
("3", 3),
("4", 4),
("5", 5),
("6", 6),
("7", 7),
("8", 8),
("9", 9),
("eno", 1),
("owt", 2),
("eerht", 3),
("ruof", 4),
("evif", 5),
("xis", 6),
("neves", 7),
("thgie", 8),
("enin", 9),
]
.into();

for i in 0..line.len() {
let slice = &line[i..];

for key in str_to_number.keys() {
if slice.starts_with(key) {
return *str_to_number.get(key).unwrap();
}
}
}

unreachable!()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn one_digit() {
let zero = String::from("zad0azdazd");

assert_eq!(solve(&Input::from(zero)), 0);
}

#[test]
fn two_digit() {
let digits = String::from("one5azd9eight");

assert_eq!(solve(&Input::from(digits)), 18);
}

#[test]
fn simple() {
let digits = String::from(
"two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen",
);

assert_eq!(solve(&Input::from(digits)), 281);
}

#[test]
fn eat_word() {
let digits = String::from("eightwo");

assert_eq!(solve(&Input::from(digits)), 82);
}
}
4 changes: 3 additions & 1 deletion aoc-2023/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Event for the Advent of Code 2023.

mod day_1;

use common::{Event, Year};

pub fn event() -> Event {
Event::new(Year::from(2023), [].into_iter())
Event::new(Year::from(2023), [day_1::puzzle()].into_iter())
}
Loading