diff --git a/2022/get.sh b/2022/get.sh deleted file mode 100755 index a1a1f37..0000000 --- a/2022/get.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -source ../.env -mkdir -p input/$1 -curl "https://adventofcode.com/2022/day/$1/input" -H "Cookie: session=${SESSION}" > input/$1/input diff --git a/2022/new.sh b/2022/new.sh new file mode 100755 index 0000000..0cf5c02 --- /dev/null +++ b/2022/new.sh @@ -0,0 +1,15 @@ +#!/bin/sh +day=$1 +type=$2 +default=$3 +test=$4 + +echo "Creating file from template..." +sed -e "s/DAY/$day/g" -e "s/TYPE/$type/" -e "s/DEFAULT/$default/" -e "s/TEST/$test/" ./template.rs > ./src/bin/day${day}.rs + +echo "Downloading input..." +source ../.env +mkdir -p input/$1 +curl -s "https://adventofcode.com/2022/day/$1/input" -H "Cookie: session=${SESSION}" > input/$1/input + +echo "Done!" diff --git a/2022/src/bin/day1.rs b/2022/src/bin/day1.rs index dcf3695..49df3b5 100644 --- a/2022/src/bin/day1.rs +++ b/2022/src/bin/day1.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use aoc::{Solver, Output}; +use aoc::Solver; // -- Runners -- fn main() -> Result<()> { @@ -12,41 +12,40 @@ mod tests { #[test] fn part1_test1() -> Result<()> { - Day::test(aoc::Part::ONE, "test-1", Output::Number(24000)) + Day::test(aoc::Part::ONE, "test-1", 24000) } #[test] fn part2_test1() -> Result<()> { - Day::test(aoc::Part::TWO, "test-1", Output::Number(45000)) + Day::test(aoc::Part::TWO, "test-1", 45000) } #[test] fn part1_solution() -> Result<()> { - Day::test(aoc::Part::ONE, "input", Output::Number(70116)) + Day::test(aoc::Part::ONE, "input", 70116) } #[test] fn part2_solution() -> Result<()> { - Day::test(aoc::Part::TWO, "input", Output::Number(206582)) + Day::test(aoc::Part::TWO, "input", 206582) } } // -- Solution -- pub struct Day; impl aoc::Solver for Day { + type Output = u32; fn day() -> u8 { 1 } - fn part1(input: &str) -> Output { - let result = input.split("\n\n") + fn part1(input: &str) -> Self::Output { + input.split("\n\n") .map(|elf| elf.lines() .flat_map(|snack| snack.parse::()) .sum()) .max() - .unwrap(); - - Output::Number(result) + .unwrap() } - fn part2(input: &str) -> Output { + fn part2(input: &str) -> Self::Output { let mut elfs: Vec = input.split("\n\n") .map(|elf| elf.lines() .flat_map(|snack| snack.parse::()) @@ -55,8 +54,6 @@ impl aoc::Solver for Day { elfs.sort_by(|a, b| b.cmp(a)); - let result = elfs.iter().take(3).sum(); - - Output::Number(result) + elfs.iter().take(3).sum() } } diff --git a/2022/src/bin/day2.rs b/2022/src/bin/day2.rs index 8afb61e..a6e24ee 100644 --- a/2022/src/bin/day2.rs +++ b/2022/src/bin/day2.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use aoc::{Solver, Output}; +use aoc::Solver; // -- Runners -- fn main() -> Result<()> { @@ -12,19 +12,19 @@ mod tests { #[test] fn part1_test1() -> Result<()> { - Day::test(aoc::Part::ONE, "test-1", Output::Number(15)) + Day::test(aoc::Part::ONE, "test-1", 15) } #[test] fn part2_test1() -> Result<()> { - Day::test(aoc::Part::TWO, "test-1", Output::Number(12)) + Day::test(aoc::Part::TWO, "test-1", 12) } #[test] fn part1_solution() -> Result<()> { - Day::test(aoc::Part::ONE, "input", Output::Number(14264)) + Day::test(aoc::Part::ONE, "input", 14264) } #[test] fn part2_solution() -> Result<()> { - Day::test(aoc::Part::TWO, "input", Output::Number(12382)) + Day::test(aoc::Part::TWO, "input", 12382) } } @@ -102,28 +102,25 @@ fn calc_score(sum: u32, (a, b): (Hand, Hand)) -> u32 { // -- Solution -- pub struct Day; impl aoc::Solver for Day { + type Output = u32; fn day() -> u8 { 2 } - fn part1(input: &str) -> Output { - let result = input.lines() + fn part1(input: &str) -> Self::Output { + input.lines() .filter_map(|round| round.split_once(" ")) .map(|(a, b)| (Hand::from(a), Hand::from(b))) - .fold(0, calc_score); - - Output::Number(result) + .fold(0, calc_score) } - fn part2(input: &str) -> Output { - let result = input.lines() + fn part2(input: &str) -> Self::Output { + input.lines() .filter_map(|round| round.split_once(" ")) .map(|(a, b)| { let opponent = Hand::from(a); (opponent, opponent.strategy(b)) }) - .fold(0, calc_score); - - Output::Number(result) + .fold(0, calc_score) } } diff --git a/2022/src/bin/day3.rs b/2022/src/bin/day3.rs index c0dd9e4..7d810dd 100644 --- a/2022/src/bin/day3.rs +++ b/2022/src/bin/day3.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use aoc::{Solver, Output}; +use aoc::Solver; // -- Runners -- fn main() -> Result<()> { @@ -12,19 +12,19 @@ mod tests { #[test] fn part1_test1() -> Result<()> { - Day::test(aoc::Part::ONE, "test-1", Output::Number(157)) + Day::test(aoc::Part::ONE, "test-1", 157) } #[test] fn part2_test1() -> Result<()> { - Day::test(aoc::Part::TWO, "test-1", Output::Number(70)) + Day::test(aoc::Part::TWO, "test-1", 70) } #[test] fn part1_solution() -> Result<()> { - Day::test(aoc::Part::ONE, "input", Output::Number(8298)) + Day::test(aoc::Part::ONE, "input", 8298) } #[test] fn part2_solution() -> Result<()> { - Day::test(aoc::Part::TWO, "input", Output::Number(2708)) + Day::test(aoc::Part::TWO, "input", 2708) } } @@ -56,12 +56,13 @@ fn find_common(group: &[&str]) -> char { // -- Solution -- pub struct Day; impl aoc::Solver for Day { + type Output = u32; fn day() -> u8 { 3 } - fn part1(input: &str) -> Output { - let result = input.lines() + fn part1(input: &str) -> Self::Output { + input.lines() .map(|line| line.split_at(line.len()/2)) .map(|(a, b)| { for c in a.chars() { @@ -73,19 +74,15 @@ impl aoc::Solver for Day { unreachable!("No characters in common, this should never happen") }) .map(convert) - .sum(); - - Output::Number(result) + .sum() } - fn part2(input: &str) -> Output { - let result = input.lines() + fn part2(input: &str) -> Self::Output { + input.lines() .collect::>() .chunks(3) .map(find_common) .map(convert) - .sum(); - - Output::Number(result) + .sum() } } diff --git a/2022/src/bin/day4.rs b/2022/src/bin/day4.rs index 57a4cf8..35cd4d8 100644 --- a/2022/src/bin/day4.rs +++ b/2022/src/bin/day4.rs @@ -1,6 +1,6 @@ use std::cmp; use anyhow::Result; -use aoc::{Solver, Output}; +use aoc::Solver; // -- Runners -- fn main() -> Result<()> { @@ -13,19 +13,19 @@ mod tests { #[test] fn part1_test1() -> Result<()> { - Day::test(aoc::Part::ONE, "test-1", Output::Number(2)) + Day::test(aoc::Part::ONE, "test-1", 2) } #[test] fn part2_test1() -> Result<()> { - Day::test(aoc::Part::TWO, "test-1", Output::Number(4)) + Day::test(aoc::Part::TWO, "test-1", 4) } #[test] fn part1_solution() -> Result<()> { - Day::test(aoc::Part::ONE, "input", Output::Number(567)) + Day::test(aoc::Part::ONE, "input", 567) } #[test] fn part2_solution() -> Result<()> { - Day::test(aoc::Part::TWO, "input", Output::Number(907)) + Day::test(aoc::Part::TWO, "input", 907) } } @@ -72,27 +72,24 @@ fn transform(s: &str) -> (Elf, Elf) { // -- Solution -- pub struct Day; impl aoc::Solver for Day { + type Output = u32; fn day() -> u8 { 4 } - fn part1(input: &str) -> Output { - let result = input + fn part1(input: &str) -> Self::Output { + input .lines() .map(transform) .filter(contains) - .count() as u32; - - Output::Number(result) + .count() as u32 } - fn part2(input: &str) -> Output { - let result = input + fn part2(input: &str) -> Self::Output { + input .lines() .map(transform) .filter(overlaps) - .count() as u32; - - Output::Number(result) + .count() as u32 } } diff --git a/2022/src/bin/day5.rs b/2022/src/bin/day5.rs index 0acc963..00eaacc 100644 --- a/2022/src/bin/day5.rs +++ b/2022/src/bin/day5.rs @@ -4,7 +4,7 @@ use lazy_static::lazy_static; use regex::Regex; use anyhow::Result; -use aoc::{Solver, Output}; +use aoc::Solver; // -- Runners -- fn main() -> Result<()> { @@ -17,19 +17,19 @@ mod tests { #[test] fn part1_test1() -> Result<()> { - Day::test(aoc::Part::ONE, "test-1", Output::String("CMZ".to_string())) + Day::test(aoc::Part::ONE, "test-1", "CMZ".to_string()) } #[test] fn part2_test1() -> Result<()> { - Day::test(aoc::Part::TWO, "test-1", Output::String("MCD".to_string())) + Day::test(aoc::Part::TWO, "test-1", "MCD".to_string()) } #[test] fn part1_solution() -> Result<()> { - Day::test(aoc::Part::ONE, "input", Output::String("RNZLFZSJH".to_string())) + Day::test(aoc::Part::ONE, "input", "RNZLFZSJH".to_string()) } #[test] fn part2_solution() -> Result<()> { - Day::test(aoc::Part::TWO, "input", Output::String("CNSFCGJSM".to_string())) + Day::test(aoc::Part::TWO, "input", "CNSFCGJSM".to_string()) } } @@ -123,7 +123,7 @@ fn parse_instruction(s: &str) -> (usize, usize, usize) { (amount, from, to) } -fn solution(input: &str, reverse: bool) -> Output { +fn solution(input: &str, reverse: bool) -> String { // The current layout description ends with an empty line let mut boat = Boat::new(&input .lines() @@ -145,21 +145,22 @@ fn solution(input: &str, reverse: bool) -> Output { boat.put(to, taken); }); - Output::String(boat.to_string()) + boat.to_string() } // -- Solution -- pub struct Day; impl aoc::Solver for Day { + type Output = String; fn day() -> u8 { 5 } - fn part1(input: &str) -> Output { + fn part1(input: &str) -> Self::Output { solution(input, true) } - fn part2(input: &str) -> Output { + fn part2(input: &str) -> Self::Output { solution(input, false) } } diff --git a/2022/src/lib.rs b/2022/src/lib.rs index 33c75b3..be75821 100644 --- a/2022/src/lib.rs +++ b/2022/src/lib.rs @@ -1,5 +1,5 @@ use core::fmt; -use std::fs; +use std::{fs, fmt::Debug}; use anyhow::{Context, Result}; @@ -8,30 +8,14 @@ pub enum Part { TWO } -#[derive(Debug,PartialEq, PartialOrd)] -pub enum Output { - Number(u32), - String(String), -} - -impl fmt::Display for Output { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Output::Number(i) => write!(f, "{}", i)?, - Output::String(s) => write!(f, "{}", s)?, - _ => panic!("fmt::Display not implemented") - } - - Ok(()) - } -} - pub trait Solver { - fn day() -> u8; - fn part1(input: &str) -> Output; - fn part2(input: &str) -> Output; + type Output: fmt::Display + Debug + PartialEq; - fn test(part: Part, name: &str, result: Output) -> Result<()> { + fn day() -> u8; + fn part1(input: &str) -> Self::Output; + fn part2(input: &str) -> Self::Output; + + fn test(part: Part, name: &str, result: Self::Output) -> Result<()> { // Select the right function let fun = match part { Part::ONE => Self::part1, diff --git a/2022/template.rs b/2022/template.rs index 5ecb4ac..f937e62 100644 --- a/2022/template.rs +++ b/2022/template.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use aoc::{Solver, Output}; +use aoc::Solver; // -- Runners -- fn main() -> Result<()> { @@ -12,22 +12,23 @@ mod tests { #[test] fn part1_test1() -> Result<()> { - Day::test(aoc::Part::ONE, "test-1", Output::Number(0)) + Day::test(aoc::Part::ONE, "test-1", TEST) } } // -- Solution -- pub struct Day; impl aoc::Solver for Day { + type Output = TYPE; fn day() -> u8 { - todo!("Day not set") + DAY } - fn part1(input: &str) -> Output { - Output::Number(0) + fn part1(input: &str) -> Self::Output { + DEFAULT } - fn part2(input: &str) -> Output { - Output::Number(0) + fn part2(input: &str) -> Self::Output { + DEFAULT } }