2023 - Day 01
This commit is contained in:
parent
1b597ec05f
commit
75e5c02986
1000
2023/input/01/input
Normal file
1000
2023/input/01/input
Normal file
File diff suppressed because it is too large
Load Diff
4
2023/input/01/test-1
Normal file
4
2023/input/01/test-1
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
7
2023/input/01/test-2
Normal file
7
2023/input/01/test-2
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
89
2023/src/bin/day01.rs
Normal file
89
2023/src/bin/day01.rs
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
#![feature(test)]
|
||||||
|
use anyhow::Result;
|
||||||
|
use aoc::Solver;
|
||||||
|
|
||||||
|
// -- Runners --
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
Day::solve()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_test1() -> Result<()> {
|
||||||
|
Day::test(Day::part1, "test-1", 142)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_solution() -> Result<()> {
|
||||||
|
Day::test(Day::part1, "input", 54968)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_test2() -> Result<()> {
|
||||||
|
Day::test(Day::part2, "test-2", 281)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_solution() -> Result<()> {
|
||||||
|
Day::test(Day::part2, "input", 54094)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Benchmarks
|
||||||
|
extern crate test;
|
||||||
|
#[bench]
|
||||||
|
#[ignore]
|
||||||
|
fn part1_bench(b: &mut test::Bencher) {
|
||||||
|
Day::benchmark(Day::part1, b)
|
||||||
|
}
|
||||||
|
#[bench]
|
||||||
|
#[ignore]
|
||||||
|
fn part2_bench(b: &mut test::Bencher) {
|
||||||
|
Day::benchmark(Day::part2, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Solution --
|
||||||
|
pub struct Day;
|
||||||
|
impl aoc::Solver for Day {
|
||||||
|
type Output1 = u32;
|
||||||
|
type Output2 = u32;
|
||||||
|
|
||||||
|
fn day() -> u8 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &str) -> Self::Output1 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let mut nums = line.chars().filter_map(|c| c.to_digit(10));
|
||||||
|
// There is always at least one number in every line
|
||||||
|
let first = nums.next().unwrap();
|
||||||
|
// If there is only one number use the first number as the last number
|
||||||
|
let last = nums.last().unwrap_or(first);
|
||||||
|
|
||||||
|
first * 10 + last
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: &str) -> Self::Output2 {
|
||||||
|
// NOTE: Spelled out numbers can overlap
|
||||||
|
// To make this substitution work correctly we keep the first and last letter of every
|
||||||
|
// spelled out number
|
||||||
|
let input = input.replace("one", "o1e");
|
||||||
|
let input = input.replace("two", "t2e");
|
||||||
|
let input = input.replace("three", "t3e");
|
||||||
|
let input = input.replace("four", "f4r");
|
||||||
|
let input = input.replace("five", "f5e");
|
||||||
|
let input = input.replace("six", "s6x");
|
||||||
|
let input = input.replace("seven", "s7n");
|
||||||
|
let input = input.replace("eight", "e8t");
|
||||||
|
let input = input.replace("nine", "n9e");
|
||||||
|
|
||||||
|
Self::part1(&input)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user