day06
This commit is contained in:
18
Cargo.lock
generated
18
Cargo.lock
generated
@@ -40,9 +40,27 @@ version = "0.1.0"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "day06"
|
name = "day06"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iter-first-max"
|
name = "iter-first-max"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "42ace18d32276b4ce7d8261c06b376398a4ea82e4715a5b37e46fddbff41a617"
|
checksum = "42ace18d32276b4ce7d8261c06b376398a4ea82e4715a5b37e46fddbff41a617"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.14.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|||||||
26
README.md
26
README.md
@@ -1,17 +1,17 @@
|
|||||||
# Advent of Code 2025
|
# Advent of Code 2025
|
||||||
```bash
|
```bash
|
||||||
day01:A => 1150 ( 45us)
|
day01:A => 1150 ( 87us)
|
||||||
day01:B => 6738 ( 59us)
|
day01:B => 6738 ( 85us)
|
||||||
day02:A => 31839939622 ( 5012us)
|
day02:A => 31839939622 ( 8509us)
|
||||||
day02:B => 41662374059 ( 48976us)
|
day02:B => 41662374059 ( 67840us)
|
||||||
day03:A => 16812 ( 57us)
|
day03:A => 16812 ( 143us)
|
||||||
day03:B => 166345822896410 ( 163us)
|
day03:B => 166345822896410 ( 304us)
|
||||||
day04:A => 1518 ( 117us)
|
day04:A => 1518 ( 213us)
|
||||||
day04:B => 0 ( 2us)
|
day04:B => 0 ( 12us)
|
||||||
day05:A => 0 ( 4us)
|
day05:A => 0 ( 15us)
|
||||||
day05:B => 0 ( 1us)
|
day05:B => 0 ( 11us)
|
||||||
day06:A => 0 ( 3319us)
|
day06:A => 6503327062445 ( 214us)
|
||||||
day06:B => 0 ( 10us)
|
day06:B => 9640641878593 ( 39us)
|
||||||
----------
|
----------
|
||||||
59652us
|
77631us
|
||||||
```
|
```
|
||||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
itertools = "0.14.0"
|
||||||
@@ -1,32 +1,34 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use crate::part06a::EquationElement::{Number, Operant};
|
|
||||||
|
|
||||||
enum EquationElement {
|
|
||||||
Operant(char),
|
|
||||||
Number(u64),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn solve() -> u64 {
|
pub fn solve() -> u64 {
|
||||||
let mut solution: u64 = 0;
|
|
||||||
|
|
||||||
let mut file: File = File::open("src/day06/input06.txt").unwrap();
|
let mut file: File = File::open("src/day06/input06.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
let mut file_content: String = String::new();
|
||||||
file.read_to_string(&mut file_content)
|
file.read_to_string(&mut file_content)
|
||||||
.expect("Can't read file");
|
.expect("Can't read file");
|
||||||
|
|
||||||
let a = file_content
|
let mut iters: Vec<_> = file_content
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line|
|
.map(|line| line.split_whitespace())
|
||||||
line.split_whitespace().map(|operant| {
|
.collect();
|
||||||
match operant {
|
|
||||||
"+" => Operant('+'),
|
|
||||||
"*" => Operant('*'),
|
|
||||||
num => Number(num.parse::<u64>().unwrap()),
|
|
||||||
}}).collect::<Vec<EquationElement>>()
|
|
||||||
).collect::<Vec<Vec<EquationElement>>>();
|
|
||||||
|
|
||||||
a.
|
std::iter::from_fn(move || {
|
||||||
|
let column: Option<Vec<&str>> = iters
|
||||||
|
.iter_mut()
|
||||||
|
.map(|it| it.next())
|
||||||
|
.collect::<Option<Vec<_>>>();
|
||||||
|
|
||||||
solution
|
let column = column?;
|
||||||
|
|
||||||
|
Some((
|
||||||
|
column[..column.len() - 1].iter().map(|v| v.parse::<u64>().unwrap()).collect::<Vec<_>>(),
|
||||||
|
*column.last().unwrap(),
|
||||||
|
))
|
||||||
|
}).map(|(vals, op)| {
|
||||||
|
match op {
|
||||||
|
"+" => vals.iter().sum::<u64>(),
|
||||||
|
"*" => vals.iter().product::<u64>(),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}).sum::<u64>()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
pub fn solve() -> u64 {
|
pub fn solve() -> u64 {
|
||||||
let mut solution: u64 = 0;
|
let mut solution: u64 = 0;
|
||||||
@@ -9,6 +10,42 @@ pub fn solve() -> u64 {
|
|||||||
file.read_to_string(&mut file_content)
|
file.read_to_string(&mut file_content)
|
||||||
.expect("Can't read file");
|
.expect("Can't read file");
|
||||||
|
|
||||||
|
file_content
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.chars())
|
||||||
|
.tuple_windows::<(_, _, _, _, _)>()
|
||||||
|
.for_each(|(a, b, c, d, op)| {
|
||||||
|
let mut operator: char = ' ';
|
||||||
|
let mut temp: u64 = 0;
|
||||||
|
a
|
||||||
|
.zip(b)
|
||||||
|
.zip(c)
|
||||||
|
.zip(d)
|
||||||
|
.zip(op)
|
||||||
|
.map(|((((a, b), c), d), op)| {
|
||||||
|
let mut value: u64 = 0;
|
||||||
|
[a, b, c, d]
|
||||||
|
.iter()
|
||||||
|
.filter(|c| **c != ' ')
|
||||||
|
.for_each(|c| {value = value * 10 + (*c as u8 - b'0') as u64});
|
||||||
|
(value, op)
|
||||||
|
})
|
||||||
|
.for_each(|(value, op)| {
|
||||||
|
if op != ' ' {
|
||||||
|
operator = op;
|
||||||
|
solution += temp;
|
||||||
|
temp = if op == '*' {1} else {0};
|
||||||
|
}
|
||||||
|
if value == 0 { return }
|
||||||
|
match operator {
|
||||||
|
'+' => temp += value,
|
||||||
|
'*' => temp *= value,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
solution += temp;
|
||||||
|
});
|
||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user