day 1 initial
This commit is contained in:
parent
cd56e3eb00
commit
657aac8f57
123
Cargo.lock
generated
123
Cargo.lock
generated
|
@ -2,6 +2,129 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.28.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.32.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||
|
||||
[[package]]
|
||||
name = "taoc2023"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"nom",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.34.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
|
|
@ -4,3 +4,5 @@ version = "0.0.1"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
nom = "7.1.3"
|
||||
tokio = "1.34.0"
|
||||
|
|
12
flake.nix
12
flake.nix
|
@ -46,7 +46,15 @@
|
|||
myRustTarget = pkgs.rust-bin.stable.latest.default;
|
||||
|
||||
craneLib = (crane.mkLib pkgs).overrideToolchain myRustTarget;
|
||||
src = craneLib.cleanCargoSource (craneLib.path ./.);
|
||||
|
||||
textFilter = path: _type: builtins.match ".*txt$" path != null;
|
||||
textOrCargo = path: type:
|
||||
(textFilter path type) || (craneLib.filterCargoSources path type);
|
||||
|
||||
src = lib.cleanSourceWith {
|
||||
src = craneLib.path ./.;
|
||||
filter = textOrCargo;
|
||||
};
|
||||
|
||||
commonArgs = {
|
||||
inherit version pname;
|
||||
|
@ -57,6 +65,7 @@
|
|||
cargo-audit
|
||||
cargo-tarpaulin
|
||||
cargo-nextest
|
||||
rust-analyzer
|
||||
];
|
||||
};
|
||||
|
||||
|
@ -105,6 +114,7 @@
|
|||
|
||||
apps.default = flake-utils.lib.mkApp {
|
||||
drv = my-crate;
|
||||
name = "taoc2023";
|
||||
};
|
||||
|
||||
devShells.default = pkgs.mkShell {
|
||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -1,10 +1,12 @@
|
|||
use taoc2023::problems::*;
|
||||
use taoc2023::utils::timeit;
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), String> {
|
||||
println!("== day 01 ==");
|
||||
timeit(|| {
|
||||
let res = day01::solution();
|
||||
println!("solution: {res}");
|
||||
});
|
||||
timeit(|| -> Result<(), String> {
|
||||
let (a, b) = day01::solution(day01::inputs::INPUT)?;
|
||||
println!("solution: {a}, {b}");
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
2
src/problems/day01/common.rs
Normal file
2
src/problems/day01/common.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub type Int = u32;
|
||||
pub type Line = (Vec<Int>, Vec<Int>);
|
47
src/problems/day01/description.txt
Normal file
47
src/problems/day01/description.txt
Normal file
|
@ -0,0 +1,47 @@
|
|||
--- Day 1: Trebuchet?! ---
|
||||
|
||||
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
|
||||
|
||||
You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.
|
||||
|
||||
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
|
||||
|
||||
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
|
||||
|
||||
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
|
||||
|
||||
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
|
||||
|
||||
For example:
|
||||
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
|
||||
In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
|
||||
|
||||
Consider your entire calibration document. What is the sum of all of the calibration values?
|
||||
|
||||
Your puzzle answer was 54338.
|
||||
--- Part Two ---
|
||||
|
||||
Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
|
||||
|
||||
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
|
||||
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
|
||||
In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.
|
||||
|
||||
What is the sum of all of the calibration values?
|
||||
|
||||
Your puzzle answer was 53389.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
4
src/problems/day01/example.txt
Normal file
4
src/problems/day01/example.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
src/problems/day01/example2.txt
Normal file
7
src/problems/day01/example2.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
1000
src/problems/day01/input.txt
Normal file
1000
src/problems/day01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
11
src/problems/day01/inputs.rs
Normal file
11
src/problems/day01/inputs.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
pub const EXAMPLE_INPUT: &str = include_str!("example.txt");
|
||||
pub const EXAMPLE_OUTPUT_A: u32 = 142;
|
||||
pub const EXAMPLE_OUTPUT_B: u32 = 142;
|
||||
|
||||
pub const EXAMPLE2_INPUT: &str = include_str!("example2.txt");
|
||||
pub const EXAMPLE2_OUTPUT_A: u32 = 209;
|
||||
pub const EXAMPLE2_OUTPUT_B: u32 = 281;
|
||||
|
||||
pub const INPUT: &str = include_str!("input.txt");
|
||||
pub const OUTPUT_A: u32 = 54338;
|
||||
pub const OUTPUT_B: u32 = 53389;
|
|
@ -1,3 +1,80 @@
|
|||
pub fn solution() -> String {
|
||||
"<todo>".to_string()
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
character::{
|
||||
complete::{anychar, line_ending, satisfy},
|
||||
is_alphabetic,
|
||||
},
|
||||
combinator::{map, map_opt},
|
||||
multi::{many1, separated_list1},
|
||||
IResult,
|
||||
};
|
||||
|
||||
use self::common::{Int, Line};
|
||||
|
||||
mod common;
|
||||
pub mod inputs;
|
||||
mod test;
|
||||
|
||||
fn parse_single_digit(input: &str) -> IResult<&str, Int> {
|
||||
map_opt(anychar, |i: char| i.to_digit(10))(input)
|
||||
}
|
||||
|
||||
fn parse_single_digit_or_word(input: &str) -> IResult<&str, Int> {
|
||||
let (_, x) = alt((
|
||||
map(tag("one"), |_| 1),
|
||||
map(tag("two"), |_| 2),
|
||||
map(tag("three"), |_| 3),
|
||||
map(tag("four"), |_| 4),
|
||||
map(tag("five"), |_| 5),
|
||||
map(tag("six"), |_| 6),
|
||||
map(tag("seven"), |_| 7),
|
||||
map(tag("eight"), |_| 8),
|
||||
map(tag("nine"), |_| 9),
|
||||
parse_single_digit,
|
||||
))(input)?;
|
||||
let n = input.get(1..).unwrap_or("");
|
||||
Ok((n, x))
|
||||
}
|
||||
|
||||
fn parse_line_sub(
|
||||
f: impl FnMut(&str) -> IResult<&str, Int>,
|
||||
input: &str,
|
||||
) -> IResult<&str, Vec<Option<Int>>> {
|
||||
many1(alt((
|
||||
map(f, Some),
|
||||
map(satisfy(|c| is_alphabetic(c as u8)), |_| None),
|
||||
)))(input)
|
||||
}
|
||||
|
||||
fn parse_line(input: &str) -> IResult<&str, Line> {
|
||||
let (_, i) = parse_line_sub(parse_single_digit, input)?;
|
||||
let i: Vec<Int> = i.into_iter().flatten().collect();
|
||||
let (out, j) = parse_line_sub(parse_single_digit_or_word, input)?;
|
||||
let j: Vec<Int> = j.into_iter().flatten().collect();
|
||||
Ok((out, (i, j)))
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> IResult<&str, Vec<Line>> {
|
||||
let (res, out) = separated_list1(line_ending, parse_line)(input)?;
|
||||
Ok((res, out))
|
||||
}
|
||||
|
||||
pub fn solution(input: &str) -> Result<(Int, Int), String> {
|
||||
let (_, input) = parse_input(input).map_err(|e| e.to_string())?;
|
||||
let (a, b): (Int, Int) = input
|
||||
.into_iter()
|
||||
.map(|(ads, bds)| {
|
||||
let f = |ds: Vec<Int>| {
|
||||
if ds.is_empty() {
|
||||
0
|
||||
} else {
|
||||
let n = ds.len() - 1;
|
||||
(ds[0] * 10) + ds[n]
|
||||
}
|
||||
};
|
||||
(f(ads), f(bds))
|
||||
})
|
||||
.fold((0, 0), |(a, b), (c, d)| (a + c, b + d));
|
||||
Ok((a, b))
|
||||
}
|
||||
|
|
25
src/problems/day01/test.rs
Normal file
25
src/problems/day01/test.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
#[cfg(test)]
|
||||
use crate::problems::day01::{inputs::*, solution};
|
||||
#[cfg(test)]
|
||||
use std::assert_eq;
|
||||
|
||||
#[test]
|
||||
pub fn example_input() {
|
||||
assert_eq!(
|
||||
solution(EXAMPLE_INPUT),
|
||||
Ok((EXAMPLE_OUTPUT_A, EXAMPLE_OUTPUT_B))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn example2_input() {
|
||||
assert_eq!(
|
||||
solution(EXAMPLE2_INPUT),
|
||||
Ok((EXAMPLE2_OUTPUT_A, EXAMPLE2_OUTPUT_B))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn input() {
|
||||
assert_eq!(solution(INPUT), Ok((OUTPUT_A, OUTPUT_B)))
|
||||
}
|
Loading…
Reference in a new issue