mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 18:01:34 +01:00
Compare commits
No commits in common. "e3b750544f250d7f6e580c08ad9398a675d13894" and "5929ef6b973acf4c4ab0de25f54b8b012e9afc74" have entirely different histories.
e3b750544f
...
5929ef6b97
4 changed files with 3 additions and 143 deletions
7
2025/rust/day-06/Cargo.lock
generated
7
2025/rust/day-06/Cargo.lock
generated
|
|
@ -1,7 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 4
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "day-06"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "day-06"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
|
|
@ -1,127 +0,0 @@
|
||||||
use std::{fmt::Debug, fs, str::FromStr};
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
|
||||||
enum Operation {
|
|
||||||
Add,
|
|
||||||
Multiply,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for Operation {
|
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
fn from_str(input: &str) -> Result<Operation, Self::Err> {
|
|
||||||
match input {
|
|
||||||
"+" => Ok(Operation::Add),
|
|
||||||
"*" => Ok(Operation::Multiply),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Problem {
|
|
||||||
numbers: Vec<u64>,
|
|
||||||
operation: Operation,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Problem {
|
|
||||||
fn solve(&self) -> u64 {
|
|
||||||
match self.operation {
|
|
||||||
Operation::Add => self.numbers.iter().sum(),
|
|
||||||
Operation::Multiply => self.numbers.iter().product(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let content = &mut fs::read_to_string("../../inputs/06.txt").unwrap();
|
|
||||||
|
|
||||||
println!("Grand total 1: {}", grand_total(&parse_input_v1(content)));
|
|
||||||
println!("Grand total 2: {}", grand_total(&parse_input_v2(content)));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_input_v1(contents: &str) -> Vec<Problem> {
|
|
||||||
let grid: Vec<Vec<&str>> = contents
|
|
||||||
.trim()
|
|
||||||
.lines()
|
|
||||||
.map(|line| line.split_whitespace().collect())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let operations_row = grid.last().unwrap();
|
|
||||||
let num_cols = grid[0].len();
|
|
||||||
|
|
||||||
(0..num_cols)
|
|
||||||
.map(|col_index| {
|
|
||||||
let operation = Operation::from_str(operations_row[col_index]).unwrap();
|
|
||||||
|
|
||||||
let numbers = grid[..grid.len() - 1]
|
|
||||||
.iter()
|
|
||||||
.map(|row| row[col_index].parse().unwrap())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Problem { numbers, operation }
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_input_v2(contents: &str) -> Vec<Problem> {
|
|
||||||
let grid: Vec<Vec<u8>> = contents
|
|
||||||
.trim_end_matches('\n')
|
|
||||||
.lines()
|
|
||||||
.map(|line| line.as_bytes().to_vec())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let operations_row = grid.last().unwrap();
|
|
||||||
|
|
||||||
let mut problems = Vec::new();
|
|
||||||
|
|
||||||
let mut numbers = Vec::new();
|
|
||||||
let mut operation = Operation::Add;
|
|
||||||
|
|
||||||
for col in 0..grid[0].len() {
|
|
||||||
if let Ok(op) = Operation::from_str(&(operations_row[col] as char).to_string()) {
|
|
||||||
operation = op;
|
|
||||||
}
|
|
||||||
|
|
||||||
let column: String = (0..grid.len() - 1)
|
|
||||||
.map(|row| grid[row][col] as char)
|
|
||||||
.filter(|c| !c.is_whitespace())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if column.is_empty() {
|
|
||||||
problems.push(Problem { numbers, operation });
|
|
||||||
numbers = Vec::new();
|
|
||||||
operation = Operation::Add;
|
|
||||||
} else {
|
|
||||||
numbers.push(column.parse().unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
problems.push(Problem { numbers, operation });
|
|
||||||
|
|
||||||
problems
|
|
||||||
}
|
|
||||||
|
|
||||||
fn grand_total(problems: &[Problem]) -> u64 {
|
|
||||||
problems.iter().map(Problem::solve).sum()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
const TEST_INPUT: &str = "123 328 51 64
|
|
||||||
45 64 387 23
|
|
||||||
6 98 215 314
|
|
||||||
* + * +
|
|
||||||
";
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_part1() {
|
|
||||||
assert_eq!(grand_total(&parse_input_v1(TEST_INPUT)), 4277556);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_part2() {
|
|
||||||
assert_eq!(grand_total(&parse_input_v2(TEST_INPUT)), 3263827);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
6
flake.lock
generated
6
flake.lock
generated
|
|
@ -3,10 +3,10 @@
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake": false,
|
"flake": false,
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1765025072,
|
"lastModified": 1764926544,
|
||||||
"narHash": "sha256-mTX6WtSh/OWj4nAsDdr8fkAOPZgANHQc/DURFczQBC4=",
|
"narHash": "sha256-xIkgV9KrBel6Qwy673iMmmggFGZ9Zz93Hd/lGhRyHRM=",
|
||||||
"ref": "refs/heads/main",
|
"ref": "refs/heads/main",
|
||||||
"rev": "b425d990c6bf65f69566c0167e1ff8049c87bc2a",
|
"rev": "27de1eaccb499f25dca8f3505b4c4f42a8e35514",
|
||||||
"shallow": true,
|
"shallow": true,
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "ssh://git@github.com/SebastianStork/advent-of-code-inputs.git"
|
"url": "ssh://git@github.com/SebastianStork/advent-of-code-inputs.git"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue