Skip to content

Commit 0c19e41

Browse files
committed
add solutions for year 2016
1 parent 264435d commit 0c19e41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2919
-103
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package de.madjosz.adventofcode.data;
2+
3+
import java.util.List;
4+
5+
public record Coord(int x, int y) {
6+
7+
public static List<int[]> DIRECTIONS = List.of(new int[] { -1, 0 }, new int[] { 1, 0 }, new int[] { 0, -1 },
8+
new int[] { 0, 1 });
9+
10+
public Coord(int[] c) {
11+
this(c[0], c[1]);
12+
}
13+
14+
public Coord add(int[] dir) {
15+
return new Coord(x + dir[0], y + dir[1]);
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.madjosz.adventofcode.data;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
6+
7+
public final class MD5 {
8+
9+
private MD5() {}
10+
11+
public static MessageDigest get() {
12+
try {
13+
return MessageDigest.getInstance("MD5");
14+
} catch (NoSuchAlgorithmException e) {
15+
throw new RuntimeException(e);
16+
}
17+
}
18+
19+
}

src/main/java/de/madjosz/adventofcode/y2015/Day03.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.madjosz.adventofcode.y2015;
22

3+
import de.madjosz.adventofcode.data.Coord;
34
import java.util.HashSet;
45
import java.util.Set;
56

@@ -64,26 +65,4 @@ public int a2() {
6465
}
6566
return visited.size();
6667
}
67-
68-
private static class Coord {
69-
70-
private final int x;
71-
private final int y;
72-
73-
public Coord(int[] c) {
74-
this.x = c[0];
75-
this.y = c[1];
76-
}
77-
78-
@Override
79-
public int hashCode() {
80-
return 31 * x + y;
81-
}
82-
83-
@Override
84-
public boolean equals(Object obj) {
85-
return obj instanceof Coord other && this.x == other.x && this.y == other.y;
86-
}
87-
88-
}
8968
}

src/main/java/de/madjosz/adventofcode/y2015/Day04.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package de.madjosz.adventofcode.y2015;
22

3+
import de.madjosz.adventofcode.data.MD5;
34
import java.security.MessageDigest;
4-
import java.security.NoSuchAlgorithmException;
55
import java.util.function.Predicate;
66

77

88
public class Day04 {
99

1010
private final String secretKey;
1111

12+
private static final MessageDigest md5 = MD5.get();
13+
1214
public Day04(String input) {
1315
this.secretKey = input;
1416
}
@@ -18,7 +20,6 @@ public int a1() {
1820
}
1921

2022
private int findSecret(Predicate<byte[]> checkDigest) {
21-
MessageDigest md5 = getMD5();
2223
for (int i = 0;; ++i) {
2324
String key = secretKey + i;
2425
byte[] digest = md5.digest(key.getBytes());
@@ -37,12 +38,4 @@ public int a2() {
3738
private static boolean sixZeros(byte[] digest) {
3839
return digest[0] == 0 && digest[1] == 0 && digest[2] == 0;
3940
}
40-
41-
private static MessageDigest getMD5() {
42-
try {
43-
return MessageDigest.getInstance("MD5");
44-
} catch (NoSuchAlgorithmException e) {
45-
throw new RuntimeException(e);
46-
}
47-
}
4841
}

src/main/java/de/madjosz/adventofcode/y2015/Day18.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static java.util.stream.Collectors.toCollection;
44

5+
import de.madjosz.adventofcode.data.Coord;
56
import java.util.ArrayList;
67
import java.util.LinkedHashMap;
78
import java.util.LinkedHashSet;
@@ -85,7 +86,7 @@ private Iterable<Coord> getNeighbors(Coord cell) {
8586
}
8687

8788
private boolean inGrid(Coord c) {
88-
return c.x >= 0 && c.x < width && c.y >= 0 && c.y < height;
89+
return c.x() >= 0 && c.x() < width && c.y() >= 0 && c.y() < height;
8990
}
9091

9192
protected Set<Coord> initialNextGen() {
@@ -116,30 +117,4 @@ protected Set<Coord> initialNextGen() {
116117
return nextGen;
117118
}
118119
}
119-
120-
private static class Coord {
121-
122-
private final int x;
123-
private final int y;
124-
125-
public Coord(int x, int y) {
126-
this.x = x;
127-
this.y = y;
128-
}
129-
130-
public Coord add(int[] dir) {
131-
return new Coord(x + dir[0], y + dir[1]);
132-
}
133-
134-
@Override
135-
public int hashCode() {
136-
return 31 * x + y;
137-
}
138-
139-
@Override
140-
public boolean equals(Object obj) {
141-
return obj instanceof Coord other && this.x == other.x && this.y == other.y;
142-
}
143-
144-
}
145120
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.madjosz.adventofcode.y2016;
2+
3+
import static java.util.stream.Collectors.toList;
4+
5+
import de.madjosz.adventofcode.data.Coord;
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
12+
public class Day01 {
13+
14+
private final List<String> input;
15+
16+
public Day01(String input) {
17+
this.input = Arrays.stream(input.split(", ")).collect(toList());
18+
}
19+
20+
public int a1() {
21+
int[] pos = new int[2];
22+
int dir = 1;
23+
for (int i = 0; i < input.size(); ++i) {
24+
String line = input.get(i);
25+
dir *= (line.charAt(0) == 'R' ? 1 : -1) * (i % 2 == 0 ? 1 : -1);
26+
pos[i % 2] += dir * Integer.parseInt(line.substring(1));
27+
}
28+
return Math.abs(pos[0]) + Math.abs(pos[1]);
29+
}
30+
31+
public int a2() {
32+
Set<Coord> visited = new HashSet<>();
33+
visited.add(new Coord(0, 0));
34+
int[] pos = new int[2];
35+
int dir = 1;
36+
for (int i = 0; i < input.size(); ++i) {
37+
String line = input.get(i);
38+
dir *= (line.charAt(0) == 'R' ? 1 : -1) * (i % 2 == 0 ? 1 : -1);
39+
int steps = Integer.parseInt(line.substring(1));
40+
for (int j = 0; j < steps; ++j) {
41+
pos[i % 2] += dir;
42+
if (!visited.add(new Coord(pos))) return Math.abs(pos[0]) + Math.abs(pos[1]);
43+
}
44+
}
45+
throw new IllegalArgumentException();
46+
}
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package de.madjosz.adventofcode.y2016;
2+
3+
import static java.lang.Math.abs;
4+
5+
import java.util.List;
6+
import java.util.function.IntConsumer;
7+
8+
9+
public class Day02 {
10+
11+
private final List<String> input;
12+
13+
public Day02(List<String> input) {
14+
this.input = input;
15+
}
16+
17+
public int a1() {
18+
int[] pos = new int[] { 1, 1 };
19+
int code = 0;
20+
for (String line : input) {
21+
line.chars().forEach(move1(pos));
22+
code = code * 10 + 3 * pos[1] + pos[0] + 1;
23+
}
24+
return code;
25+
}
26+
27+
private IntConsumer move1(int[] pos) {
28+
return i -> {
29+
switch (i) {
30+
case 'D': if (pos[1] < 2) ++pos[1]; break;
31+
case 'U': if (pos[1] > 0) --pos[1]; break;
32+
case 'R': if (pos[0] < 2) ++pos[0]; break;
33+
case 'L': if (pos[0] > 0) --pos[0]; break;
34+
}
35+
};
36+
}
37+
38+
private static final char[] KEYPAD = new char[] { '1', '3', '7', 'B', 'D' };
39+
40+
public String a2() {
41+
int[] pos = new int[] { -2, 0 };
42+
StringBuilder code = new StringBuilder();
43+
for (String line : input) {
44+
line.chars().forEach(move2(pos));
45+
code.appendCodePoint(KEYPAD[pos[1] + 2] + pos[0]);
46+
}
47+
return code.toString();
48+
}
49+
50+
private IntConsumer move2(int[] pos) {
51+
return i -> {
52+
switch (i) {
53+
case 'D': if (pos[1] + abs(pos[0]) < 2) ++pos[1]; break;
54+
case 'U': if (pos[1] - abs(pos[0]) > -2) --pos[1]; break;
55+
case 'R': if (pos[0] + abs(pos[1]) < 2) ++pos[0]; break;
56+
case 'L': if (pos[0] - abs(pos[1]) > -2) --pos[0]; break;
57+
}
58+
};
59+
}
60+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.madjosz.adventofcode.y2016;
2+
3+
import static java.util.stream.Collectors.toList;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.IntStream;
8+
9+
10+
public class Day03 {
11+
12+
private final List<int[]> sides;
13+
14+
public Day03(List<String> input) {
15+
this.sides = input.stream()
16+
.map(s -> s.split("\s+"))
17+
.map(p -> IntStream.rangeClosed(1, 3).map(i -> Integer.parseInt(p[i])).toArray())
18+
.collect(toList());
19+
}
20+
21+
public int a1() {
22+
return (int) sides.stream().map(int[]::clone).filter(a -> {
23+
Arrays.sort(a);
24+
return a[0] + a[1] > a[2];
25+
}).count();
26+
}
27+
28+
public int a2() {
29+
int count = 0;
30+
for (int i = 0; i < sides.size(); i += 3)
31+
for (int j = 0; j < 3; ++j) {
32+
int[] a = new int[] { sides.get(i)[j], sides.get(i + 1)[j], sides.get(i + 2)[j] };
33+
Arrays.sort(a);
34+
if (a[0] + a[1] > a[2]) ++count;
35+
}
36+
return count;
37+
}
38+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package de.madjosz.adventofcode.y2016;
2+
3+
import static java.util.function.Function.identity;
4+
import static java.util.stream.Collectors.counting;
5+
import static java.util.stream.Collectors.groupingBy;
6+
import static java.util.stream.Collectors.joining;
7+
import static java.util.stream.Collectors.toList;
8+
9+
import java.util.Arrays;
10+
import java.util.Comparator;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Map.Entry;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
import java.util.stream.Stream;
17+
18+
19+
public class Day04 {
20+
21+
private final List<Room> rooms;
22+
23+
private static final Pattern ROOM = Pattern.compile("([a-z\\-]+)-(\\d+)\\[([a-z\\-]+)\\]");
24+
25+
private record Room(String name, int sectorid, String checksum) {
26+
27+
private Room shiftName() {
28+
String shifted = name().chars()
29+
.map(i -> i == '-' ? ' ' : (((i + sectorid - 'a') % 26) + 'a'))
30+
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
31+
.toString();
32+
return new Room(shifted, sectorid, checksum);
33+
}
34+
}
35+
36+
public Day04(List<String> input) {
37+
this.rooms = input.stream()
38+
.map(ROOM::matcher)
39+
.filter(Matcher::find)
40+
.map(g -> new Room(g.group(1), Integer.parseInt(g.group(2)), g.group(3)))
41+
.collect(toList());
42+
}
43+
44+
public int a1() {
45+
return validRooms().mapToInt(Room::sectorid).sum();
46+
}
47+
48+
private Stream<Room> validRooms() {
49+
return rooms.stream().filter(this::isValidRoom);
50+
}
51+
52+
private boolean isValidRoom(Room room) {
53+
String checksum = Arrays.stream(room.name().replace("-", "").split(""))
54+
.collect(groupingBy(identity(), counting()))
55+
.entrySet()
56+
.stream()
57+
.sorted(Comparator.<Entry<String, Long>, Long> comparing(Map.Entry::getValue)
58+
.reversed()
59+
.thenComparing(Entry::getKey))
60+
.map(Entry::getKey)
61+
.limit(5)
62+
.collect(joining());
63+
return room.checksum().equals(checksum);
64+
}
65+
66+
public int a2() {
67+
return a2("northpole object");
68+
}
69+
70+
int a2(String neededRoom) {
71+
return validRooms().map(Room::shiftName)
72+
.filter(r -> r.name().startsWith(neededRoom))
73+
.map(Room::sectorid)
74+
.findAny()
75+
.orElseThrow();
76+
}
77+
78+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy