Question 10 and 11
This commit is contained in:
1 parent
6e61950ee2
commit
7ab01bca76
7 files changed
+406
No files matched your search
@@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.all-dressed-programming</groupId>
|
||||||
|
<artifactId>advent-of-code-2024</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
|
<maven.compiler.target>23</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.alldressedprogramming;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.alldressedprogramming.question10;
|
||||||
|
|
||||||
|
public class Inputs {
|
||||||
|
public static final String MOCK_INPUT ="89010123\n"
|
||||||
|
+ "78121874\n"
|
||||||
|
+ "87430965\n"
|
||||||
|
+ "96549874\n"
|
||||||
|
+ "45678903\n"
|
||||||
|
+ "32019012\n"
|
||||||
|
+ "01329801\n"
|
||||||
|
+ "10456732";
|
||||||
|
|
||||||
|
|
||||||
|
public static final String REAL_INPUT ="78760345801298896321001230110976301870134341034545687\n"
|
||||||
|
+ "69671236989367765410678345221885432963233452127632796\n"
|
||||||
|
+ "10589987670456001323569896336797010154324569498101845\n"
|
||||||
|
+ "23478678561032132124428987445788923245013478545613932\n"
|
||||||
|
+ "12034569432983045034310176596657654356932103456754301\n"
|
||||||
|
+ "06123765301874126965223456780345543267843212769861212\n"
|
||||||
|
+ "67610854322965437876104320691275676189732301890770023\n"
|
||||||
|
+ "98545921013456946766782111234987089078721450321981167\n"
|
||||||
|
+ "23434530145697854325693003435672128569670965492843298\n"
|
||||||
|
+ "10127647654787765014234512345965432154589876782194587\n"
|
||||||
|
+ "01018758943439890143109671236878941003432106543087656\n"
|
||||||
|
+ "10129667872108743232198780987567650012343267890989943\n"
|
||||||
|
+ "78734531231065654651015697898232109871054154901077830\n"
|
||||||
|
+ "69632340345676544543234506701145696780567063212566321\n"
|
||||||
|
+ "12541256543989035672103215012098785991278343103455410\n"
|
||||||
|
+ "01230167612108120780043454123210034876369752101769511\n"
|
||||||
|
+ "43456898909817431891012763204987121765459861019878700\n"
|
||||||
|
+ "32197899768746532982367874215672100894312772923467011\n"
|
||||||
|
+ "44089432897655673675498985365523456783203689876554322\n"
|
||||||
|
+ "43476521784554984566780189474310327894104501298458943\n"
|
||||||
|
+ "32562340603443295432110276589210016765067898347457654\n"
|
||||||
|
+ "41001456512782106798023345341232108778754106756346655\n"
|
||||||
|
+ "58989867425691065897654301250343419689603245891258743\n"
|
||||||
|
+ "67670198304781478654761298765430589576512231230969512\n"
|
||||||
|
+ "50501078213210569763890181056521677433443100945872402\n"
|
||||||
|
+ "41432189362387659812083212347893478922109811876721321\n"
|
||||||
|
+ "32343276651096543202144307438982560013078320125630430\n"
|
||||||
|
+ "21054565789823430143435698729861011224565410432145561\n"
|
||||||
|
+ "78769834658712894354998705610154398348901565589006776\n"
|
||||||
|
+ "69876765541006765267888214567863287657652651671217889\n"
|
||||||
|
+ "56945454532214890122379823098978101098343780980368923\n"
|
||||||
|
+ "47834503654323012001456702112109780123210891276456914\n"
|
||||||
|
+ "32322112567410163100014511003498898194541201345567805\n"
|
||||||
|
+ "01012012398789654011723621014586767087687632436679876\n"
|
||||||
|
+ "01201034498218765327898734765675432101598543567987015\n"
|
||||||
|
+ "32789125567309014456783349898987013216967845678872126\n"
|
||||||
|
+ "45694876323418123242124456787016524567856934569543034\n"
|
||||||
|
+ "34543945410567032103023456689145434787643021078676545\n"
|
||||||
|
+ "67432876509878749854510965670236543694532122189089496\n"
|
||||||
|
+ "78761789034569858987627874321987231243231821672102387\n"
|
||||||
|
+ "89450321123988567546596765345698100340100910565201256\n"
|
||||||
|
+ "44321450210676875654587034276765657654327823434389855\n"
|
||||||
|
+ "32340565012363966703456123189894398789016434325670765\n"
|
||||||
|
+ "01051876765454567812890012007654289430145045410581678\n"
|
||||||
|
+ "98762998898323234906721063218723176521032156921892569\n"
|
||||||
|
+ "45101237012210105415430874349812067898149867865433454\n"
|
||||||
|
+ "36788946543101076010987965456701098778928766456521043\n"
|
||||||
|
+ "29897654218760187623875478309898105667810610347876542\n"
|
||||||
|
+ "10898743009451296874566329214321234354320521298985031\n"
|
||||||
|
+ "01734542112321345987017812105670985012131494567874120\n"
|
||||||
|
+ "45629610321030996506323901078987876543012383456923454\n"
|
||||||
|
+ "30018723478945887215489765212985432692105672321012765\n"
|
||||||
|
+ "21167634569876774345677894303876501783234561030109876";
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package com.alldressedprogramming.question10;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public record Point(int height,
|
||||||
|
int x,
|
||||||
|
int y) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ParkMap {
|
||||||
|
|
||||||
|
final Map<Integer, Map<Integer, Point>> data;
|
||||||
|
|
||||||
|
public ParkMap(Map<Integer, Map<Integer, Point>> data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ParkMap create(String input) {
|
||||||
|
var rows = input.split("\n");
|
||||||
|
var data = new HashMap<Integer, Map<Integer, Point>>();
|
||||||
|
var y = 0;
|
||||||
|
for (var row : rows) {
|
||||||
|
var x = 0;
|
||||||
|
for (var col : row.split("")) {
|
||||||
|
var point = new Point(".".equals(col) ? -1 : Integer.parseInt(col), x, y);
|
||||||
|
var m = data.computeIfAbsent(x, (key) -> new HashMap<Integer, Point>());
|
||||||
|
m.put(y, point);
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
return new ParkMap(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Point> get(int x, int y ){
|
||||||
|
if(!data.containsKey(x)){
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
return Optional.ofNullable(data.get(x).get(y));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Point> getDirectNeighbours(int x, int y){
|
||||||
|
var ret = new ArrayList<Point>(4);
|
||||||
|
get(x + 1, y ).ifPresent(ret::add);
|
||||||
|
get(x - 1, y ).ifPresent(ret::add);
|
||||||
|
get(x , y + 1).ifPresent(ret::add);
|
||||||
|
get(x, y -1).ifPresent(ret::add);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
public List<Point> getDirectNeighbours(Point p){
|
||||||
|
return getDirectNeighbours(p.x(), p.y());
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Point> addPointToTrail(List<Point> trail, Point point){
|
||||||
|
var ret = new ArrayList<>(trail);
|
||||||
|
ret.addLast(point);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<List<Point>> continueTrail(List<Point> trail){
|
||||||
|
var lastPoint = trail.getLast();
|
||||||
|
return getDirectNeighbours(lastPoint).stream()
|
||||||
|
.filter(p -> p.height() == lastPoint.height() + 1)
|
||||||
|
.map(newLastPoint -> addPointToTrail(trail, newLastPoint));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<List<Point>> continueTrails(List<List<Point>> trails){
|
||||||
|
return trails.stream().flatMap(this::continueTrail).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<List<Point>> findAllTrails(Point p){
|
||||||
|
if(p.height() != 0){
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
List<List<Point>> ret = List.of(List.of(p));
|
||||||
|
while(!ret.isEmpty() && ret.getFirst().size() != 10){
|
||||||
|
ret = continueTrails(ret);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<List<Point>> findAllTrails(int x, int y){
|
||||||
|
return get(x, y).map(this::findAllTrails).orElse(List.of());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Stream<Point> getAllPoints(){
|
||||||
|
return data.values().stream().flatMap(x -> x.values().stream());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long score(List<List<Point>> trails){
|
||||||
|
return trails.stream().map(List::getLast).distinct().count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long question10A(String input){
|
||||||
|
var parkMap = ParkMap.create(input);
|
||||||
|
return parkMap.getAllPoints().map(parkMap::findAllTrails).mapToLong(Main::score).sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long question10B(String input){
|
||||||
|
var parkMap = ParkMap.create(input);
|
||||||
|
return parkMap.getAllPoints().map(parkMap::findAllTrails).mapToLong(List::size).sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
System.out.println("Answer to question 10 A is " + question10A(Inputs.REAL_INPUT));
|
||||||
|
System.out.println("Answer to question 10 B is " + question10B(Inputs.REAL_INPUT));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.alldressedprogramming.question11;
|
||||||
|
|
||||||
|
public class Inputs {
|
||||||
|
|
||||||
|
public static final String MOCK_INPUT = "125 17";
|
||||||
|
public static final String REAL_INPUT = "872027 227 18 9760 0 4 67716 9245696";
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package com.alldressedprogramming.question11;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.groupingBy;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
|
||||||
|
public static class Rock{
|
||||||
|
|
||||||
|
private final long value;
|
||||||
|
|
||||||
|
public Rock(long value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "(" + value + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rock rock = (Rock) o;
|
||||||
|
return value == rock.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Long.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Rock> transmutate() {
|
||||||
|
if(value == 0){
|
||||||
|
return List.of(Rock.of(1));
|
||||||
|
}
|
||||||
|
if( numberOfDigits(value) % 2 == 0){
|
||||||
|
return List.of(Rock.of(smallHalfOf(value)), Rock.of(largeHalfOf(value)));
|
||||||
|
|
||||||
|
}
|
||||||
|
return List.of(Rock.of(value * 2024));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Rock of(long i) {
|
||||||
|
// could leverage a nice cache
|
||||||
|
return new Rock(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long numberOfDigits(long n){
|
||||||
|
var ret = 1;
|
||||||
|
while(n >= 10){
|
||||||
|
n = n / 10;
|
||||||
|
ret ++;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private long smallHalfOf(long originalValue) {
|
||||||
|
var mod = 1;
|
||||||
|
var n = numberOfDigits(originalValue);
|
||||||
|
for(var i = 0; i < n /2 ; i++){
|
||||||
|
mod *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return originalValue % mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
private long largeHalfOf(long originalValue) {
|
||||||
|
var mod = 1;
|
||||||
|
var n = numberOfDigits(originalValue);
|
||||||
|
for(var i = 0; i < n /2 ; i++){
|
||||||
|
mod *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return originalValue / mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RockRow {
|
||||||
|
|
||||||
|
private final Map<Rock, Long> rocks;
|
||||||
|
|
||||||
|
public RockRow(Map<Rock, Long> rocks) {
|
||||||
|
this.rocks = rocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RockRow of(List<Rock> rocks) {
|
||||||
|
return new RockRow(rocks.stream().collect(groupingBy(Function.identity(), Collectors.counting())));
|
||||||
|
}
|
||||||
|
|
||||||
|
public RockRow transmutate() {
|
||||||
|
Map<Rock, Long> ret = new HashMap<>();
|
||||||
|
for (var itm : rocks.entrySet()) {
|
||||||
|
var newRocks = itm.getKey().transmutate();
|
||||||
|
for (var newRock : newRocks) {
|
||||||
|
ret.put(newRock, ret.getOrDefault(newRock, 0L) + itm.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new RockRow(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long size() {
|
||||||
|
return rocks.values().stream().mapToLong(x -> x).sum();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RockRow parseInput(String input) {
|
||||||
|
var ret = Stream.of(input.split(" ")).map(Long::parseLong).map(Rock::new).toList();
|
||||||
|
return RockRow.of(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static RockRow question11A(String input) {
|
||||||
|
var rocks=parseInput(input);
|
||||||
|
for(var i = 0; i < 25; i++){
|
||||||
|
rocks = rocks.transmutate();
|
||||||
|
}
|
||||||
|
return rocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RockRow question11B(String input) {
|
||||||
|
var rocks=parseInput(input);
|
||||||
|
for (var i = 0; i < 75; i++) {
|
||||||
|
rocks = rocks.transmutate();
|
||||||
|
}
|
||||||
|
return rocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("The answer to question 11A is: " + question11A(Inputs.REAL_INPUT).size()); // 199946
|
||||||
|
System.out.println("The answer to question 11B is: " + question11B(Inputs.REAL_INPUT).size());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user