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

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 @@