Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update daily solution definition format #3

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
884 changes: 569 additions & 315 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ name = "advent_of_code"
version = "0.1.0"

[workspace]
members = ["scaffold", "common", "aoc_lib", "aoc_2021", "aoc_2022", "aoc_2023"]
members = [
"scaffold",
"common",
"aoc_lib",
"aoc_2021",
"aoc_2022",
"aoc_2023",
"aoc_2024",
]

[dependencies]
common = { path = "common" }
aoc_2021 = { path = "aoc_2021" }
aoc_2022 = { path = "aoc_2022" }
aoc_2023 = { path = "aoc_2023" }
aoc_2024 = { path = "aoc_2024" }

clap = { version = "4.0.29", features = ["derive"] }
chrono = "0.4.31"
Expand Down
36 changes: 15 additions & 21 deletions aoc_2021/src/day_01.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
use common::{Answer, Solution};
use common::{solution, Answer};

pub struct Day01;
solution!("Sonar Sweep", 1);

impl Solution for Day01 {
fn name(&self) -> &'static str {
"Sonar Sweep"
}
fn part_a(input: &str) -> Answer {
let data = input
.lines()
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>();

fn part_a(&self, input: &str) -> Answer {
let data = input
.lines()
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>();

data.windows(2).filter(|x| x[0] < x[1]).count().into()
}
data.windows(2).filter(|x| x[0] < x[1]).count().into()
}

fn part_b(&self, input: &str) -> Answer {
let d = input
.lines()
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>();
fn part_b(input: &str) -> Answer {
let d = input
.lines()
.map(|x| x.parse::<u32>().unwrap())
.collect::<Vec<u32>>();

d.windows(4).filter(|x| x[2] > x[0]).count().into()
}
d.windows(4).filter(|x| x[2] > x[0]).count().into()
}
74 changes: 34 additions & 40 deletions aoc_2021/src/day_02.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
use common::{Answer, Solution};
use common::{solution, Answer};

pub struct Day02;
solution!("Dive!", 2);

impl Solution for Day02 {
fn name(&self) -> &'static str {
"Dive!"
}

fn part_a(&self, input: &str) -> Answer {
let mut dep: u32 = 0;
let mut hor: u32 = 0;
fn part_a(input: &str) -> Answer {
let mut dep: u32 = 0;
let mut hor: u32 = 0;

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

match seg[0] {
"forward" => hor += x,
"up" => dep -= x,
"down" => dep += x,
_ => {}
}
match seg[0] {
"forward" => hor += x,
"up" => dep -= x,
"down" => dep += x,
_ => {}
}

(dep * hor).into()
}

fn part_b(&self, input: &str) -> Answer {
let mut dep: u32 = 0;
let mut hor: u32 = 0;
let mut aim: u32 = 0;

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

match seg[0] {
"forward" => {
hor += x;
dep += aim * x;
}
"up" => aim -= x,
"down" => aim += x,
_ => {}
(dep * hor).into()
}

fn part_b(input: &str) -> Answer {
let mut dep: u32 = 0;
let mut hor: u32 = 0;
let mut aim: u32 = 0;

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

match seg[0] {
"forward" => {
hor += x;
dep += aim * x;
}
"up" => aim -= x,
"down" => aim += x,
_ => {}
}

(dep * hor).into()
}

(dep * hor).into()
}
100 changes: 47 additions & 53 deletions aoc_2021/src/day_03.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,67 @@
use common::{Answer, Solution};
use common::{solution, Answer};

pub struct Day03;
solution!("Binary Diagnostic", 3);

impl Solution for Day03 {
fn name(&self) -> &'static str {
"Binary Diagnostic"
}
fn part_a(input: &str) -> Answer {
let num_len = input.lines().next().unwrap().len();

fn part_a(&self, input: &str) -> Answer {
let num_len = input.lines().next().unwrap().len();
let mut gamma = vec![0; num_len];
let mut epsilon = vec![1; num_len];

let mut gamma = vec![0; num_len];
let mut epsilon = vec![1; num_len];
for i in 0..num_len {
let mut z = 0;
let mut o = 0;

for i in 0..num_len {
let mut z = 0;
let mut o = 0;
input.lines().for_each(|j| match j.chars().nth(i).unwrap() {
'0' => z += 1,
'1' => o += 1,
_ => {}
});

input.lines().for_each(|j| match j.chars().nth(i).unwrap() {
'0' => z += 1,
'1' => o += 1,
_ => {}
});

if o > z {
epsilon[i] = 0;
gamma[i] = 1;
}
if o > z {
epsilon[i] = 0;
gamma[i] = 1;
}

let gamma = int_from_bin(&gamma).unwrap();
let epsilon = int_from_bin(&epsilon).unwrap();

(epsilon * gamma).into()
}

fn part_b(&self, input: &str) -> Answer {
let num_len = input.lines().next().unwrap().len();

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

let mut co2_keep = oxygen_keep.clone();
let mut co2_raw = oxygen_raw.clone();
let mut co2_scrub = 0;
(epsilon * gamma).into()
}

for i in 0..num_len {
// Filter Oxygen
let imax = get_imax(&oxygen_raw, i);
oxygen_raw = gen_raw(oxygen_raw, num_len, &oxygen_keep);
oxygen_keep.retain(|x| x.chars().nth(i).unwrap() == imax);
fn part_b(input: &str) -> Answer {
let num_len = input.lines().next().unwrap().len();

// Filter Co2
let imax = get_imax(&co2_raw, i);
co2_raw = gen_raw(co2_raw, num_len, &co2_keep);
co2_keep.retain(|x| x.chars().nth(i).unwrap() != imax);
let mut oxygen_keep = input.lines().collect::<Vec<&str>>();
let mut oxygen_raw = vec![[0, 0]; num_len];
let mut oxygen_gen = 0;

if oxygen_keep.len() == 1 {
oxygen_gen = isize::from_str_radix(oxygen_keep.first().unwrap(), 2).unwrap();
}
let mut co2_keep = oxygen_keep.clone();
let mut co2_raw = oxygen_raw.clone();
let mut co2_scrub = 0;

if co2_keep.len() == 1 {
co2_scrub = isize::from_str_radix(co2_keep.first().unwrap(), 2).unwrap();
}
for i in 0..num_len {
// Filter Oxygen
let imax = get_imax(&oxygen_raw, i);
oxygen_raw = gen_raw(oxygen_raw, num_len, &oxygen_keep);
oxygen_keep.retain(|x| x.chars().nth(i).unwrap() == imax);

// Filter Co2
let imax = get_imax(&co2_raw, i);
co2_raw = gen_raw(co2_raw, num_len, &co2_keep);
co2_keep.retain(|x| x.chars().nth(i).unwrap() != imax);

if oxygen_keep.len() == 1 {
oxygen_gen = isize::from_str_radix(oxygen_keep.first().unwrap(), 2).unwrap();
}

(oxygen_gen * co2_scrub).into()
if co2_keep.len() == 1 {
co2_scrub = isize::from_str_radix(co2_keep.first().unwrap(), 2).unwrap();
}
}

(oxygen_gen * co2_scrub).into()
}

fn int_from_bin(inp: &[u32]) -> Option<usize> {
Expand Down
30 changes: 12 additions & 18 deletions aoc_2021/src/day_04.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
use common::{Answer, Solution};
use common::{solution, Answer};

pub struct Day04;
solution!("Giant Squid", 4);

impl Solution for Day04 {
fn name(&self) -> &'static str {
"Giant Squid"
}

fn part_a(&self, input: &str) -> Answer {
let bingo = Bingo::parse_input(input);
let winning = bingo.solve();
fn part_a(input: &str) -> Answer {
let bingo = Bingo::parse_input(input);
let winning = bingo.solve();

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

fn part_b(&self, input: &str) -> Answer {
let bingo = Bingo::parse_input(input);
let loseing = bingo.loseing_solve();
fn part_b(input: &str) -> Answer {
let bingo = Bingo::parse_input(input);
let losing = bingo.losing_solve();

loseing.0[loseing.1].final_out(loseing.2).into()
}
losing.0[losing.1].final_out(losing.2).into()
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -73,7 +67,7 @@ impl Bingo {
}
}

fn loseing_solve(self) -> (Vec<Board>, usize, u32) {
fn losing_solve(self) -> (Vec<Board>, usize, u32) {
let mut nums = self.numbers.clone();
let mut tick = self.boards;
let mut take = self.take;
Expand Down
20 changes: 7 additions & 13 deletions aoc_2021/src/day_05.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
use common::{Answer, Solution};
use common::{solution, Answer};

use hashbrown::HashMap;

pub struct Day05;
solution!("Hydrothermal Venture", 5);

impl Solution for Day05 {
fn name(&self) -> &'static str {
"Hydrothermal Venture"
}

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

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

/// dig -> Weather to include Diagonal Lines
Expand Down
24 changes: 9 additions & 15 deletions aoc_2021/src/day_06.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
use common::{Answer, Solution};
use common::{solution, Answer};

use std::hash::Hash;

use hashbrown::HashMap;

pub struct Day06;
solution!("Lanternfish", 6);

impl Solution for Day06 {
fn name(&self) -> &'static str {
"Lanternfish"
}

fn part_a(&self, input: &str) -> Answer {
let data = Fish::parse_inp(input);
Fish::sim(data, 80).into()
}
fn part_a(input: &str) -> Answer {
let data = Fish::parse_inp(input);
Fish::sim(data, 80).into()
}

fn part_b(&self, input: &str) -> Answer {
let data = Fish::parse_inp(input);
Fish::sim(data, 256).into()
}
fn part_b(input: &str) -> Answer {
let data = Fish::parse_inp(input);
Fish::sim(data, 256).into()
}

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
Expand Down
Loading
Loading