Last active
December 15, 2024 22:41
-
-
Save kravchik/6867b2388d3bd8352b4a45a69a39d8ab to your computer and use it in GitHub Desktop.
Aoc24.12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package yk.aoc2024; | |
import org.junit.Test; | |
import yk.aoc2024.utils.AocUtils2D; | |
import yk.jcommon.fastgeom.Vec2i; | |
import yk.jcommon.fastgeom.yfor.YForVec2i; | |
import yk.ycollections.Tuple; | |
import yk.ycollections.YList; | |
import yk.ycollections.YSet; | |
import static org.junit.Assert.assertEquals; | |
import static yk.aoc2024.utils.AocUtils.INT_ADD; | |
import static yk.aoc2024.utils.AocUtils.readPuzzle; | |
import static yk.aoc2024.utils.AocUtils2D.*; | |
import static yk.jcommon.fastgeom.Vec2i.v2i; | |
import static yk.ycollections.YArrayList.al; | |
import static yk.ycollections.YHashSet.hs; | |
import static yk.yfor.YForGen.yfor; | |
import static yk.yfor.YForGenerators.yfor; | |
public class Aoc12 { | |
public static String TEST_INPUT_1 = | |
"AAAA\n" + | |
"BBCD\n" + | |
"BBCC\n" + | |
"EEEC"; | |
public static String TEST_INPUT_2 = | |
"OOOOO\n" + | |
"OXOXO\n" + | |
"OOOOO\n" + | |
"OXOXO\n" + | |
"OOOOO"; | |
public static String TEST_INPUT_21 = | |
"EEEEE\n" + | |
"EXXXX\n" + | |
"EEEEE\n" + | |
"EXXXX\n" + | |
"EEEEE"; | |
public static String TEST_INPUT_22 = | |
"AAAAAA\n" + | |
"AAABBA\n" + | |
"AAABBA\n" + | |
"ABBAAA\n" + | |
"ABBAAA\n" + | |
"AAAAAA"; | |
public static String TEST_INPUT_3 = | |
"RRRRIICCFF\n" + | |
"RRRRIICCCF\n" + | |
"VVRRRCCFFF\n" + | |
"VVRCCCJFFF\n" + | |
"VVVVCJJCFE\n" + | |
"VVIVCCJJEE\n" + | |
"VVIIICJJEE\n" + | |
"MIIIIIJJEE\n" + | |
"MIIISIJEEE\n" + | |
"MMMISSJEEE"; | |
@Test | |
public void test1() { | |
assertEquals(140, solve1(TEST_INPUT_1)); | |
assertEquals(772, solve1(TEST_INPUT_2)); | |
assertEquals(1930, solve1(TEST_INPUT_3)); | |
} | |
@Test | |
public void test2() { | |
assertEquals(80, solve2(TEST_INPUT_1)); | |
assertEquals(436, solve2(TEST_INPUT_2)); | |
assertEquals(236, solve2(TEST_INPUT_21)); | |
assertEquals(368, solve2(TEST_INPUT_22)); | |
assertEquals(1206, solve2(TEST_INPUT_3)); | |
} | |
@Test | |
public void solution1() { | |
assertEquals(1461806, solve1(readPuzzle("aoc12.txt"))); | |
} | |
@Test | |
public void solution2() { | |
assertEquals(887932, solve2(readPuzzle("aoc12.txt"))); | |
} | |
private int solve1(String input) { | |
return extractFields(parse2d(input)) | |
.map(f -> f.size() * f.reduce(0, (i, pos) -> i + DIRS.filter(d -> !f.contains(d.add(pos))).size())) | |
.reduce(INT_ADD); | |
} | |
private int solve2(String input) { | |
YSet<Tuple<Vec2i, Vec2i>> seen = hs(); | |
return extractFields(parse2d(input)).map(field -> yfor(field) | |
.flatMap(pos -> DIRS | |
.filter(dir -> !field.contains(pos.add(dir))) | |
.filter(dir -> !seen.contains(new Tuple<>(pos, dir))) | |
.map(dir -> al(dir.rot90(), dir.rot_90()).map(d -> yfor(pos, p -> p.add(d)) | |
.whilst(p -> field.contains(p) && !field.contains(p.add(dir))) | |
.peek(p -> seen.add(new Tuple<>(p, dir))) | |
.count()))) | |
.count() * field.size()).reduce(INT_ADD); | |
} | |
private static YList<YSet<Vec2i>> extractFields(YList<YList<String>> map) { | |
YSet<Vec2i> seen = hs(); | |
return new YForVec2i(v2i(0), v2i(map.first().size(), map.size())) | |
.filter(p -> !seen.contains(p)) | |
.map(p -> { | |
YSet<Vec2i> fieldBody = hs(p); | |
String fieldType = getAt(map, p); | |
YSet<Vec2i> edge = hs(p); | |
while (edge.notEmpty()) { | |
edge = edge.flatMap(e -> AocUtils2D.DIRS | |
.map(d -> e.add(d)) | |
.filter(p2 -> !fieldBody.contains(p2)) | |
.filter(p2 -> fieldType.equals(getAtOr(map, p2, null))) | |
.forThis(m -> fieldBody.addAll(m))); | |
} | |
seen.addAll(fieldBody); | |
return fieldBody; | |
}).toList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment