Skip to content
Snippets Groups Projects
Commit 1f169a43 authored by LUDMANN Pierre's avatar LUDMANN Pierre
Browse files

[day15] done

parent 81027ce7
No related branches found
No related tags found
1 merge request!2Day15 ludmann1
package CieDuVaseDesNoces;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
record Key(String key) {
@Override
public int hashCode() {
......@@ -11,14 +18,118 @@ record Key(String key) {
}
}
class BucketMap<K, V> implements Map<K, V> {
public int num_buckets;
ArrayList<LinkedList<SimpleEntry<K, V>>> boxes;
BucketMap() {
num_buckets = 256;
boxes = new ArrayList<>(num_buckets);
for (int i = 0; i < num_buckets; i++) {
boxes.add(new LinkedList<>());
}
}
public int size() {
return boxes.stream().mapToInt(LinkedList::size).sum();
}
public boolean isEmpty() {
return size() == 0;
}
public boolean containsKey(Object key) {
return boxes.get(key.hashCode()).stream().map(Entry::getKey)
.anyMatch(key::equals);
}
public boolean containsValue(Object value) {
return values().contains(value);
}
public V get(Object key) {
return boxes.get(key.hashCode()).stream()
.filter(entry -> entry.getKey().equals(key)).findAny()
.orElse(null).getValue();
}
public V put(K key, V value) {
LinkedList<SimpleEntry<K, V>> box = boxes.get(key.hashCode());
Optional<SimpleEntry<K, V>> optionalEntry =
box.stream().filter(entry -> entry.getKey().equals(key))
.findAny();
if (optionalEntry.isPresent())
return optionalEntry.get().setValue(value);
else {
SimpleEntry<K, V> ans = new SimpleEntry<>(key, value);
box.add(ans);
return null;
}
}
public V remove(Object key) {
LinkedList<SimpleEntry<K, V>> box = boxes.get(key.hashCode());
SimpleEntry<K, V> entryToRemove =
box.stream().filter(entry -> entry.getKey().equals(key))
.findAny().orElse(null);
return box.remove(entryToRemove) ? entryToRemove.getValue() : null;
}
public void putAll(Map<? extends K, ? extends V> m) {
m.forEach(this::put);
}
public void clear() {
boxes.forEach(List::clear);
}
public Set<K> keySet() {
return boxes.stream()
.flatMap(box -> box.stream().map(SimpleEntry::getKey))
.collect(Collectors.toSet());
}
public Collection<V> values() {
return boxes.stream()
.flatMap(box -> box.stream().map(SimpleEntry::getValue))
.toList();
}
public Set<Entry<K, V>> entrySet() {
return boxes.stream().flatMap(List::stream).collect(Collectors.toSet());
}
public List<V> getBucket(int i) {
return boxes.get(i).stream().map(SimpleEntry::getValue).toList();
}
}
public class Day15 extends Day {
Day15(String inputName) {
super(inputName);
}
@Override
String part1() {
return null;
return String.valueOf(Arrays.stream(input.getFirst().split(","))
.mapToInt(key -> new Key(key).hashCode()).sum());
}
@Override
String part2() {
return null;
BucketMap<Key, Integer> series = new BucketMap<>();
Matcher matcher = Pattern.compile(
"\\G(?:^|,)\\s*(?<label>\\w+)(?:-|=(?<focal>[1-9]))\\s*")
.matcher(input.getFirst());
while (matcher.find()) {
Key label = new Key(matcher.group("label"));
String focal = matcher.group("focal");
if (focal == null) series.remove(label);
else series.put(label, Integer.valueOf(focal));
}
return String.valueOf(IntStream.range(0, series.num_buckets).map(i ->
IntStream.range(0, series.getBucket(i).size())
.map(j -> series.getBucket(i).get(j) * (j + 1)).sum() *
(i + 1)).sum());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment