Skip to content

Commit

Permalink
AoC 2023 Day 1 - rust
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 2, 2023
1 parent f5ac6fa commit 92bbec0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| bash | | | | | | | | | | | | | | | | | | | | | | | | | |
| c++ | | | | | | | | | | | | | | | | | | | | | | | | | |
| julia | | | | | | | | | | | | | | | | | | | | | | | | | |
| rust | | [](src/main/rust/AoC2023_02/src/main.rs) | | | | | | | | | | | | | | | | | | | | | | | |
| rust | [](src/main/rust/AoC2023_01/src/main.rs) | [](src/main/rust/AoC2023_02/src/main.rs) | | | | | | | | | | | | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2023@ -->

## 2022
Expand Down
7 changes: 7 additions & 0 deletions src/main/rust/AoC2023_01/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "AoC2023_01"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../aoc" }
102 changes: 102 additions & 0 deletions src/main/rust/AoC2023_01/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#![allow(non_snake_case)]

use aoc::Puzzle;

const NUMS: [&str; 9] = [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];

struct AoC2023_01;

impl AoC2023_01 {
fn solve(&self, input: &[String], f: impl Fn(&str) -> Vec<u32>) -> u32 {
input
.iter()
.map(|line| f(line))
.map(|digits| digits.first().unwrap() * 10 + digits.last().unwrap())
.sum()
}
}

impl aoc::Puzzle for AoC2023_01 {
type Input = Vec<String>;
type Output1 = u32;
type Output2 = u32;

aoc::puzzle_year_day!(2023, 1);

fn parse_input(&self, lines: Vec<String>) -> Vec<String> {
lines
}

fn part_1(&self, input: &Vec<String>) -> u32 {
fn get_digits(line: &str) -> Vec<u32> {
line.chars()
.filter(|c| c.is_ascii_digit())
.map(|c| c.to_digit(10).unwrap())
.collect::<Vec<_>>()
}
self.solve(input, get_digits)
}

fn part_2(&self, input: &Vec<String>) -> u32 {
fn find_digit(s: &str) -> Option<u32> {
let c = s.chars().next().unwrap();
if c.is_ascii_digit() {
return Some(c.to_digit(10).unwrap());
} else {
for (j, num) in NUMS.iter().enumerate() {
if s.starts_with(num) {
return Some(j as u32 + 1);
}
}
}
None
}

fn get_digits(line: &str) -> Vec<u32> {
(0..line.len())
.map(|i| &line[i..])
.filter_map(find_digit)
.collect::<Vec<_>>()
}
self.solve(input, get_digits)
}

fn samples(&self) {
aoc::puzzle_samples! {
self, part_1, TEST1, 142,
self, part_2, TEST2, 281
};
}
}

fn main() {
AoC2023_01 {}.run(std::env::args());
}

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

#[test]
pub fn samples() {
AoC2023_01 {}.samples();
}
}

const TEST1: &str = "\
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
";
const TEST2: &str = "\
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
";
7 changes: 7 additions & 0 deletions src/main/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92bbec0

Please sign in to comment.