Day 05 solution

This commit is contained in:
Natty 2023-12-05 20:29:01 +01:00
parent c79673ec86
commit 75ef97b6c2
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
35 changed files with 1837 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/.vscode
/*/dist-newstyle
/*/target
/.idea

9
day05a/.gitattributes vendored Normal file
View File

@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf
# These are Windows script files and should use crlf
*.bat text eol=crlf

3
day05a/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
.gradle
build

20
day05a/build.gradle.kts Normal file
View File

@ -0,0 +1,20 @@
plugins {
java
application
}
repositories {
mavenCentral()
}
tasks.withType<JavaExec> {
standardInput = System.`in`
}
application {
mainClass.set("sh.natty.aoc23.Main")
}
dependencies {
implementation("org.apache.commons:commons-lang3:3.14.0")
}

View File

@ -0,0 +1,10 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
[versions]
guava = "32.1.2-jre"
junit-jupiter = "5.10.0"
[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }

BIN
day05a/gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

249
day05a/gradlew vendored Executable file
View File

@ -0,0 +1,249 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

92
day05a/gradlew.bat vendored Normal file
View File

@ -0,0 +1,92 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

192
day05a/input.txt generated Normal file
View File

@ -0,0 +1,192 @@
seeds: 3429320627 235304036 1147330745 114559245 1684000747 468955901 677937579 96599505 1436970021 26560102 3886049334 159534901 936845926 25265009 3247146679 95841652 3696363517 45808572 2319065313 125950148
seed-to-soil map:
583826644 2288418886 120919689
2666741396 3172314277 160907737
416244021 605500997 167582623
779666561 2280573809 7845077
704746333 908146497 74920228
845411123 2565941729 61831565
1527751557 3025978089 146336188
2827649133 2012274036 268299773
259640867 2409338575 156603154
0 2766337222 259640867
787511638 983066725 57899485
907242688 1526828044 485445992
1674087745 237273108 368227889
2180879562 1040966210 485861834
1392688680 773083620 135062877
3095948906 0 237273108
2042315634 2627773294 138563928
soil-to-fertilizer map:
3793729374 3825015981 63222599
1438266078 0 258943930
1292079166 1479426911 146186912
2816531945 2822520060 385496901
1078023340 1741334425 98335224
3856951973 3668871521 111838140
4180611137 3904426682 114356159
0 1958275780 804927654
3968790113 3380257222 80572704
843553208 2801828988 20691072
1724422279 286156201 594958638
3616894031 4018782841 39547646
1697210008 258943930 27212271
3700747997 3472761445 92981377
4101643138 3565742822 50848378
4152491516 3460829926 11931519
976862296 1378265867 101161044
2319380917 881114839 497151028
3380257222 4058330487 236636809
3656441677 3780709661 44306320
864244280 1845657764 112618016
1176358564 1625613823 115720602
4164423035 3888238580 16188102
3202028846 1839669649 5988115
804927654 2763203434 38625554
4049362817 3616591200 52280321
fertilizer-to-water map:
3734704645 4081344261 116089008
474703780 198917265 194664963
1879970783 393582228 36617128
1197375949 1845422975 8989824
466658346 430199356 8045434
2310800010 2638298964 424967672
919557740 1398638397 135905600
3687134144 3790001806 1934146
3470663058 2339708475 216471086
1759740912 1126580170 64763562
896009775 1048686270 23547965
3348634097 3711378096 32073660
3091810489 2234799361 92321214
889058738 1119629133 6951037
2784472328 3743451756 46550050
2843610278 3063266636 231881310
1532834459 674183164 226906453
3273953517 2183514026 51285335
3075491588 4197433269 16318901
3325238852 3791935952 23395245
3689068290 3665741741 45636355
3384455721 3861402055 22006111
3406461832 3295147946 64201226
1412808810 438244790 32132927
3380707757 2556179561 3747964
3850793653 4001077593 80266668
1450046808 470377717 82787651
669368743 1626190154 219232821
1365871086 1072234235 46937724
3184131703 3969255894 11450375
1824504474 553165368 55466309
1206365773 1854412799 159505313
1055463340 906773661 141912609
3931060321 3359349172 306392569
2756139006 3883408166 28333322
193812194 608631677 65551487
2229584884 4213752170 81215126
3195582078 2559927525 78371439
259363681 1191343732 207294665
2183514026 3815331197 46070858
2735767682 3980706269 20371324
888601564 1119171959 457174
1916587911 901089617 5684044
1922271955 1534543997 91646157
4237452890 3911741488 57514406
2831022378 2327120575 12587900
1444941737 193812194 5105071
water-to-light map:
3241790649 0 474458786
1987249042 1535533387 1254541607
289948525 715361304 527138528
3716249435 1242499832 58349573
1051771035 3080023519 694575489
817087053 1300849405 234683982
1746346524 474458786 240902518
0 2790074994 289948525
light-to-temperature map:
2243197897 1858683458 54522139
694397455 637497541 323467072
3781060233 3489670799 513907063
2297720036 960964613 400594644
3448397921 3305645149 184025650
1096611912 137478840 356155107
2159128717 1774614278 84069180
1769470938 1913205597 389657779
1017864527 2302863376 78747385
3632423571 4003577862 148636662
281342434 1361559257 413055021
3393398302 4239967677 54999619
137478840 493633947 143863594
3305645149 4152214524 87753153
1452767019 2381610761 316703919
temperature-to-humidity map:
36297311 0 6725362
2219701249 4240213747 25288799
3578106372 1498215295 38253390
1356053404 1610598521 173674950
446166190 530183876 120299205
813026177 2579337380 225593633
133147960 367690092 61449909
223651565 500781338 29402538
253054103 174578005 193112087
194597869 6725362 29053696
566465395 85143164 1372917
799049686 3045085309 13976491
1529728354 3595799720 487441441
2420467399 2406340018 172679747
2593147146 1784273471 622066547
2244990048 3198005099 143889028
3437740738 4233433470 6780277
43022673 650483081 18483950
2407809136 1485557032 12658263
3215213693 1060498533 222527045
580905107 86516081 88061924
2017169795 1283025578 202531454
3710504883 2579019765 317615
61506623 429140001 71641337
3716287969 769584936 114202886
1328506806 3066097836 27546598
3703468847 3059061800 7036036
769584936 4265502546 29464750
1292525403 883787822 35981403
3710822498 919769225 5465471
3830490855 4083241161 150192309
3616359762 925234696 87109085
2388879076 3093644434 18930060
3529951620 1012343781 48154752
4220837460 1536468685 74129836
3444521015 3112574494 85430605
567838312 35779058 13066795
1038619810 3341894127 253905593
3980683164 2804931013 240154296
0 48845853 36297311
humidity-to-location map:
2609743610 4133079426 108193613
1608826026 1793129808 148682069
3749483646 1957417643 61460641
3216466252 4241273039 49689894
2717937223 1941811877 15605766
3810944287 1225630249 233181949
2124315534 3020458025 302014415
264620169 272336891 68907407
861861645 2458132363 209077985
4044126236 1458812198 250841060
3134115707 2375781818 82350545
1070939630 4290962933 4004363
2733542989 4089410242 43669184
1074943993 3479498485 533882033
2777212173 2018878284 356903534
2426329949 4013380518 76029724
333527576 0 7716722
3266156146 3346379827 130079823
2526267060 1709653258 83476550
1757508095 3476459650 3038835
0 7716722 264620169
1760546930 861861645 363768604
3396235969 2667210348 353247677
2502359673 3322472440 23907387

View File

@ -0,0 +1,66 @@
package sh.natty.aoc23;
import org.apache.commons.lang3.mutable.MutableLong;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
record Mapping(long destination, long length) {
}
public class Main {
public static void main(String[] args) {
var reader = new BufferedReader(new InputStreamReader(System.in));
var accum = new ArrayList<>(List.of(new ArrayList<String>()));
var lines = reader.lines()
.reduce(accum, (acc, val) -> {
if (val.isBlank())
acc.add(new ArrayList<>());
var last = acc.getLast();
last.add(val);
return acc;
}, (l, r) -> {
l.addAll(r);
return l;
});
var seeds = Arrays.stream(lines.removeFirst()
.getFirst()
.split(":")[1]
.trim()
.split("\\s+"))
.map(Long::parseLong)
.toList();
var maps = lines.stream().map(l -> new TreeMap<>(l.stream()
.filter(Predicate.not(String::isBlank))
.skip(1)
.map(a -> Arrays.stream(a.split(" "))
.mapToLong(Long::parseLong)
.toArray())
.collect(Collectors.toMap(k -> k[1], v -> new Mapping(v[0], v[2]))))
).toList();
var locations = seeds.stream()
.map(s ->
maps.stream()
.collect(() -> new MutableLong(s), (acc, v) -> {
var e = v.floorEntry(acc.getValue());
if (e == null)
return;
var val = e.getValue();
var offs = acc.getValue() - e.getKey();
if (offs > val.length())
return;
acc.setValue(val.destination() + offs);
}, MutableLong::setValue))
.map(MutableLong::getValue)
.toList();
System.out.println(locations);
System.out.println(locations.stream().min(Comparator.naturalOrder()));
}
}

33
day05a/test.txt generated Normal file
View File

@ -0,0 +1,33 @@
seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4

9
day05b/.gitattributes vendored Normal file
View File

@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf
# These are Windows script files and should use crlf
*.bat text eol=crlf

3
day05b/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
.gradle
build

20
day05b/build.gradle.kts Normal file
View File

@ -0,0 +1,20 @@
plugins {
java
application
}
repositories {
mavenCentral()
}
tasks.withType<JavaExec> {
standardInput = System.`in`
}
application {
mainClass.set("sh.natty.aoc23.Main")
}
dependencies {
implementation("org.apache.commons:commons-lang3:3.14.0")
}

View File

@ -0,0 +1,10 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
[versions]
guava = "32.1.2-jre"
junit-jupiter = "5.10.0"
[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }

BIN
day05b/gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

249
day05b/gradlew vendored Executable file
View File

@ -0,0 +1,249 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

92
day05b/gradlew.bat vendored Normal file
View File

@ -0,0 +1,92 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

192
day05b/input.txt generated Normal file
View File

@ -0,0 +1,192 @@
seeds: 3429320627 235304036 1147330745 114559245 1684000747 468955901 677937579 96599505 1436970021 26560102 3886049334 159534901 936845926 25265009 3247146679 95841652 3696363517 45808572 2319065313 125950148
seed-to-soil map:
583826644 2288418886 120919689
2666741396 3172314277 160907737
416244021 605500997 167582623
779666561 2280573809 7845077
704746333 908146497 74920228
845411123 2565941729 61831565
1527751557 3025978089 146336188
2827649133 2012274036 268299773
259640867 2409338575 156603154
0 2766337222 259640867
787511638 983066725 57899485
907242688 1526828044 485445992
1674087745 237273108 368227889
2180879562 1040966210 485861834
1392688680 773083620 135062877
3095948906 0 237273108
2042315634 2627773294 138563928
soil-to-fertilizer map:
3793729374 3825015981 63222599
1438266078 0 258943930
1292079166 1479426911 146186912
2816531945 2822520060 385496901
1078023340 1741334425 98335224
3856951973 3668871521 111838140
4180611137 3904426682 114356159
0 1958275780 804927654
3968790113 3380257222 80572704
843553208 2801828988 20691072
1724422279 286156201 594958638
3616894031 4018782841 39547646
1697210008 258943930 27212271
3700747997 3472761445 92981377
4101643138 3565742822 50848378
4152491516 3460829926 11931519
976862296 1378265867 101161044
2319380917 881114839 497151028
3380257222 4058330487 236636809
3656441677 3780709661 44306320
864244280 1845657764 112618016
1176358564 1625613823 115720602
4164423035 3888238580 16188102
3202028846 1839669649 5988115
804927654 2763203434 38625554
4049362817 3616591200 52280321
fertilizer-to-water map:
3734704645 4081344261 116089008
474703780 198917265 194664963
1879970783 393582228 36617128
1197375949 1845422975 8989824
466658346 430199356 8045434
2310800010 2638298964 424967672
919557740 1398638397 135905600
3687134144 3790001806 1934146
3470663058 2339708475 216471086
1759740912 1126580170 64763562
896009775 1048686270 23547965
3348634097 3711378096 32073660
3091810489 2234799361 92321214
889058738 1119629133 6951037
2784472328 3743451756 46550050
2843610278 3063266636 231881310
1532834459 674183164 226906453
3273953517 2183514026 51285335
3075491588 4197433269 16318901
3325238852 3791935952 23395245
3689068290 3665741741 45636355
3384455721 3861402055 22006111
3406461832 3295147946 64201226
1412808810 438244790 32132927
3380707757 2556179561 3747964
3850793653 4001077593 80266668
1450046808 470377717 82787651
669368743 1626190154 219232821
1365871086 1072234235 46937724
3184131703 3969255894 11450375
1824504474 553165368 55466309
1206365773 1854412799 159505313
1055463340 906773661 141912609
3931060321 3359349172 306392569
2756139006 3883408166 28333322
193812194 608631677 65551487
2229584884 4213752170 81215126
3195582078 2559927525 78371439
259363681 1191343732 207294665
2183514026 3815331197 46070858
2735767682 3980706269 20371324
888601564 1119171959 457174
1916587911 901089617 5684044
1922271955 1534543997 91646157
4237452890 3911741488 57514406
2831022378 2327120575 12587900
1444941737 193812194 5105071
water-to-light map:
3241790649 0 474458786
1987249042 1535533387 1254541607
289948525 715361304 527138528
3716249435 1242499832 58349573
1051771035 3080023519 694575489
817087053 1300849405 234683982
1746346524 474458786 240902518
0 2790074994 289948525
light-to-temperature map:
2243197897 1858683458 54522139
694397455 637497541 323467072
3781060233 3489670799 513907063
2297720036 960964613 400594644
3448397921 3305645149 184025650
1096611912 137478840 356155107
2159128717 1774614278 84069180
1769470938 1913205597 389657779
1017864527 2302863376 78747385
3632423571 4003577862 148636662
281342434 1361559257 413055021
3393398302 4239967677 54999619
137478840 493633947 143863594
3305645149 4152214524 87753153
1452767019 2381610761 316703919
temperature-to-humidity map:
36297311 0 6725362
2219701249 4240213747 25288799
3578106372 1498215295 38253390
1356053404 1610598521 173674950
446166190 530183876 120299205
813026177 2579337380 225593633
133147960 367690092 61449909
223651565 500781338 29402538
253054103 174578005 193112087
194597869 6725362 29053696
566465395 85143164 1372917
799049686 3045085309 13976491
1529728354 3595799720 487441441
2420467399 2406340018 172679747
2593147146 1784273471 622066547
2244990048 3198005099 143889028
3437740738 4233433470 6780277
43022673 650483081 18483950
2407809136 1485557032 12658263
3215213693 1060498533 222527045
580905107 86516081 88061924
2017169795 1283025578 202531454
3710504883 2579019765 317615
61506623 429140001 71641337
3716287969 769584936 114202886
1328506806 3066097836 27546598
3703468847 3059061800 7036036
769584936 4265502546 29464750
1292525403 883787822 35981403
3710822498 919769225 5465471
3830490855 4083241161 150192309
3616359762 925234696 87109085
2388879076 3093644434 18930060
3529951620 1012343781 48154752
4220837460 1536468685 74129836
3444521015 3112574494 85430605
567838312 35779058 13066795
1038619810 3341894127 253905593
3980683164 2804931013 240154296
0 48845853 36297311
humidity-to-location map:
2609743610 4133079426 108193613
1608826026 1793129808 148682069
3749483646 1957417643 61460641
3216466252 4241273039 49689894
2717937223 1941811877 15605766
3810944287 1225630249 233181949
2124315534 3020458025 302014415
264620169 272336891 68907407
861861645 2458132363 209077985
4044126236 1458812198 250841060
3134115707 2375781818 82350545
1070939630 4290962933 4004363
2733542989 4089410242 43669184
1074943993 3479498485 533882033
2777212173 2018878284 356903534
2426329949 4013380518 76029724
333527576 0 7716722
3266156146 3346379827 130079823
2526267060 1709653258 83476550
1757508095 3476459650 3038835
0 7716722 264620169
1760546930 861861645 363768604
3396235969 2667210348 353247677
2502359673 3322472440 23907387

View File

@ -0,0 +1,117 @@
package sh.natty.aoc23;
import org.apache.commons.lang3.NumberRange;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
record Mapping(long destination, long length) {
}
public static void main(String[] args) {
var reader = new BufferedReader(new InputStreamReader(System.in));
var accum = new ArrayList<>(List.of(new ArrayList<String>()));
var lines = reader.lines()
.reduce(accum, (acc, val) -> {
if (val.isBlank())
acc.add(new ArrayList<>());
var last = acc.getLast();
last.add(val);
return acc;
}, (l, r) -> {
l.addAll(r);
return l;
});
var seeds = Arrays.stream(lines.removeFirst()
.getFirst()
.split(":")[1]
.trim()
.split("\\s+"))
.map(Long::parseLong)
.toList();
var seedRanges = IntStream.range(0, seeds.size() / 2)
.mapToObj(i -> new NumberRange<>(seeds.get(i * 2), seeds.get(i * 2) + seeds.get(i * 2 + 1) - 1, null))
.toList();
var maps = lines.stream().map(l -> new TreeMap<>(l.stream()
.filter(Predicate.not(String::isBlank))
.skip(1)
.map(a -> Arrays.stream(a.split(" "))
.mapToLong(Long::parseLong)
.toArray())
.collect(Collectors.toMap(k -> k[1], v -> new Mapping(v[0], v[2]))))
).toList();
var ranges = seedRanges;
for (var map : maps) {
ranges = ranges.stream().flatMap(r -> {
var minKey = map.floorKey(r.getMinimum());
var maxKey = map.floorKey(r.getMaximum());
if (minKey == null || maxKey == null)
return Stream.of(r);
var sub = map.subMap(minKey, true, maxKey, true);
if (sub.isEmpty())
return Stream.of(r);
var newRanges = new ArrayList<NumberRange<Long>>();
var starts = new ArrayList<Long>();
starts.add(r.getMinimum());
var ends = new ArrayList<Long>();
for (var e : sub.sequencedEntrySet()) {
var k = e.getKey();
var v = e.getValue();
var l = v.length();
if (k + l < r.getMinimum())
continue;
var ir = new NumberRange<>(k, k + l - 1, null);
var offset = v.destination() - k;
var intersection = ir.intersectionWith(r);
newRanges.add(new NumberRange<>(intersection.getMinimum() + offset, intersection.getMaximum() + offset, null));
ends.add(k - 1);
starts.add(k + l);
}
ends.add(r.getMaximum());
var itStart = starts.iterator();
var itEnd = ends.iterator();
while (itStart.hasNext() && itEnd.hasNext()) {
var start = itStart.next();
var end = itEnd.next();
if (end < start)
continue;
var rr = new NumberRange<>(start, end, null);
if (r.isOverlappedBy(rr)) {
var intersection = rr.intersectionWith(r);
newRanges.add(new NumberRange<>(intersection.getMinimum(), intersection.getMaximum(), null));
}
}
return newRanges.stream();
}).toList();
}
System.out.println(ranges);
System.out.println(ranges.stream().map(NumberRange::getMinimum).min(Comparator.naturalOrder()));
}
}

33
day05b/test.txt generated Normal file
View File

@ -0,0 +1,33 @@
seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4

10
run.sh
View File

@ -20,7 +20,13 @@ fi
cd "$1"
case "$1" in
# Default solutions -- Haskell
*) stack run <input.txt
# Solutions -- Haskell
"day01a" | "day01b" | \
"day02a" | "day02b" | \
"day03a" | "day03b" | \
"day04a" | "day04b") stack run <input.txt
;;
# Default solutions -- Java
*) ./gradlew run <input.txt
;;
esac

9
template00_java/.gitattributes vendored Normal file
View File

@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf
# These are Windows script files and should use crlf
*.bat text eol=crlf

3
template00_java/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
.gradle
build

View File

@ -0,0 +1,16 @@
plugins {
java
application
}
repositories {
mavenCentral()
}
tasks.withType<JavaExec> {
standardInput = System.`in`
}
application {
mainClass.set("sh.natty.aoc23.Main")
}

View File

@ -0,0 +1,10 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
[versions]
guava = "32.1.2-jre"
junit-jupiter = "5.10.0"
[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }

Binary file not shown.

View File

@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

249
template00_java/gradlew vendored Executable file
View File

@ -0,0 +1,249 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

92
template00_java/gradlew.bat vendored Normal file
View File

@ -0,0 +1,92 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@ -0,0 +1,7 @@
package sh.natty.aoc23;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}

View File

@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package template00_java;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}

10
test.sh
View File

@ -20,7 +20,13 @@ fi
cd "$1"
case "$1" in
# Default solutions -- Haskell
*) stack run <test.txt
# Solutions -- Haskell
"day01a" | "day01b" | \
"day02a" | "day02b" | \
"day03a" | "day03b" | \
"day04a" | "day04b") stack run <test.txt
;;
# Default solutions -- Java
*) ./gradlew run <test.txt
;;
esac