Day 1 initial solution
This commit is contained in:
commit
b9dfa6ab29
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
/Cargo.lock
|
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc2023"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
aoc-runner = "0.3.0"
|
||||||
|
aoc-runner-derive = "0.3.0"
|
||||||
|
nom = "7.1.3"
|
1000
input/2023/day1.txt
Normal file
1000
input/2023/day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
55
src/day01.rs
Normal file
55
src/day01.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
use nom::{IResult, bytes::complete::tag, combinator::{map, opt}, branch::alt, character::complete::satisfy, Finish};
|
||||||
|
|
||||||
|
#[aoc_generator(day1)]
|
||||||
|
fn input_generator(input: &str) -> Vec<String> {
|
||||||
|
input.split('\n').map(|line| line.to_string()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day1, part1)]
|
||||||
|
fn part1(input: &[String]) -> u32 {
|
||||||
|
input.iter().map(|line| {
|
||||||
|
line.chars()
|
||||||
|
.filter(|c| c.is_ascii_digit())
|
||||||
|
.map(|c| (c as u32) - (b'0' as u32))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}).map(|line| (line[0] * 10) + (line.last().unwrap())).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_text_digit(input: &str) -> IResult<&str, u32> {
|
||||||
|
let one = map(tag("one"), |_| 1);
|
||||||
|
let two = map(tag("two"), |_| 2);
|
||||||
|
let three = map(tag("three"), |_| 3);
|
||||||
|
let four = map(tag("four"), |_| 4);
|
||||||
|
let five = map(tag("five"), |_| 5);
|
||||||
|
let six = map(tag("six"), |_| 6);
|
||||||
|
let seven = map(tag("seven"), |_| 7);
|
||||||
|
let eight = map(tag("eight"), |_| 8);
|
||||||
|
let nine = map(tag("nine"), |_| 9);
|
||||||
|
alt((one, two, three, four, five, six, seven, eight, nine))(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_ascii_digit(input: &str) -> IResult<&str, u32> {
|
||||||
|
map(satisfy(|c| c.is_ascii_digit()), |c| (c as u32) - (b'0' as u32))(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_digit(input: &str) -> IResult<&str, u32> {
|
||||||
|
alt((parse_text_digit, parse_ascii_digit))(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_line(line: &str) -> Vec<u32> {
|
||||||
|
let mut digits = Vec::new();
|
||||||
|
let mut line = line;
|
||||||
|
while line.len() > 0 {
|
||||||
|
if let (_, Some(digit)) = opt(parse_digit)(line).finish().unwrap() {
|
||||||
|
digits.push(digit);
|
||||||
|
}
|
||||||
|
line = &line[1..];
|
||||||
|
}
|
||||||
|
digits
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day1, part2)]
|
||||||
|
fn part2(input: &[String]) -> u32 {
|
||||||
|
input.iter().map(|line| parse_line(line)).map(|line| (line[0] * 10) + (line.last().unwrap())).sum()
|
||||||
|
}
|
5
src/lib.rs
Normal file
5
src/lib.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
use aoc_runner_derive::aoc_lib;
|
||||||
|
|
||||||
|
mod day01;
|
||||||
|
|
||||||
|
aoc_lib! { year = 2023 }
|
Loading…
Reference in New Issue
Block a user