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

2024 Let's go! #21

Merged
merged 24 commits into from
Dec 7, 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
13 changes: 9 additions & 4 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Run tests

on: [push]
on: [ push ]

jobs:
list-tests:
Expand All @@ -25,12 +25,14 @@ jobs:
- 'year_2019/**'
year_2023:
- 'year_2023/**'
year_2024:
- 'year_2024/**'
- id: set-matrix
run: echo "::set-output name=matrix::$(./list_test_combinations.sh ${{ join(fromJSON(steps.filter.outputs.changes), ' ') }})"

test:
runs-on: ubuntu-latest
needs: [list-tests]
needs: [ list-tests ]
if: ${{ needs.list-tests.outputs.matrix != '[]' && needs.list-tests.outputs.matrix != '' }}
strategy:
matrix:
Expand All @@ -49,7 +51,7 @@ jobs:
python -m pip install --upgrade pip virtualenv
python -m pip install -r requirements.txt
- name: Run Tests
if: ${{ matrix.language == 'python' && matrix.folder != 'none' }}
if: ${{ matrix.language == 'python'}}
run: python -m unittest discover -t . -s ${{ matrix.folder }} -p "*.py"

- name: Setup Rust
Expand All @@ -58,18 +60,21 @@ jobs:
with:
channel: ${{ matrix.version }}
- name: Build
if: ${{ matrix.language == 'rust' }}
working-directory: ${{ matrix.folder }}
run: cargo build
- name: Run tests
if: ${{ matrix.language == 'rust' }}
working-directory: ${{ matrix.folder }}
run: cargo test
- name: Run binary
if: ${{ matrix.language == 'rust' && matrix.folder != 'aoc_utils' }}
working-directory: ${{ matrix.folder }}
run: cargo run

all-tests:
runs-on: ubuntu-latest
needs: [list-tests, test]
needs: [ list-tests, test ]
if: always()
steps:
- name: Failed tests
Expand Down
7 changes: 7 additions & 0 deletions aoc_utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions aoc_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "aoc_utils"
version = "0.1.0"
edition = "2021"

[dependencies]
64 changes: 64 additions & 0 deletions aoc_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::collections::{HashMap, HashSet};
use std::fs::read_to_string;

pub fn read_lines(filename: &str) -> Vec<String> {
read_to_string(filename)
.unwrap()
.lines()
.map(String::from)
.collect()
}

#[test]
fn test_prime_factors() {
fn check(number: u32, factors: Vec<u32>) {
let result = prime_factors(&number);
assert_eq!(
result.iter().map(|(_n, &exponent)| exponent).sum::<u32>(),
factors.len() as u32
);

for (number, &exponent) in result.iter() {
let expected = factors.iter().filter(|&n| n == number).count() as u32;
assert_eq!(exponent, expected);
}
}

check(2, vec![2]);
check(3, vec![3]);
check(4, vec![2, 2]);
check(5, vec![5]);
check(6, vec![2, 3]);
check(7, vec![7]);
check(8, vec![2, 2, 2]);
check(9, vec![3, 3]);
}

pub fn prime_factors(number: &u32) -> HashMap<u32, u32> {
let mut result = HashMap::new();

let mut n = *number;
for i in primes(&n) {
while n % i == 0 {
n = &n / i;

let count = result.entry(i).or_insert(0);
*count += 1;
}
}
result
}

pub fn primes(number: &u32) -> impl Iterator<Item = u32> {
let mut sieve = HashSet::new();
let &n = number;

let upper_bound = (n as f64).sqrt().floor() as u32 + 1;
for x in 2..upper_bound {
for y in 2..(n / x) + 1 {
sieve.insert(x * y as u32);
}
}

(2..n + 1).filter(move |&i| !&sieve.contains(&i))
}
30 changes: 18 additions & 12 deletions list_test_combinations.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
#!/bin/bash

for folder in "${@}"; do
folder_clean=$(basename "$(realpath "${folder}")")
function gen_test_config() {
folder="$1"
language="$2"

if [ -e "${folder_clean}/__init__.py" ]; then
language="python"
elif [ -e "${folder_clean}/Cargo.toml" ]; then
language="rust"
else
continue
fi

version_file="${folder_clean}/${language}_versions.txt"
version_file="${folder}/${language}_versions.txt"
if [ ! -e "${version_file}" ]; then
version_file="default_${language}_versions.txt"
fi

versions=$(cat "${version_file}")

for version in ${versions}; do
echo "{\"folder\": \"${folder_clean}\", \"language\":\"${language}\", \"version\":\"${version}\"}"
echo "{\"folder\": \"${folder}\", \"language\":\"${language}\", \"version\":\"${version}\"}"
done
}

for folder in "${@}"; do
folder_clean=$(basename "$(realpath "${folder}")")

if [ -e "${folder_clean}/__init__.py" ]; then
gen_test_config "${folder_clean}" "python"
fi

if [ -e "${folder_clean}/Cargo.toml" ]; then
gen_test_config "${folder_clean}" "rust"
fi

done | jq -s -c
7 changes: 7 additions & 0 deletions year_2023/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions year_2023/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aoc_utils = { path = "../aoc_utils" }
2 changes: 1 addition & 1 deletion year_2023/src/day1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;

#[test]
fn test_mine() {
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day10/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::{Display, Formatter};

Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::cmp::max;
use std::cmp::min;
use std::collections::HashSet;
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day12/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashMap;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day13/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::fmt;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day14/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashMap;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day16/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashSet;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day17/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::cmp::min;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day18/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::{HashMap, HashSet};
use std::ops::Index;

Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day19/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashMap;
use std::fmt::Debug;

Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::cmp::max;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day20/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::{HashMap, HashSet, VecDeque};
use std::ops::Deref;

Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day21/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::{HashMap, HashSet};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day22/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::{HashMap, HashSet, VecDeque};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day23/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::{HashSet, VecDeque};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day24/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;

#[test]
fn test_mine() {
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day3/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashMap;
use std::ops::Index;

Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day4/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashSet;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day5/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::cmp::min;
use std::collections::HashSet;
use std::iter::zip;
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day6/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::iter::zip;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day7/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::ops::Index;
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day8/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::HashMap;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion year_2023/src/day9/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils;
use aoc_utils as utils;
use std::collections::VecDeque;

#[test]
Expand Down
Loading
Loading