Skip to content

Commit

Permalink
Catching up on days 7-9
Browse files Browse the repository at this point in the history
  • Loading branch information
akaritakai committed Dec 15, 2024
1 parent 2b0256e commit 7c68514
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Olivia Kaufman <[email protected]>"]
edition = "2021"

[dependencies]
itertools = "0.13.0"
lazy-regex = "3.3.0"
petgraph = "0.6.5"
reqwest = {version = "0.12.9", features = ["blocking"]}
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[![Build Status](https://github.com/akaritakai/AdventOfCode2024/actions/workflows/main.yml/badge.svg)](https://github.com/akaritakai/AdventOfCode2024/actions)
[![Code Coverage](https://img.shields.io/codecov/c/github/akaritakai/AdventOfCode2024.svg)](https://codecov.io/gh/akaritakai/AdventOfCode2024)
![Stars](https://img.shields.io/badge/gold%20stars%20⭐-12-yellow)
![Days Completed](https://img.shields.io/badge/days%20completed-6-green)
![Stars](https://img.shields.io/badge/gold%20stars%20⭐-17-yellow)
![Days Completed](https://img.shields.io/badge/days%20completed-8-green)

This repo contains my Advent of Code 2024 solutions in Rust. After providing it with your puzzle inputs (or your
session token), running the program will print out the answers to all currently solved days of the puzzle. A Docker image is provided to ensure compatibility with machines that do not want to install dependencies.
Expand All @@ -27,6 +27,11 @@ Day 05 Part 1: 5374
Day 05 Part 2: 4260
Day 06 Part 1: 5564
Day 06 Part 2: 1976
Day 07 Part 1: 20665830408335
Day 07 Part 2: 354060705047464
Day 08 Part 1: 359
Day 08 Part 2: 1293
Day 09 Part 1: 6385338159127
```

## Docker Instructions
Expand Down
2 changes: 1 addition & 1 deletion resources/tests
Submodule tests updated from 9097b8 to e99a47
143 changes: 143 additions & 0 deletions src/day07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use crate::puzzle::Puzzle;

pub struct Day {
equations: Vec<Equation>,
}

impl Puzzle for Day {
/// TODO
///
/// Time complexity: TODO
/// Auxiliary space complexity: TODO
fn solve_part_1(&self) -> String {
let mut sum = 0;
for equation in &self.equations {
if test_equation(
equation.operands[0],
equation.test_value,
&equation.operands[1..],
false,
) {
sum += equation.test_value;
}
}
sum.to_string()
}

/// TODO
///
/// Time complexity: TODO
/// Auxiliary space complexity: TODO
fn solve_part_2(&self) -> String {
let mut sum = 0;
for equation in &self.equations {
if test_equation(
equation.operands[0],
equation.test_value,
&equation.operands[1..],
true,
) {
sum += equation.test_value;
}
}
sum.to_string()
}
}

impl Day {
pub fn create(input: &str) -> Box<dyn Puzzle> {
let equations = input
.lines()
.map(|line| {
let mut parts = line.split(": ");
let test_value = parts.next().unwrap().parse().unwrap();
let operands = parts
.next()
.unwrap()
.split_whitespace()
.map(|operand| operand.parse().unwrap())
.collect();
Equation {
test_value,
operands,
}
})
.collect();
Box::new(Day { equations })
}
}

struct Equation {
test_value: u64,
operands: Vec<u64>,
}

fn test_equation(current: u64, expected: u64, remainder: &[u64], part_2: bool) -> bool {
if remainder.is_empty() {
return current == expected;
}
if current > expected {
return false;
}
test_equation(current + remainder[0], expected, &remainder[1..], part_2)
|| test_equation(current * remainder[0], expected, &remainder[1..], part_2)
|| (part_2
&& test_equation(
format!("{}{}", current, remainder[0]).parse().unwrap(),
expected,
&remainder[1..],
part_2,
))
}

#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;

#[test]
fn test_part_1_example_1() {
let input = "\
190: 10 19\n\
3267: 81 40 27\n\
83: 17 5\n\
156: 15 6\n\
7290: 6 8 6 15\n\
161011: 16 10 13\n\
192: 17 8 14\n\
21037: 9 7 18 13\n\
292: 11 6 16 20";
let puzzle = Day::create(&input);
assert_eq!(puzzle.solve_part_1(), "3749");
}

#[test]
fn test_solve_part_1() {
let input = std::fs::read_to_string(PathBuf::from("resources/tests/07")).unwrap();
let puzzle = Day::create(&input);
assert_eq!(puzzle.solve_part_1(), "20665830408335");
}

#[test]
fn test_part_2_example_1() {
let input = "\
190: 10 19\n\
3267: 81 40 27\n\
83: 17 5\n\
156: 15 6\n\
7290: 6 8 6 15\n\
161011: 16 10 13\n\
192: 17 8 14\n\
21037: 9 7 18 13\n\
292: 11 6 16 20";
let puzzle = Day::create(input);
assert_eq!(puzzle.solve_part_2(), "11387");
}

#[test]
fn test_solve_part_2() {
let input = std::fs::read_to_string(PathBuf::from("resources/tests/07")).unwrap();
let puzzle = Day::create(&input);
assert_eq!(puzzle.solve_part_2(), "354060705047464");
}
}
Loading

0 comments on commit 7c68514

Please sign in to comment.