This commit is contained in:
2025-12-02 20:48:58 +04:00
commit 8bee5bd9a3
13 changed files with 4633 additions and 0 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/target
/.direnv
/result

226
Cargo.lock generated Normal file
View File

@@ -0,0 +1,226 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "addr2line"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
dependencies = [
"gimli",
]
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aoc_2025"
version = "0.1.0"
dependencies = [
"color-eyre",
]
[[package]]
name = "backtrace"
version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-link",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "color-eyre"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5920befb47832a6d61ee3a3a846565cfa39b331331e68a3b1d1116630f2f26d"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b88ea9df13354b55bc7234ebcce36e6ef896aca2e42a15de9e10edce01b427"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "eyre"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "gimli"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
[[package]]
name = "indenter"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "964de6e86d545b246d84badc0fef527924ace5134f30641c203ef52ba83f58d5"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.178"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
]
[[package]]
name = "object"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "owo-colors"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c6901729fa79e91a0913333229e9ca5dc725089d1c363b2f4b4760709dc4a52"
[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "rustc-demangle"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "thread_local"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
dependencies = [
"cfg-if",
]
[[package]]
name = "tracing"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d15d90a0b5c19378952d479dc858407149d7bb45a14de0142f6c534b16fc647"
dependencies = [
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a04e24fab5c89c6a36eb8558c9656f30d81de51dfa4d3b45f26b21d61fa0a6c"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1581020d7a273442f5b45074a6a57d5757ad0a47dac0e9f0bd57b81936f3db"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

16
Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
cargo-features = ["codegen-backend"]
[package]
name = "aoc_2025"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "day01"
path = "src/bin/day01/main.rs"
[profile.dev]
codegen-backend = "cranelift"
opt-level = 0
[dependencies]
color-eyre = "0.6.5"

112
flake.lock generated Normal file
View File

@@ -0,0 +1,112 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1763938834,
"narHash": "sha256-j8iB0Yr4zAvQLueCZ5abxfk6fnG/SJ5JnGUziETjwfg=",
"owner": "ipetkov",
"repo": "crane",
"rev": "d9e753122e51cee64eb8d2dddfe11148f339f5a2",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1764642553,
"narHash": "sha256-mvbFFzVBhVK1FjyPHZGMAKpNiqkr7k++xIwy+p/NQvA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f720de59066162ee879adcc8c79e15c51fe6bfb4",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1744536153,
"narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"crane": "crane",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1764643237,
"narHash": "sha256-6Ezx9DqVv5UZ7DBK9rcNwBuQUENFyWPS7M09I+FvNao=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "e66d6b924ac59e6c722f69332f6540ea57c69233",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

43
flake.nix Normal file
View File

@@ -0,0 +1,43 @@
{
description = "";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs =
{
nixpkgs,
crane,
flake-utils,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
craneLib = (crane.mkLib pkgs).overrideToolchain (
p:
p.rust-bin.nightly.latest.default.override {
extensions = [
"rustc-codegen-cranelift-preview"
"rust-analyzer"
"rust-src"
];
}
);
in
{
devShells.default = craneLib.devShell {
packages = [ ];
};
}
);
}

4068
inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

10
inputs/day1example.txt Normal file
View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

1
inputs/day2.txt Normal file
View File

@@ -0,0 +1 @@
328412-412772,1610-2974,163-270,7693600637-7693779967,352-586,65728-111612,734895-926350,68-130,183511-264058,8181752851-8181892713,32291-63049,6658-12472,720-1326,21836182-21869091,983931-1016370,467936-607122,31-48,6549987-6603447,8282771161-8282886238,7659673-7828029,2-18,7549306131-7549468715,3177-5305,20522-31608,763697750-763835073,5252512393-5252544612,6622957-6731483,9786096-9876355,53488585-53570896

1
inputs/day2example.txt Normal file
View File

@@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

69
src/bin/day01/main.rs Normal file
View File

@@ -0,0 +1,69 @@
use std::{
fs::File,
io::{BufRead, BufReader},
};
use color_eyre::{
Report,
eyre::{ContextCompat, eyre},
};
#[derive(Debug, Clone)]
enum DialDir {
L(u32),
R(u32),
}
impl From<DialDir> for i32 {
fn from(val: DialDir) -> Self {
match val {
DialDir::L(v) => -(v as i32),
DialDir::R(v) => v as i32,
}
}
}
impl TryFrom<&str> for DialDir {
type Error = Report;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let prefix = value.get(0..1).context("Failed to get first char")?;
match prefix {
"L" => value[1..].parse().map(DialDir::L).map_err(|v| v.into()),
"R" => value[1..].parse().map(DialDir::R).map_err(|v| v.into()),
_ => Err(eyre!("Invalid Start")),
}
}
}
fn main() -> Result<(), Report> {
color_eyre::install()?;
// let filepath = "testinput.txt";
let filepath = "inputs/day1.txt";
let file = File::open(filepath)?;
let bufreader = BufReader::new(file);
let mut lines = bufreader.lines();
let mut total_value = 50;
let mut code = 0;
let mut code2 = 0;
while let Some(Ok(line)) = lines.next() {
let prev = total_value;
let dialcode = DialDir::try_from(line.as_str())?;
total_value += std::convert::Into::<i32>::into(dialcode.clone());
let big_jump = total_value.div_euclid(100);
total_value = total_value.rem_euclid(100);
if big_jump < 0 && prev == 0 {
code2 -= 1;
}
if big_jump > 0 && total_value == 0 {
code2 -= 1;
}
code2 += big_jump.abs();
if total_value == 0 {
code += 1;
}
println!("{dialcode:?} {prev}->{total_value} {code} {code2}");
}
println!("code is {} {}!", code, code + code2);
Ok(())
}

82
src/bin/day02/main.rs Normal file
View File

@@ -0,0 +1,82 @@
use color_eyre::{Report, eyre::ContextCompat};
use std::{fs::File, io::Read};
#[derive(Debug)]
struct IdRange {
start: u64,
end: u64,
}
impl TryFrom<&str> for IdRange {
type Error = Report;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let mut split = value.split("-");
let start: u64 = split.next().context("Invalid start ID")?.parse()?;
let end: u64 = split.next().context("Invalid end ID")?.parse()?;
Ok(Self { start, end })
}
}
impl IdRange {
fn get_invalid(&self) -> Result<Vec<u64>, Report> {
Ok((self.start..self.end + 1)
.filter(|v| {
let str = v.to_string();
let len = str.len();
if (len % 2) != 0 {
return false;
}
str[0..len / 2] == str[len / 2..len]
})
.collect())
}
fn get_invalid2(&self) -> Result<Vec<u64>, Report> {
Ok((self.start..self.end + 1)
.filter(|v| {
let str = v.to_string();
let len = str.len();
(1..len + 1)
.filter(|v| (len % v) == 0)
.filter(|v| (len / v) != 1)
.filter(|v| {
let strchunk = str.chars().collect::<Vec<char>>();
let strchunk = strchunk.chunks(*v).collect::<Vec<&[char]>>();
!strchunk.iter().any(|v| strchunk.iter().any(|x| v != x))
})
.sum::<usize>()
> 0
})
.collect())
}
}
fn main() -> Result<(), Report> {
color_eyre::install()?;
// let filepath = "testinput2.txt";
let filepath = "inputs/day2.txt";
let mut file = File::open(filepath)?;
let mut string = String::new();
let _ = file.read_to_string(&mut string);
let ranges = string
.trim_end()
.split(",")
.map(IdRange::try_from)
.collect::<Result<Vec<IdRange>, Report>>()?;
let code1 = ranges
.iter()
.map(|v| v.get_invalid().map(|v| v.into_iter().sum::<u64>()))
.collect::<Result<Vec<u64>, Report>>()?
.into_iter()
.sum::<u64>();
println!("code1: {code1}");
let code2 = ranges
.iter()
.map(|v| v.get_invalid2().map(|v| v.into_iter().sum::<u64>()))
.collect::<Result<Vec<u64>, Report>>()?
.into_iter()
.sum::<u64>();
println!("code1: {code2}");
Ok(())
}

1
src/lib.rs Normal file
View File

@@ -0,0 +1 @@