Skip to content

Commit

Permalink
Solution functions take input
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 26, 2023
1 parent 56504d9 commit e3688dd
Show file tree
Hide file tree
Showing 42 changed files with 229 additions and 274 deletions.
10 changes: 5 additions & 5 deletions aoc_2021/src/day_01.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

pub struct Day01;

Expand All @@ -7,8 +7,8 @@ impl Solution for Day01 {
"Sonar Sweep"
}

fn part_a(&self) -> String {
let data = problem::load(2021, 1)
fn part_a(&self, input: &str) -> String {
let data = input
.lines()
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>();
Expand All @@ -17,8 +17,8 @@ impl Solution for Day01 {
inc.to_string()
}

fn part_b(&self) -> String {
let d = problem::load(2021, 1)
fn part_b(&self, input: &str) -> String {
let d = input
.lines()
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>();
Expand Down
12 changes: 5 additions & 7 deletions aoc_2021/src/day_02.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

pub struct Day02;

Expand All @@ -7,12 +7,11 @@ impl Solution for Day02 {
"Dive!"
}

fn part_a(&self) -> String {
let d = problem::load(2021, 2);
fn part_a(&self, input: &str) -> String {
let mut dep: u32 = 0;
let mut hor: u32 = 0;

for i in d.lines() {
for i in input.lines() {
let seg = i.split(' ').collect::<Vec<&str>>();
let x = seg[1].parse::<u32>().unwrap();

Expand All @@ -27,13 +26,12 @@ impl Solution for Day02 {
(dep * hor).to_string()
}

fn part_b(&self) -> String {
let d = problem::load(2021, 2);
fn part_b(&self, input: &str) -> String {
let mut dep: u32 = 0;
let mut hor: u32 = 0;
let mut aim: u32 = 0;

for i in d.lines() {
for i in input.lines() {
let seg = i.split(' ').collect::<Vec<&str>>();
let x = seg[1].parse::<u32>().unwrap();

Expand Down
16 changes: 7 additions & 9 deletions aoc_2021/src/day_03.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

pub struct Day03;

Expand All @@ -7,9 +7,8 @@ impl Solution for Day03 {
"Binary Diagnostic"
}

fn part_a(&self) -> String {
let data = problem::load(2021, 3);
let num_len = data.lines().next().unwrap().len();
fn part_a(&self, input: &str) -> String {
let num_len = input.lines().next().unwrap().len();

let mut gamma = vec![0; num_len];
let mut epsilon = vec![1; num_len];
Expand All @@ -18,7 +17,7 @@ impl Solution for Day03 {
let mut z = 0;
let mut o = 0;

data.lines().for_each(|j| match j.chars().nth(i).unwrap() {
input.lines().for_each(|j| match j.chars().nth(i).unwrap() {
'0' => z += 1,
'1' => o += 1,
_ => {}
Expand All @@ -36,11 +35,10 @@ impl Solution for Day03 {
(epsilon * gamma).to_string()
}

fn part_b(&self) -> String {
let data = problem::load(2021, 3);
let num_len = data.lines().next().unwrap().len();
fn part_b(&self, input: &str) -> String {
let num_len = input.lines().next().unwrap().len();

let mut oxygen_keep = data.lines().collect::<Vec<&str>>();
let mut oxygen_keep = input.lines().collect::<Vec<&str>>();
let mut oxygen_raw = vec![[0, 0]; num_len];
let mut oxygen_gen = 0;

Expand Down
14 changes: 7 additions & 7 deletions aoc_2021/src/day_04.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

pub struct Day04;

Expand All @@ -7,15 +7,15 @@ impl Solution for Day04 {
"Giant Squid"
}

fn part_a(&self) -> String {
let bingo = Bingo::parse_input(problem::load(2021, 4));
fn part_a(&self, input: &str) -> String {
let bingo = Bingo::parse_input(input);
let winning = bingo.solve();

winning.0[winning.1].final_out(winning.2).to_string()
}

fn part_b(&self) -> String {
let bingo = Bingo::parse_input(problem::load(2021, 4));
fn part_b(&self, input: &str) -> String {
let bingo = Bingo::parse_input(input);
let loseing = bingo.loseing_solve();

loseing.0[loseing.1].final_out(loseing.2).to_string()
Expand All @@ -36,7 +36,7 @@ struct Board {
}

impl Bingo {
fn parse_input(inp: String) -> Self {
fn parse_input(inp: &str) -> Self {
let mut lines = inp.lines();
let numbers = lines
.next()
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Bingo {
}

impl Board {
fn parse(inp: String) -> Vec<Self> {
fn parse(inp: &str) -> Vec<Self> {
let mut boards = Vec::new();
let inp = inp.replace('\r', "");
let raw_boards = inp.split("\n\n").skip(1);
Expand Down
16 changes: 8 additions & 8 deletions aoc_2021/src/day_05.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

use hashbrown::HashMap;

Expand All @@ -9,18 +9,18 @@ impl Solution for Day05 {
"Hydrothermal Venture"
}

fn part_a(&self) -> String {
run(false).to_string()
fn part_a(&self, input: &str) -> String {
run(input, false).to_string()
}

fn part_b(&self) -> String {
run(true).to_string()
fn part_b(&self, input: &str) -> String {
run(input, true).to_string()
}
}

/// dig -> Weather to include Diagonal Lines
fn run(dig: bool) -> u32 {
let data = Segment::parse_inp(problem::load(2021, 5), dig).unwrap();
fn run(input: &str, dig: bool) -> u32 {
let data = Segment::parse_inp(input, dig).unwrap();
let mut all_loc = HashMap::new();

for x in data {
Expand All @@ -47,7 +47,7 @@ struct Segment {
}

impl Segment {
fn parse_inp(inp: String, dig: bool) -> Option<Vec<Segment>> {
fn parse_inp(inp: &str, dig: bool) -> Option<Vec<Segment>> {
let mut out = Vec::new();

for line in inp.lines() {
Expand Down
12 changes: 6 additions & 6 deletions aoc_2021/src/day_06.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

use std::hash::Hash;

Expand All @@ -11,15 +11,15 @@ impl Solution for Day06 {
"Lanternfish"
}

fn part_a(&self) -> String {
let data = Fish::parse_inp(problem::load(2021, 6));
fn part_a(&self, input: &str) -> String {
let data = Fish::parse_inp(input);
let out = Fish::sim(data, 80);

out.to_string()
}

fn part_b(&self) -> String {
let data = Fish::parse_inp(problem::load(2021, 6));
fn part_b(&self, input: &str) -> String {
let data = Fish::parse_inp(input);
let out = Fish::sim(data, 256);

out.to_string()
Expand All @@ -36,7 +36,7 @@ impl Fish {
Fish { timer }
}

fn parse_inp(inp: String) -> Vec<Fish> {
fn parse_inp(inp: &str) -> Vec<Fish> {
inp.lines()
.next()
.unwrap()
Expand Down
12 changes: 6 additions & 6 deletions aoc_2021/src/day_07.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

pub struct Day07;

Expand All @@ -7,8 +7,8 @@ impl Solution for Day07 {
"The Treachery of Whales"
}

fn part_a(&self) -> String {
let data = parse_crabs(problem::load(2021, 7));
fn part_a(&self, input: &str) -> String {
let data = parse_crabs(input);

let min = data.iter().min().unwrap();
let max = data.iter().max().unwrap();
Expand All @@ -24,8 +24,8 @@ impl Solution for Day07 {
this_min.to_string()
}

fn part_b(&self) -> String {
let data = parse_crabs(problem::load(2021, 7));
fn part_b(&self, input: &str) -> String {
let data = parse_crabs(input);

let min = data.iter().min().unwrap();
let max = data.iter().max().unwrap();
Expand All @@ -42,7 +42,7 @@ impl Solution for Day07 {
}
}

fn parse_crabs(inp: String) -> Vec<u32> {
fn parse_crabs(inp: &str) -> Vec<u32> {
inp.lines()
.next()
.unwrap()
Expand Down
12 changes: 6 additions & 6 deletions aoc_2021/src/day_08.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

use hashbrown::HashMap;

Expand All @@ -15,8 +15,8 @@ impl Solution for Day08 {
"Seven Segment Search"
}

fn part_a(&self) -> String {
let data = parse(problem::load(2021, 8));
fn part_a(&self, input: &str) -> String {
let data = parse(input);
let mut inc = 0;

for i in data {
Expand All @@ -29,8 +29,8 @@ impl Solution for Day08 {
inc.to_string()
}

fn part_b(&self) -> String {
let data = parse(problem::load(2021, 8));
fn part_b(&self, input: &str) -> String {
let data = parse(input);
let mut inc = 0;

let perms = permutations(CHARS.to_vec());
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Solution for Day08 {
}
}

fn parse(inp: String) -> Vec<(Vec<String>, Vec<String>)> {
fn parse(inp: &str) -> Vec<(Vec<String>, Vec<String>)> {
let mut out = Vec::new();

for i in inp.lines() {
Expand Down
12 changes: 6 additions & 6 deletions aoc_2021/src/day_09.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

pub struct Day09;

Expand All @@ -7,22 +7,22 @@ impl Solution for Day09 {
"Smoke Basin"
}

fn part_a(&self) -> String {
let data = parse(problem::load(2021, 9));
fn part_a(&self, input: &str) -> String {
let data = parse(input);
let low = lowest(data);

low.iter().map(|x| *x + 1).sum::<u32>().to_string()
}

fn part_b(&self) -> String {
let data = parse(problem::load(2021, 9));
fn part_b(&self, input: &str) -> String {
let data = parse(input);
let basins = basins(data);

basins.iter().rev().take(3).product::<u32>().to_string()
}
}

fn parse(inp: String) -> Vec<Vec<u32>> {
fn parse(inp: &str) -> Vec<Vec<u32>> {
inp.lines()
.map(|x| x.chars().map(|f| f.to_digit(10).unwrap()).collect())
.collect()
Expand Down
12 changes: 6 additions & 6 deletions aoc_2021/src/day_10.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{problem, Solution};
use common::Solution;

const CHARS: [(char, char); 4] = [('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')];

Expand All @@ -9,8 +9,8 @@ impl Solution for Day10 {
"Syntax Scoring"
}

fn part_a(&self) -> String {
let data = parse(problem::load(2021, 10));
fn part_a(&self, input: &str) -> String {
let data = parse(input);

let mut total = 0;
for i in data {
Expand All @@ -37,8 +37,8 @@ impl Solution for Day10 {
total.to_string()
}

fn part_b(&self) -> String {
let data = parse(problem::load(2021, 10));
fn part_b(&self, input: &str) -> String {
let data = parse(input);

let mut scores = Vec::new();
for i in data {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Solution for Day10 {
}
}

fn parse(lines: String) -> Vec<String> {
fn parse(lines: &str) -> Vec<String> {
lines
.lines()
.map(|x| x.to_string())
Expand Down
Loading

0 comments on commit e3688dd

Please sign in to comment.