mirror of
https://git.sr.ht/~anthonycicc/advent_of_code_2023
synced 2025-04-06 00:32:33 -04:00
Day 01
This commit is contained in:
parent
ca12d38587
commit
bf60586c80
15 changed files with 4326 additions and 27 deletions
38
.gitignore
vendored
38
.gitignore
vendored
|
@ -1,2 +1,36 @@
|
|||
/target
|
||||
result*
|
||||
### https://raw.github.com/github/gitignore/ca12d385871268aedd8d4b42b827fa7bd2aa0dda/Rust.gitignore
|
||||
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
|
||||
### https://raw.github.com/github/gitignore/4488915eec0b3a45b5c63ead28f286819c0917de/Rust.gitignore
|
||||
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
|
||||
|
|
121
Cargo.lock
generated
121
Cargo.lock
generated
|
@ -6,6 +6,15 @@ version = 3
|
|||
name = "advent_of_code_2023"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.4"
|
||||
|
@ -61,6 +70,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "41fffed7514f420abec6d183b1d3acfd9099c79c3a10a06ade4f8203f1411272"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -75,6 +85,18 @@ dependencies = [
|
|||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.4.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.6.0"
|
||||
|
@ -88,6 +110,7 @@ dependencies = [
|
|||
"clap",
|
||||
"common",
|
||||
"day_01",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -108,14 +131,112 @@ name = "day_01"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"common",
|
||||
"regex",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.70"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.1"
|
||||
|
|
|
@ -7,4 +7,7 @@ license = "MIT"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[workspace]
|
||||
members = ['common', 'cli']
|
||||
members = ['common', 'cli', 'day_01']
|
||||
|
||||
[workspace.dependencies]
|
||||
thiserror = "1"
|
|
@ -6,6 +6,9 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4" }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
common = { path = "../common" }
|
||||
day_01 = { path = "../day_01" }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::ops::Deref;
|
||||
use std::path::Path;
|
||||
use std::{error::Error, fs};
|
||||
|
||||
use clap::{ArgGroup, Parser};
|
||||
|
@ -29,8 +31,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
};
|
||||
|
||||
match (cli.day, cli.part) {
|
||||
(day @ Day::One, part @ Part::One) => {
|
||||
println!("{}", day_01::run_day_and_part(day, part, input_str));
|
||||
(Day::One, part) => {
|
||||
println!("{}", day_01::run_part(part, input_str).unwrap().deref());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -6,4 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = {version = "4.4.10"}
|
||||
clap = {version = "4.4.10", features = ["derive"]}
|
||||
|
|
|
@ -6,4 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
common = {path = "../common"}
|
||||
common = { path = "../common" }
|
||||
regex = "1"
|
||||
thiserror = { workspace = true }
|
1
day_01/process.sd
Normal file
1
day_01/process.sd
Normal file
|
@ -0,0 +1 @@
|
|||
cat raw_d1p1.txt | sd oneight oneeight | sd threeight threeeight | sd fiveight fiveeight | sd nineight nineeight | sd twone twoone | sd sevenine sevennine | sd eightwo eighttwo > d1p1.txt
|
|
@ -1,7 +1,139 @@
|
|||
use common::{Day, Part};
|
||||
use common::Part;
|
||||
use regex::Regex;
|
||||
use std::fmt::Display;
|
||||
|
||||
pub fn run_day_and_part(day: Day, part: Part, inp: String) -> Result<(), ()> {
|
||||
match (day, part) {
|
||||
(Day::One, Part::One) => Ok(()),
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Day01Error {
|
||||
#[error("something weird happened")]
|
||||
RuntimeError,
|
||||
}
|
||||
|
||||
pub fn run_part(part: Part, inp: String) -> Result<Box<dyn Display>, Day01Error> {
|
||||
match part {
|
||||
Part::One => Ok(Box::new(part_01(&inp))),
|
||||
Part::Two => Ok(Box::new(part_02(&inp))),
|
||||
}
|
||||
}
|
||||
|
||||
fn part_01(inp: &str) -> usize {
|
||||
let split_inp = inp.lines();
|
||||
|
||||
let mut sum = 0;
|
||||
for line in split_inp {
|
||||
let matches: Vec<&str> = line.matches(char::is_numeric).collect();
|
||||
let (first, last) = (matches.first().unwrap(), matches.last().unwrap());
|
||||
sum += format!("{}{}", first, last).parse::<usize>().unwrap();
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
fn get_nums(inp: &str) -> Vec<&str> {
|
||||
let re =
|
||||
Regex::new(r"(one|two|three|four|five|six|seven|eight|nine|1|2|3|4|5|6|7|8|9)").unwrap();
|
||||
return re.find_iter(inp).map(|m| m.as_str()).collect();
|
||||
}
|
||||
|
||||
fn convert_num(inp: &str) -> usize {
|
||||
match inp {
|
||||
"1" | "one" => 1,
|
||||
"2" | "two" => 2,
|
||||
"3" | "three" => 3,
|
||||
"4" | "four" => 4,
|
||||
"5" | "five" => 5,
|
||||
"6" | "six" => 6,
|
||||
"7" | "seven" => 7,
|
||||
"8" | "eight" => 8,
|
||||
"9" | "nine" => 9,
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn part_02(inp: &str) -> usize {
|
||||
let split_inp = inp.lines().map(|line| get_nums(line)).collect::<Vec<_>>();
|
||||
|
||||
let mut sum: usize = 0;
|
||||
for line in split_inp {
|
||||
let (first, last) = (
|
||||
convert_num(line.first().unwrap()),
|
||||
convert_num(line.last().unwrap()),
|
||||
);
|
||||
sum += format!("{}{}", first, last).parse::<usize>().unwrap()
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod part_01_tests {
|
||||
use crate::part_01;
|
||||
|
||||
#[test]
|
||||
fn test_line() {
|
||||
assert_eq!(part_01("1abc2"), 12)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lines() {
|
||||
assert_eq!(
|
||||
part_01(
|
||||
r#"1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet"#
|
||||
),
|
||||
142
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod part_02_tests {
|
||||
use crate::{get_nums, part_02};
|
||||
|
||||
#[test]
|
||||
fn test_regex() {
|
||||
assert_eq!(get_nums("two1nine"), vec!["two", "1", "nine"])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line() {
|
||||
assert_eq!(part_02("two1nine"), 29)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lines() {
|
||||
assert_eq!(
|
||||
part_02(
|
||||
r#"two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen"#
|
||||
),
|
||||
281
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_more_lines() {
|
||||
assert_eq!(
|
||||
part_02(
|
||||
r#"ckmb52fldxkseven3fkjgcbzmnr7
|
||||
gckhqpb6twoqnjxqplthree2fourkspnsnzxlz1
|
||||
2onetwocrgbqm7
|
||||
frkh2nineqmqxrvdsevenfive
|
||||
four9two
|
||||
six7sixqrdfive3twonehsk
|
||||
xkvsone2
|
||||
one65
|
||||
rggxsff1seven
|
||||
djbcgrrtqdshpqqzj43rgcr"#
|
||||
),
|
||||
361
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
30
flake.lock
generated
30
flake.lock
generated
|
@ -23,12 +23,12 @@
|
|||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1699217310,
|
||||
"narHash": "sha256-xpW3VFUG7yE6UE6Wl0dhqencuENSkV7qpnpe9I8VbPw=",
|
||||
"rev": "d535642bbe6f377077f7c23f0febb78b1463f449",
|
||||
"revCount": 457,
|
||||
"lastModified": 1701384536,
|
||||
"narHash": "sha256-PdYjpXmkn4FYJU7uvmCa54b0PPXBgDZHJaJpSEWR1Ek=",
|
||||
"rev": "07c531adf572dafd494db0672cdac00e48216171",
|
||||
"revCount": 469,
|
||||
"type": "tarball",
|
||||
"url": "https://api.flakehub.com/f/pinned/ipetkov/crane/0.15.0/018ba14c-834e-709d-9b6e-fe0e1521f57e/source.tar.gz"
|
||||
"url": "https://api.flakehub.com/f/pinned/ipetkov/crane/0.15.1/018c226e-9985-758b-9809-17419c4ebd2d/source.tar.gz"
|
||||
},
|
||||
"original": {
|
||||
"type": "tarball",
|
||||
|
@ -43,12 +43,12 @@
|
|||
"rust-analyzer-src": []
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1698819743,
|
||||
"narHash": "sha256-L3vZfifHmog7sJvzXk8qiKISkpyltb+GaThqMJ7PU9Y=",
|
||||
"rev": "1a92c6d75963fd594116913c23041da48ed9e020",
|
||||
"revCount": 1653,
|
||||
"lastModified": 1701411808,
|
||||
"narHash": "sha256-K8QDx8UgbvGdENuvPvcsCXcd8brd55OkRDFLBT7xUVY=",
|
||||
"rev": "3776d0e2a30184cc6a0ba20fb86dc6df5b41fccd",
|
||||
"revCount": 1694,
|
||||
"type": "tarball",
|
||||
"url": "https://api.flakehub.com/f/pinned/nix-community/fenix/0.1.1653%2Brev-1a92c6d75963fd594116913c23041da48ed9e020/018b89f2-5d13-73a7-9511-233a39510d75/source.tar.gz"
|
||||
"url": "https://api.flakehub.com/f/pinned/nix-community/fenix/0.1.1694%2Brev-3776d0e2a30184cc6a0ba20fb86dc6df5b41fccd/018c2471-ae06-7cb3-a542-c80f7a549d7b/source.tar.gz"
|
||||
},
|
||||
"original": {
|
||||
"type": "tarball",
|
||||
|
@ -74,12 +74,12 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1701068326,
|
||||
"narHash": "sha256-vmMceA+q6hG1yrjb+MP8T0YFDQIrW3bl45e7z24IEts=",
|
||||
"rev": "8cfef6986adfb599ba379ae53c9f5631ecd2fd9c",
|
||||
"revCount": 553283,
|
||||
"lastModified": 1701253981,
|
||||
"narHash": "sha256-ztaDIyZ7HrTAfEEUt9AtTDNoCYxUdSd6NrRHaYOIxtk=",
|
||||
"rev": "e92039b55bcd58469325ded85d4f58dd5a4eaf58",
|
||||
"revCount": 554114,
|
||||
"type": "tarball",
|
||||
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.553283%2Brev-8cfef6986adfb599ba379ae53c9f5631ecd2fd9c/018c18d1-b364-7bfd-aced-a123b87538af/source.tar.gz"
|
||||
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.554114%2Brev-e92039b55bcd58469325ded85d4f58dd5a4eaf58/018c246f-3485-7920-b58c-92909d475b54/source.tar.gz"
|
||||
},
|
||||
"original": {
|
||||
"type": "tarball",
|
||||
|
|
|
@ -131,6 +131,7 @@
|
|||
|
||||
# Extra inputs can be added here; cargo and rustc are provided by default.
|
||||
packages = [
|
||||
pkgs.sd
|
||||
# pkgs.ripgrep
|
||||
];
|
||||
};
|
||||
|
|
1000
inputs/acicchetti-jf/day_01/d1p1.txt
Normal file
1000
inputs/acicchetti-jf/day_01/d1p1.txt
Normal file
File diff suppressed because it is too large
Load diff
1000
inputs/acicchetti-jf/day_01/d1p2.txt
Normal file
1000
inputs/acicchetti-jf/day_01/d1p2.txt
Normal file
File diff suppressed because it is too large
Load diff
1000
inputs/anthonycicc/day_01/d1p1.txt
Normal file
1000
inputs/anthonycicc/day_01/d1p1.txt
Normal file
File diff suppressed because it is too large
Load diff
1000
inputs/anthonycicc/day_01/d1p2.txt
Normal file
1000
inputs/anthonycicc/day_01/d1p2.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue