Skip to content

Commit

Permalink
Added utility script to create template Java files and retrieve actua…
Browse files Browse the repository at this point in the history
…l input for an AOC problem
  • Loading branch information
zodac committed Dec 3, 2023
1 parent c9c6235 commit a61f036
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
58 changes: 58 additions & 0 deletions create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
###################################
# Basic script used to download an
# input file for an Advent Of Code
# problem day, and create the Java
# files from templates.
###################################

if [ "$#" -ne 2 ]; then
echo "Expected two inputs, the year and the day"
fi

year="${1}"
day="${2}"

if [[ ${day} -lt 10 ]]; then
day_long="0${day: -1}"
else
day_long="${day}"
fi

echo "Creating template for ${year}, Day ${day_long}:"

if [[ ! -d "./${year}" ]]; then
echo -e "\tNew year, please create directory structure"
exit 1
fi

echo -e "\t- Creating Java source file"
cp ./templates/Day.java "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
sed -i -e "s|%YEAR%|${year}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
sed -i -e "s|%DAY%|${day}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
sed -i -e "s|%DAY_LONG%|${day_long}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"

echo -e "\t- Creating Java test file"
cp ./templates/DayTest.java "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
sed -i -e "s|%YEAR%|${year}|g" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
sed -i -e "s|%DAY%|${day}|g" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"
sed -i -e "s|%DAY_LONG%|${day_long}|g" "./${year}/src/test/java/me/zodac/advent/Day${day_long}Test.java"

echo -e "\t- Creating example input file"
touch "./${year}/src/test/resources/day${day_long}.txt"

echo -e "\t- Creating actual input file"
if [[ -z "${AOC_COOKIE}" ]]; then
echo -e "\t\tNo cookie set for AOC, cannot create actual input file"
fi

curl --silent --header "Cookie: session=${AOC_COOKIE}" "https://adventofcode.com/${year}/day/${day}/input" > "./advent-of-code-inputs/${year}/day${day_long}.txt"
cd ./advent-of-code-inputs || exit 1
git add "${year}/day${day_long}.txt"
git commit -m "Adding input for ${year}, Day ${day}"
git push
cd .. || exit 1

echo -e "\t- Retrieving title"
title=$(curl --silent "https://adventofcode.com/${year}/day/${day}" | grep '<h2>' | awk '{split($0,a,"<h2>")} END{print a[2]}' | awk '{split($0,a,"</h2>")} END{print a[1]}' | cut -d ':' -f2 | cut -d '-' -f1 | awk '{$1=$1;print}')
sed -i -e "s|%TITLE%|${title}|g" "./${year}/src/main/java/me/zodac/advent/Day${day_long}.java"
52 changes: 52 additions & 0 deletions templates/Day.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* BSD Zero Clause License
*
* Copyright (c) 2021-2023 zodac.me
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package me.zodac.advent;

import java.util.Collection;

/**
* Solution for %YEAR%, Day %DAY%.
*
* @see <a href="https://adventofcode.com/%YEAR%/day/%DAY%">[%YEAR%: %DAY_LONG%] %TITLE%</a>
*/
public final class Day%DAY_LONG% {

private Day%DAY_LONG%() {

}

/**
* Part 1.
*
* @param values the input values
* @return the part 1 result
*/
public static long part1(final Collection<String> values) {
return 0L;
}

/**
* Part 2.
*
* @param values the input values
* @return the part 2 result
*/
public static long part2(final Collection<String> values) {
return 0L;
}
}
64 changes: 64 additions & 0 deletions templates/DayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* BSD Zero Clause License
*
* Copyright (c) 2021-2023 zodac.me
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package me.zodac.advent;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import me.zodac.advent.input.ExampleInput;
import me.zodac.advent.input.PuzzleInput;
import org.junit.jupiter.api.Test;

/**
* Tests to verify answers for {@link Day%DAY_LONG%}.
*/
public class Day%DAY_LONG%Test {

private static final String INPUT_FILENAME = "day%DAY_LONG%.txt";

@Test
void example() {
final List<String> values = ExampleInput.readLines(INPUT_FILENAME);

final long part1Result = Day%DAY_LONG%.part1(values);
assertThat(part1Result)
.isEqualTo(0L);

final long part2Result = Day%DAY_LONG%.part2(values);
assertThat(part2Result)
.isEqualTo(0L);
}

@Test
void part1() {
final List<String> values = PuzzleInput.readLines(INPUT_FILENAME);

final long part1Result = Day%DAY_LONG%.part1(values);
assertThat(part1Result)
.isEqualTo(0L);
}

@Test
void part2() {
final List<String> values = PuzzleInput.readLines(INPUT_FILENAME);

final long part2Result = Day%DAY_LONG%.part2(values);
assertThat(part2Result)
.isEqualTo(0L);
}
}

0 comments on commit a61f036

Please sign in to comment.