Skip to content

Commit 30cff01

Browse files
committed
Initial version with 2 demo days form 2019
Change-Id: Id6449c6e4f65d927a85bafd0c4528487562c6000
1 parent 054f59e commit 30cff01

File tree

9 files changed

+321
-1
lines changed

9 files changed

+321
-1
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
#IntelliJ
26+
*.iml
27+
.idea
28+
29+
#target folder
30+
target/

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# adventofcode2020
1+
# adventofcode2020
2+
Based on https://github.com/dave-burke/advent-of-code-java-starter/

pom.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.beachboys</groupId>
8+
<artifactId>adventofcode2020</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>9</source>
17+
<target>9</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
<properties>
23+
<maven.compiler.target>11</maven.compiler.target>
24+
<maven.compiler.source>11</maven.compiler.source>
25+
</properties>
26+
27+
28+
</project>

src/main/java/de/beachboys/Day.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.beachboys;
2+
3+
import java.util.List;
4+
5+
public interface Day {
6+
7+
String part1(List<String> input);
8+
9+
String part2(List<String> input);
10+
11+
}

src/main/java/de/beachboys/Day01.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package de.beachboys;
2+
3+
import java.io.FileInputStream;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class Day01 implements Day {
8+
9+
public String part1(List<String> input) {
10+
return Integer.valueOf(input.stream().mapToInt(Integer::valueOf).map(x -> x / 3 - 2).reduce(0, Integer::sum)).toString();
11+
}
12+
13+
public String part2(List<String> input) {
14+
return Integer.valueOf(input.stream().mapToInt(Integer::valueOf).map(this::calculateFuel).reduce(0, Integer::sum)).toString();
15+
}
16+
17+
private int calculateFuel(int i) {
18+
int totalFuel = 0;
19+
int currentValue = i;
20+
while (true) {
21+
currentValue = currentValue / 3 - 2;
22+
if (currentValue <= 0)
23+
break;
24+
totalFuel += currentValue;
25+
}
26+
return totalFuel;
27+
}
28+
29+
30+
}

src/main/java/de/beachboys/Day02.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package de.beachboys;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
public class Day02 implements Day {
9+
10+
public String part1(List<String> input) {
11+
String realInput = input.get(0);
12+
String[] stringArray = realInput.split(",");
13+
List<Integer> list = Arrays.stream(stringArray).map(Integer::valueOf).collect(Collectors.toList());
14+
15+
runLogic(list);
16+
17+
System.out.println("Complete list + " + list.toString());
18+
return list.get(0) + "";
19+
}
20+
21+
private void runLogic(List<Integer> list) {
22+
int currentIndex = 0;
23+
24+
int opcode = 0;
25+
while(opcode != 99) {
26+
opcode = list.get(currentIndex);
27+
switch (opcode) {
28+
case 99:
29+
//
30+
break;
31+
case 1:
32+
int result = list.get(list.get(currentIndex + 1)) + list.get(list.get(currentIndex + 2));
33+
int setIndex = list.get(currentIndex + 3);
34+
if (setIndex < list.size()) {
35+
list.set(setIndex, result);
36+
}
37+
break;
38+
case 2:
39+
int result2 = list.get(list.get(currentIndex + 1)) * list.get(list.get(currentIndex + 2));
40+
int setIndex2 = list.get(currentIndex + 3);
41+
if (setIndex2 < list.size()) {
42+
list.set(setIndex2, result2);
43+
}
44+
break;
45+
default:
46+
}
47+
currentIndex += 4;
48+
}
49+
}
50+
51+
public String part2(List<String> input) {
52+
String realInput = input.get(0);
53+
String[] stringArray = realInput.split(",");
54+
List<Integer> list = Arrays.stream(stringArray).map(Integer::valueOf).collect(Collectors.toList());
55+
List<Integer> listNew = list;
56+
for (int i = 0; i<100; i++) {
57+
for (int j = 0; j< 100; j++) {
58+
listNew = new ArrayList<>(list);
59+
listNew.set(1, i);
60+
listNew.set(2, j);
61+
try {
62+
runLogic(listNew);
63+
if (listNew.get(0) == 19690720) {
64+
return (listNew.get(1)*100+listNew.get(2)) + "";
65+
}
66+
} catch (Exception e) {
67+
}
68+
69+
}
70+
}
71+
72+
return "error";
73+
}
74+
75+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package de.beachboys;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
import java.util.function.Function;
6+
7+
import static java.util.stream.Collectors.toList;
8+
9+
public class Runner {
10+
11+
public static int CURRENT_DAY = 2;
12+
public static int CURRENT_PART = 1;
13+
14+
public static final Map<Integer, Day> DAYS = new HashMap<>();
15+
16+
static {
17+
DAYS.put(1, new Day01());
18+
DAYS.put(2, new Day02());
19+
}
20+
21+
public static void main(String[] args) {
22+
System.out.println("Current day: " + CURRENT_DAY + ", current part: " + CURRENT_PART);
23+
Function<List<String>, String> currentPart;
24+
if (CURRENT_PART == 1) {
25+
currentPart = DAYS.get(CURRENT_DAY)::part1;
26+
} else {
27+
currentPart = DAYS.get(CURRENT_DAY)::part2;
28+
}
29+
Scanner in = new Scanner(System.in);
30+
while (true) {
31+
System.out.println("Testinput (q to exit, d to download, r to use real data): ");
32+
String input = in.nextLine();
33+
switch (input) {
34+
case "q":
35+
System.exit(0);
36+
break;
37+
case "d":
38+
downloadInput();
39+
break;
40+
case "r":
41+
System.out.println(currentPart.apply(loadInputLines()));
42+
break;
43+
default:
44+
System.out.println(currentPart.apply(List.of(input)));
45+
break;
46+
}
47+
}
48+
}
49+
50+
private static void downloadInput() {
51+
// TODO
52+
}
53+
54+
private static List<String> loadInputLines(){
55+
String paddedDay = String.valueOf(CURRENT_DAY);
56+
if(CURRENT_DAY < 10) {
57+
paddedDay = "0" + CURRENT_DAY;
58+
}
59+
String fileName = "day" + paddedDay + ".txt";
60+
61+
try(BufferedReader r = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(fileName)))){
62+
return r.lines().collect(toList());
63+
} catch(IOException e){
64+
throw new UncheckedIOException(e);
65+
}
66+
}
67+
}

src/main/resources/day01.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
103910
2+
133712
3+
82560
4+
91679
5+
98354
6+
89007
7+
93288
8+
132363
9+
91373
10+
83666
11+
55958
12+
90387
13+
100869
14+
98127
15+
120197
16+
86931
17+
60370
18+
143999
19+
71541
20+
115662
21+
51287
22+
81624
23+
58307
24+
60408
25+
141664
26+
89781
27+
127772
28+
132353
29+
101220
30+
104001
31+
140488
32+
58072
33+
75764
34+
120003
35+
82386
36+
77603
37+
130604
38+
86672
39+
120987
40+
80334
41+
67674
42+
52918
43+
98041
44+
102541
45+
97612
46+
50436
47+
129998
48+
84854
49+
101867
50+
82039
51+
108966
52+
80708
53+
54588
54+
86854
55+
89607
56+
71869
57+
126093
58+
89460
59+
86558
60+
77651
61+
53295
62+
132188
63+
137266
64+
97370
65+
114620
66+
86691
67+
147199
68+
147299
69+
72616
70+
142654
71+
88610
72+
104030
73+
64256
74+
54867
75+
76532
76+
145081
77+
102335
78+
72987
79+
72684
80+
148155
81+
59739
82+
85954
83+
141001
84+
125171
85+
107764
86+
141622
87+
89536
88+
92435
89+
69038
90+
84518
91+
119700
92+
119801
93+
81677
94+
125317
95+
72683
96+
128905
97+
93666
98+
75633
99+
117361
100+
82295

src/main/resources/day02.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,9,19,23,2,23,10,27,1,27,5,31,1,31,6,35,1,6,35,39,2,39,13,43,1,9,43,47,2,9,47,51,1,51,6,55,2,55,10,59,1,59,5,63,2,10,63,67,2,9,67,71,1,71,5,75,2,10,75,79,1,79,6,83,2,10,83,87,1,5,87,91,2,9,91,95,1,95,5,99,1,99,2,103,1,103,13,0,99,2,14,0,0

0 commit comments

Comments
 (0)