aoc2024

Advent of Code 2024
Log | Files | Refs | README

commit efd500459462d87f5df89c3b0479a5356f8aa90d
parent d80a28341221572c97007e2743aa0dabf5585985
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Thu,  2 Jan 2025 21:36:16 +0000

Day 14, pt1

Diffstat:
Aday14.txt | 500+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday14_sample.txt | 12++++++++++++
Asrc/main/java/day14_pt1.java | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 595 insertions(+), 0 deletions(-)

diff --git a/day14.txt b/day14.txt @@ -0,0 +1,500 @@ +p=24,28 v=-92,3 +p=41,77 v=40,66 +p=40,18 v=98,-57 +p=90,81 v=-35,-20 +p=87,44 v=78,72 +p=67,16 v=31,-76 +p=39,41 v=-40,-50 +p=44,56 v=-30,-72 +p=48,8 v=95,42 +p=76,36 v=-36,-41 +p=87,55 v=-69,96 +p=66,84 v=83,45 +p=15,13 v=-13,82 +p=31,23 v=-81,64 +p=16,12 v=-10,63 +p=79,58 v=-88,-91 +p=34,77 v=39,-97 +p=14,27 v=67,48 +p=88,14 v=-24,57 +p=31,82 v=27,-15 +p=71,23 v=73,93 +p=65,101 v=83,59 +p=75,26 v=38,-43 +p=27,8 v=98,-61 +p=6,32 v=-53,-30 +p=66,69 v=13,-16 +p=34,88 v=52,-87 +p=65,52 v=34,-10 +p=93,25 v=-26,-26 +p=60,17 v=25,-29 +p=72,26 v=-54,82 +p=88,78 v=99,54 +p=44,77 v=-61,16 +p=97,99 v=57,97 +p=40,28 v=95,22 +p=64,55 v=65,-5 +p=63,57 v=-60,68 +p=61,71 v=96,88 +p=54,41 v=-62,87 +p=30,56 v=-36,-90 +p=34,12 v=61,-88 +p=83,17 v=84,80 +p=77,39 v=-39,7 +p=95,11 v=-35,-59 +p=98,29 v=26,-73 +p=78,52 v=31,11 +p=56,31 v=25,-7 +p=64,102 v=4,-97 +p=11,4 v=-36,-46 +p=12,21 v=-8,-93 +p=34,0 v=15,-30 +p=56,13 v=-3,-81 +p=75,90 v=-42,-32 +p=65,41 v=-21,32 +p=11,38 v=-53,77 +p=79,49 v=-74,-6 +p=77,83 v=90,-27 +p=61,68 v=-45,26 +p=76,54 v=-86,-30 +p=88,11 v=23,-28 +p=45,93 v=65,45 +p=89,29 v=-55,-17 +p=98,51 v=-47,41 +p=52,43 v=19,34 +p=63,9 v=-48,58 +p=61,8 v=19,-40 +p=83,22 v=-11,-45 +p=64,27 v=-76,-55 +p=20,53 v=-31,-91 +p=75,80 v=-2,-70 +p=21,89 v=-16,-80 +p=70,45 v=-5,17 +p=4,13 v=-99,-18 +p=5,4 v=-78,-97 +p=34,31 v=-49,41 +p=74,66 v=-54,-58 +p=79,90 v=-23,-90 +p=78,45 v=84,-7 +p=15,93 v=-59,42 +p=97,46 v=54,26 +p=46,33 v=-58,-62 +p=83,95 v=-77,65 +p=94,34 v=-38,-64 +p=32,11 v=-55,-97 +p=81,91 v=-11,-44 +p=33,35 v=-7,71 +p=92,26 v=69,22 +p=82,91 v=-42,76 +p=36,63 v=-64,28 +p=17,75 v=67,-45 +p=7,13 v=-4,27 +p=78,62 v=-5,-17 +p=31,28 v=-46,3 +p=8,52 v=-99,79 +p=24,56 v=-94,53 +p=50,3 v=28,25 +p=14,65 v=-65,9 +p=47,42 v=37,60 +p=100,99 v=-89,-69 +p=93,18 v=-80,39 +p=39,24 v=17,16 +p=89,3 v=-35,-84 +p=16,74 v=97,81 +p=11,76 v=91,52 +p=28,81 v=-6,-56 +p=77,8 v=12,-92 +p=29,55 v=85,85 +p=7,76 v=99,57 +p=60,6 v=92,1 +p=8,31 v=-96,10 +p=57,3 v=-50,-90 +p=37,40 v=49,91 +p=42,50 v=-27,60 +p=38,48 v=-98,36 +p=74,86 v=-75,95 +p=70,1 v=4,-54 +p=16,96 v=-74,6 +p=15,88 v=-74,59 +p=94,100 v=-4,-6 +p=39,64 v=-10,-40 +p=23,91 v=93,64 +p=74,95 v=-88,-38 +p=29,71 v=-58,40 +p=95,67 v=-63,-37 +p=46,48 v=8,33 +p=23,71 v=76,26 +p=61,42 v=-33,-79 +p=2,79 v=23,14 +p=24,26 v=-56,77 +p=35,5 v=6,-23 +p=81,33 v=41,-43 +p=50,55 v=6,-67 +p=4,47 v=-47,55 +p=6,62 v=-4,14 +p=18,36 v=9,-57 +p=91,40 v=66,17 +p=49,19 v=31,13 +p=6,65 v=-50,-70 +p=66,68 v=37,26 +p=97,11 v=-59,83 +p=86,32 v=-93,-74 +p=79,30 v=87,65 +p=13,64 v=39,38 +p=55,41 v=4,10 +p=62,43 v=62,-63 +p=67,28 v=2,65 +p=60,17 v=-54,-47 +p=39,10 v=-73,-85 +p=84,63 v=-58,-28 +p=71,88 v=-54,90 +p=34,15 v=-28,-42 +p=12,71 v=-50,-58 +p=3,44 v=-1,24 +p=65,10 v=3,-18 +p=82,67 v=-74,69 +p=44,82 v=15,-58 +p=30,1 v=92,-71 +p=94,19 v=14,-33 +p=100,50 v=-34,90 +p=36,59 v=9,-27 +p=36,61 v=-48,-74 +p=6,6 v=2,-95 +p=41,31 v=-3,65 +p=28,80 v=18,28 +p=21,17 v=-1,65 +p=35,10 v=-89,44 +p=8,59 v=-99,-64 +p=40,1 v=21,-54 +p=66,96 v=71,-59 +p=39,75 v=73,-32 +p=8,17 v=48,8 +p=66,3 v=10,8 +p=47,96 v=94,23 +p=43,55 v=52,55 +p=11,28 v=97,-81 +p=27,65 v=-42,-94 +p=39,38 v=28,-7 +p=58,76 v=-33,-82 +p=13,38 v=22,51 +p=90,75 v=78,-15 +p=42,20 v=52,75 +p=4,38 v=-71,89 +p=87,78 v=14,33 +p=77,73 v=41,-82 +p=34,16 v=3,-71 +p=93,46 v=48,41 +p=23,94 v=37,1 +p=0,96 v=-20,37 +p=91,77 v=19,-43 +p=47,100 v=-6,-4 +p=52,91 v=28,76 +p=25,95 v=18,-54 +p=21,67 v=67,55 +p=93,97 v=17,73 +p=39,1 v=-98,-11 +p=83,17 v=38,87 +p=100,35 v=84,-78 +p=40,45 v=21,-5 +p=74,24 v=-22,-11 +p=15,84 v=12,-66 +p=100,67 v=-69,-77 +p=6,28 v=-7,-45 +p=24,61 v=82,24 +p=2,32 v=16,-72 +p=29,6 v=-8,17 +p=2,24 v=94,-52 +p=2,39 v=65,31 +p=2,98 v=-90,-49 +p=39,87 v=-40,-82 +p=23,77 v=-28,-56 +p=46,32 v=95,84 +p=45,99 v=-58,-20 +p=55,21 v=-73,99 +p=57,47 v=-9,-70 +p=67,57 v=-85,55 +p=77,95 v=-97,11 +p=71,70 v=-83,-33 +p=28,25 v=-52,44 +p=42,29 v=74,-14 +p=27,6 v=27,-9 +p=51,39 v=-35,22 +p=76,32 v=84,-82 +p=62,1 v=-27,-30 +p=31,76 v=80,42 +p=81,52 v=32,-72 +p=65,79 v=-81,-51 +p=4,74 v=36,-1 +p=76,97 v=-5,-85 +p=79,60 v=81,74 +p=36,72 v=-98,-51 +p=0,95 v=-93,-90 +p=25,41 v=-38,-39 +p=81,22 v=-8,-33 +p=45,17 v=-61,32 +p=90,64 v=-69,86 +p=73,100 v=48,35 +p=27,79 v=-83,33 +p=86,2 v=-66,13 +p=67,27 v=16,10 +p=93,69 v=20,76 +p=10,44 v=36,48 +p=71,29 v=59,99 +p=6,6 v=-56,99 +p=66,29 v=53,-21 +p=90,102 v=-66,-94 +p=8,24 v=39,-63 +p=52,47 v=36,95 +p=2,40 v=-23,-67 +p=82,77 v=-48,-37 +p=93,32 v=-53,51 +p=24,5 v=-28,-90 +p=21,34 v=79,17 +p=67,15 v=-14,37 +p=43,37 v=67,-93 +p=89,96 v=64,-92 +p=9,8 v=94,-37 +p=60,71 v=20,-87 +p=8,19 v=66,13 +p=94,49 v=-32,-43 +p=80,82 v=-39,-94 +p=48,42 v=-9,-62 +p=79,47 v=82,-6 +p=37,11 v=89,-69 +p=55,1 v=74,73 +p=58,67 v=-27,98 +p=53,80 v=-27,-68 +p=80,21 v=62,-52 +p=74,45 v=4,17 +p=19,8 v=82,56 +p=3,20 v=67,11 +p=96,93 v=94,49 +p=15,18 v=8,47 +p=6,3 v=54,68 +p=84,36 v=-23,29 +p=72,100 v=20,63 +p=38,35 v=-94,90 +p=56,95 v=28,85 +p=47,9 v=92,-11 +p=42,48 v=57,-11 +p=14,17 v=52,-26 +p=47,30 v=83,82 +p=96,61 v=-35,57 +p=18,6 v=-6,-8 +p=54,82 v=-18,40 +p=5,30 v=79,-91 +p=97,44 v=63,48 +p=69,28 v=-37,-32 +p=84,4 v=87,-35 +p=7,60 v=-79,41 +p=70,20 v=53,29 +p=95,59 v=-45,10 +p=89,69 v=99,28 +p=29,52 v=-50,20 +p=41,52 v=-52,79 +p=49,12 v=77,-71 +p=4,84 v=-56,57 +p=5,71 v=-20,34 +p=64,12 v=-91,3 +p=59,40 v=-2,43 +p=90,70 v=75,-41 +p=44,70 v=-30,-63 +p=6,44 v=-13,-74 +p=81,24 v=-63,9 +p=24,5 v=-80,-6 +p=80,1 v=90,-11 +p=28,100 v=21,42 +p=14,26 v=-16,-64 +p=12,30 v=56,42 +p=7,26 v=11,60 +p=68,33 v=62,-14 +p=83,81 v=-20,-15 +p=68,100 v=56,42 +p=53,47 v=22,-79 +p=41,31 v=-92,22 +p=56,37 v=62,-93 +p=65,35 v=-69,62 +p=86,56 v=53,-17 +p=46,25 v=-67,-21 +p=21,47 v=67,60 +p=54,38 v=-73,-81 +p=24,10 v=70,-57 +p=21,57 v=-73,-55 +p=96,50 v=40,40 +p=92,87 v=14,-8 +p=82,78 v=39,59 +p=92,30 v=87,-12 +p=97,27 v=-30,-13 +p=91,8 v=-75,80 +p=79,81 v=50,-3 +p=95,32 v=-57,15 +p=34,59 v=-59,-78 +p=87,65 v=81,-29 +p=51,90 v=-57,81 +p=62,22 v=-43,-31 +p=23,3 v=85,49 +p=20,75 v=-13,-58 +p=26,75 v=-89,21 +p=37,48 v=3,74 +p=44,24 v=-27,56 +p=19,78 v=-43,78 +p=59,27 v=65,-55 +p=34,8 v=-89,49 +p=40,4 v=-90,-28 +p=89,93 v=-23,80 +p=76,101 v=-42,-49 +p=94,99 v=-23,-73 +p=46,44 v=-64,-48 +p=19,49 v=-22,-48 +p=93,36 v=-35,41 +p=6,7 v=43,6 +p=26,100 v=-25,49 +p=90,12 v=-82,57 +p=51,102 v=-6,-61 +p=98,2 v=62,11 +p=100,22 v=-13,22 +p=2,31 v=-23,5 +p=28,98 v=-98,-8 +p=4,2 v=75,6 +p=96,80 v=-87,9 +p=0,46 v=-93,-98 +p=67,60 v=62,84 +p=10,13 v=-90,88 +p=29,47 v=-89,-72 +p=35,32 v=-67,46 +p=24,16 v=-83,87 +p=53,51 v=-49,17 +p=35,24 v=-22,10 +p=13,94 v=-65,66 +p=77,99 v=59,-95 +p=35,46 v=-52,-24 +p=81,78 v=-81,46 +p=23,28 v=-28,39 +p=87,92 v=-72,-85 +p=66,50 v=-86,19 +p=93,77 v=-74,-59 +p=77,43 v=60,44 +p=21,59 v=-66,24 +p=34,58 v=-37,62 +p=6,11 v=54,-11 +p=2,2 v=-90,-78 +p=98,95 v=14,-42 +p=25,40 v=15,-50 +p=9,99 v=42,66 +p=82,83 v=5,-62 +p=9,75 v=-84,9 +p=93,34 v=83,81 +p=34,30 v=-15,-31 +p=94,24 v=-4,-7 +p=35,6 v=46,-30 +p=57,59 v=31,76 +p=60,95 v=28,54 +p=97,86 v=-33,98 +p=51,57 v=50,89 +p=61,21 v=14,-25 +p=6,7 v=72,27 +p=77,83 v=-97,-27 +p=84,99 v=93,-30 +p=59,25 v=62,-33 +p=27,57 v=-55,36 +p=48,100 v=-55,-73 +p=51,8 v=-30,92 +p=10,61 v=21,-65 +p=53,48 v=80,-43 +p=27,47 v=-10,79 +p=99,78 v=42,-49 +p=77,31 v=1,-50 +p=18,61 v=-16,26 +p=37,34 v=-12,44 +p=67,24 v=-94,-81 +p=39,19 v=-73,87 +p=56,59 v=74,7 +p=93,11 v=42,-64 +p=73,45 v=84,10 +p=46,6 v=-89,49 +p=27,26 v=76,-57 +p=22,42 v=-86,96 +p=55,6 v=-73,75 +p=7,58 v=48,-15 +p=83,99 v=47,83 +p=54,5 v=-64,-30 +p=51,12 v=34,-16 +p=89,72 v=32,-75 +p=96,101 v=66,18 +p=11,69 v=-62,26 +p=83,68 v=38,76 +p=0,37 v=82,-39 +p=3,82 v=-53,71 +p=84,91 v=-25,24 +p=18,77 v=39,-58 +p=1,74 v=-81,74 +p=85,17 v=-52,-44 +p=87,11 v=-11,80 +p=94,58 v=-97,-51 +p=21,25 v=83,14 +p=22,13 v=82,-23 +p=97,40 v=20,-98 +p=59,30 v=-70,23 +p=76,12 v=34,95 +p=76,15 v=26,78 +p=42,31 v=72,29 +p=10,18 v=-7,-4 +p=20,12 v=51,46 +p=56,98 v=98,-29 +p=96,25 v=57,15 +p=49,51 v=13,-24 +p=44,84 v=-42,60 +p=99,94 v=-62,1 +p=45,72 v=86,-46 +p=31,51 v=-82,75 +p=47,76 v=1,-6 +p=75,96 v=-57,-30 +p=26,90 v=-28,60 +p=67,91 v=62,-13 +p=24,68 v=18,71 +p=89,101 v=-35,42 +p=19,86 v=-25,-8 +p=25,84 v=15,28 +p=91,44 v=-75,-98 +p=34,55 v=9,38 +p=49,24 v=49,-40 +p=53,50 v=83,60 +p=16,71 v=-47,-68 +p=23,50 v=21,24 +p=30,23 v=-27,44 +p=92,38 v=-75,48 +p=98,77 v=24,-40 +p=39,17 v=-4,-19 +p=53,7 v=-67,-23 +p=76,32 v=-57,63 +p=40,90 v=92,95 +p=46,40 v=-16,-44 +p=88,22 v=-20,-98 +p=81,78 v=96,-3 +p=52,33 v=-71,76 +p=90,81 v=23,-27 +p=62,50 v=90,85 +p=38,57 v=-60,97 +p=78,61 v=-57,-41 +p=70,47 v=43,10 +p=7,83 v=2,25 +p=49,91 v=-61,76 +p=53,55 v=-51,87 +p=18,38 v=38,-12 +p=85,23 v=4,-21 +p=85,94 v=-14,90 +p=27,74 v=87,-44 +p=6,68 v=-93,-89 +p=59,75 v=-91,57 +p=87,88 v=75,-1 +p=34,82 v=-77,-22 +p=77,27 v=41,34 +p=18,20 v=85,99 +p=49,1 v=30,-43 +p=17,8 v=30,-80 +p=5,80 v=-1,-1 +p=67,14 v=-45,-3 +p=46,21 v=-3,-58 +p=48,10 v=36,-73 +p=78,57 v=-87,54 +p=50,40 v=-70,-74 diff --git a/day14_sample.txt b/day14_sample.txt @@ -0,0 +1,12 @@ +p=0,4 v=3,-3 +p=6,3 v=-1,-3 +p=10,3 v=-1,2 +p=2,0 v=2,-1 +p=0,0 v=1,3 +p=3,0 v=-2,-2 +p=7,6 v=-1,-3 +p=3,0 v=-1,-2 +p=9,3 v=2,3 +p=7,3 v=-1,2 +p=2,4 v=2,-3 +p=9,5 v=-3,-3 diff --git a/src/main/java/day14_pt1.java b/src/main/java/day14_pt1.java @@ -0,0 +1,83 @@ +import java.nio.file.Files; +import java.nio.file.Path; +import java.io.IOException; +import java.util.*; +import java.util.stream.*; +import java.util.regex.*; + +class day14_pt1 { + static final Pattern P = Pattern.compile("p=(-?\\d+),(-?\\d+) v=(-?\\d+),(-?\\d+)"); + record Point(int x, int y) { + Point add(Vec v, int width, int height) { + var nx = (this.x + v.x) % width; + if (nx < 0) { + nx += width; + } + var ny = (this.y + v.y) % height; + if (ny < 0) { + ny += height; + } + return new Point(nx, ny); + } + } + record Vec(int x, int y) {} + record Robot(int label, Vec velocity) {} + public static void main(String[] args) throws IOException { + var lines = Files.readAllLines(Path.of(args[0])); + var width = 101; + var height = 103; + var robots = new ArrayList<Robot>(); + var positions = new ArrayList<Point>(); + for (var ix=0; ix<lines.size(); ix++) { + var line = lines.get(ix); + var m = P.matcher(line); + if (!m.matches()) throw new RuntimeException("line didn't match [%s]".formatted(line)); + var px = Integer.parseInt(m.group(1)); + var py = Integer.parseInt(m.group(2)); + var dx = Integer.parseInt(m.group(3)); + var dy = Integer.parseInt(m.group(4)); + robots.add(new Robot(ix, new Vec(dx, dy))); + positions.add(new Point(px, py)); + } + // System.out.println(robots); + // System.out.println(positions); + for (var secs = 0; secs<100; secs++) { + for (var ix=0; ix<robots.size(); ix++) { + var robot = robots.get(ix); + var p = positions.get(ix); + var p2 = p.add(robot.velocity, width, height); + positions.set(ix, p2); + } + } + + var cts = positions.stream().collect(Collectors.toMap(p -> p, p -> 1, (i1, i2) -> i1 + i2)); + // debug + for (var y=0; y<height; y++) { + for (var x=0; x<width; x++) { + var ct = cts.get(new Point(x, y)); + if (ct != null) { + System.out.printf("%d", ct); + } else { + System.out.print("."); + } + } + System.out.print("\n"); + } + var quads = new int[][]{ + new int[]{0, 0, width/2, height/2}, + new int[]{width/2+1, 0, width, height/2}, + new int[]{0, height/2+1, width/2, height}, + new int[]{width/2+1, height/2+1, width, height}, + }; + var res = Arrays.stream(quads).mapToLong(quad -> { + var sum = 0l; + for (var y=quad[1]; y< quad[3]; y++) { + for (var x=quad[0]; x<quad[2]; x++) { + sum += cts.getOrDefault(new Point(x, y), 0); + } + } + return sum; + }).reduce(1, (a, b) -> a * b); + System.out.printf("Day 14 pt1: %d\n", res); + } +}