feat: 2023 day one

This commit is contained in:
Max Hohlfeld 2023-12-02 19:38:30 +01:00
parent e6590f44b9
commit ce309b14be
4 changed files with 1064 additions and 0 deletions

1000
src/aoc2023/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

59
src/aoc2023/day01/mod.rs Normal file
View File

@ -0,0 +1,59 @@
use std::cmp::Ordering;
pub fn task_one(input: &str) -> String {
let mut sum = 0;
for line in input.lines() {
let numbers_in_line: Vec<char> = line.chars().filter(|c| c.is_numeric()).collect();
let first = numbers_in_line.first().unwrap().to_digit(10).unwrap() * 10;
let last = numbers_in_line.last().unwrap().to_digit(10).unwrap();
sum = sum + first + last;
}
sum.to_string()
}
pub fn task_two(input: &str) -> String {
let mut sum = 0;
for line in input.lines() {
let first = find_first_number(line, &LITERAL_NUMBERS) * 10;
let last = find_first_number(&line.chars().rev().collect::<String>(), &LITERAL_NUMBERS_REVERSE);
sum = sum + first + last;
}
sum.to_string()
}
const LITERAL_NUMBERS: [(&str, u32); 9] = [("one", 1), ("two", 2), ("three", 3), ("four", 4), ("five", 5), ("six", 6), ("seven", 7), ("eight", 8), ("nine", 9)];
const LITERAL_NUMBERS_REVERSE: [(&str, u32); 9] = [("eno", 1), ("owt", 2), ("eerht", 3), ("ruof", 4), ("evif", 5), ("xis", 6), ("neves", 7), ("thgie", 8), ("enin", 9)];
fn find_first_number(line: &str, numbers: &[(&str, u32); 9]) -> u32 {
let chars: Vec<char> = line.chars().collect();
let index_of_first_digit = line.find(|c: char| c.is_digit(10)).unwrap();
let mut places_of_literal_digits: Vec<(Option<usize>, u32)> = numbers
.iter()
.map(|sec| (line.find(sec.0), sec.1))
.filter(|x| x.0.is_some())
.collect();
places_of_literal_digits.sort_by(|a, b| {
if a.0.unwrap() > b.0.unwrap() {
return Ordering::Greater
} else if a.0.unwrap() < b.0.unwrap() {
return Ordering::Less
} else {
return Ordering::Equal
}
});
if !places_of_literal_digits.is_empty() && places_of_literal_digits.first().unwrap().0.unwrap() < index_of_first_digit {
return places_of_literal_digits.first().unwrap().1;
} else {
return chars.get(index_of_first_digit).unwrap().to_digit(10).unwrap();
}
}

1
src/aoc2023/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod day01;

View File

@ -1,5 +1,6 @@
mod aoc2021; mod aoc2021;
mod aoc2022; mod aoc2022;
mod aoc2023;
use std::fs; use std::fs;
@ -43,5 +44,8 @@ fn main() {
puzzles.push(Puzzle { day: 9, year: 2022, task_one: aoc2022::day09::task_one, task_two: aoc2022::day09::task_two }); puzzles.push(Puzzle { day: 9, year: 2022, task_one: aoc2022::day09::task_one, task_two: aoc2022::day09::task_two });
puzzles.push(Puzzle { day: 10, year: 2022, task_one: aoc2022::day10::task_one, task_two: aoc2022::day10::task_two }); puzzles.push(Puzzle { day: 10, year: 2022, task_one: aoc2022::day10::task_one, task_two: aoc2022::day10::task_two });
// AOC 2023
puzzles.push(Puzzle { day: 1, year: 2023, task_one: aoc2023::day01::task_one, task_two: aoc2023::day01::task_two });
puzzles.iter().for_each(|puzzle| puzzle.solve_and_print()); puzzles.iter().for_each(|puzzle| puzzle.solve_and_print());
} }