mirror of
https://github.com/SebastianStork/advent-of-code.git
synced 2026-01-21 11:01:34 +01:00
2025/day-06: Solve part 1
This commit is contained in:
parent
5929ef6b97
commit
5bb9bcc7b3
4 changed files with 93 additions and 3 deletions
7
2025/rust/day-06/Cargo.lock
generated
Normal file
7
2025/rust/day-06/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day-06"
|
||||
version = "0.1.0"
|
||||
6
2025/rust/day-06/Cargo.toml
Normal file
6
2025/rust/day-06/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day-06"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
77
2025/rust/day-06/src/main.rs
Normal file
77
2025/rust/day-06/src/main.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
use std::{fmt::Debug, fs};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum Operation {
|
||||
Add,
|
||||
Multiply,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Problem {
|
||||
numbers: Vec<u64>,
|
||||
operation: Operation,
|
||||
}
|
||||
|
||||
impl Problem {
|
||||
fn solve(&self) -> u64 {
|
||||
if self.operation == Operation::Add {
|
||||
self.numbers.iter().sum()
|
||||
} else {
|
||||
self.numbers.iter().product()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let problems = parse_input(&fs::read_to_string("../../inputs/06.txt").unwrap());
|
||||
|
||||
println!("Grand total: {}", grand_total(&problems));
|
||||
}
|
||||
|
||||
fn parse_input(contents: &str) -> Vec<Problem> {
|
||||
let grid: Vec<Vec<&str>> = contents
|
||||
.trim()
|
||||
.lines()
|
||||
.map(|line| line.split_whitespace().collect())
|
||||
.collect();
|
||||
|
||||
let mut list: Vec<Problem> = Vec::new();
|
||||
|
||||
for (col, _) in grid[0].iter().enumerate() {
|
||||
let mut numbers: Vec<u64> = Vec::new();
|
||||
|
||||
let operation = match grid[grid.len() - 1][col] {
|
||||
"+" => Operation::Add,
|
||||
"*" => Operation::Multiply,
|
||||
_ => panic!("Unsupported Operation"),
|
||||
};
|
||||
|
||||
for (row, _) in grid[..grid.len() - 1].iter().enumerate() {
|
||||
numbers.push(grid[row][col].parse().unwrap());
|
||||
}
|
||||
|
||||
list.push(Problem { numbers, operation });
|
||||
}
|
||||
|
||||
list
|
||||
}
|
||||
|
||||
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(TEST_INPUT)), 4277556);
|
||||
}
|
||||
}
|
||||
6
flake.lock
generated
6
flake.lock
generated
|
|
@ -3,10 +3,10 @@
|
|||
"inputs": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1764926544,
|
||||
"narHash": "sha256-xIkgV9KrBel6Qwy673iMmmggFGZ9Zz93Hd/lGhRyHRM=",
|
||||
"lastModified": 1765025072,
|
||||
"narHash": "sha256-mTX6WtSh/OWj4nAsDdr8fkAOPZgANHQc/DURFczQBC4=",
|
||||
"ref": "refs/heads/main",
|
||||
"rev": "27de1eaccb499f25dca8f3505b4c4f42a8e35514",
|
||||
"rev": "b425d990c6bf65f69566c0167e1ff8049c87bc2a",
|
||||
"shallow": true,
|
||||
"type": "git",
|
||||
"url": "ssh://git@github.com/SebastianStork/advent-of-code-inputs.git"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue