From 8c72fa449d09d2a861b89ed99321763a3242b7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sat, 14 Dec 2024 12:53:29 +0100 Subject: [PATCH] Day 14. --- data.go | 3 + day_14/data.txt | 500 ++++++++++++++++++++++++++++++++++++++++ day_14/example_data.txt | 12 + day_14/solution.go | 140 +++++++++++ day_14/solution_test.go | 15 ++ solutions.go | 3 + 6 files changed, 673 insertions(+) create mode 100644 day_14/data.txt create mode 100644 day_14/example_data.txt create mode 100644 day_14/solution.go create mode 100644 day_14/solution_test.go diff --git a/data.go b/data.go index fe3a252..d759129 100644 --- a/data.go +++ b/data.go @@ -42,3 +42,6 @@ var Day12Data string //go:embed day_13/data.txt var Day13Data string + +//go:embed day_14/data.txt +var Day14Data string diff --git a/day_14/data.txt b/day_14/data.txt new file mode 100644 index 0000000..f10d1d7 --- /dev/null +++ b/day_14/data.txt @@ -0,0 +1,500 @@ +p=77,59 v=2,-23 +p=77,53 v=-90,9 +p=85,100 v=-90,-77 +p=22,57 v=82,-94 +p=84,72 v=-39,60 +p=57,76 v=14,-94 +p=2,92 v=-52,-63 +p=38,73 v=42,-39 +p=4,93 v=-93,41 +p=79,56 v=27,-13 +p=20,5 v=-10,-68 +p=51,94 v=38,-2 +p=15,5 v=-93,-68 +p=4,49 v=56,6 +p=37,78 v=1,63 +p=46,60 v=-18,70 +p=19,47 v=-96,54 +p=79,83 v=21,-26 +p=77,59 v=82,-86 +p=27,40 v=-51,24 +p=84,68 v=-88,-72 +p=59,60 v=-22,18 +p=66,67 v=-88,24 +p=64,101 v=-67,86 +p=13,44 v=-53,-68 +p=44,53 v=-80,34 +p=31,71 v=27,-25 +p=98,71 v=90,78 +p=25,60 v=-19,-47 +p=64,59 v=-56,-16 +p=67,23 v=-63,72 +p=85,36 v=-99,-5 +p=89,64 v=-81,-13 +p=71,33 v=-7,-18 +p=30,75 v=27,74 +p=51,86 v=42,50 +p=35,3 v=-41,17 +p=67,7 v=52,44 +p=71,6 v=-40,21 +p=49,51 v=-18,-32 +p=77,22 v=-95,86 +p=72,16 v=-49,63 +p=96,11 v=-38,7 +p=26,44 v=82,-4 +p=36,5 v=42,24 +p=20,13 v=45,30 +p=47,23 v=8,-87 +p=24,99 v=13,-77 +p=2,66 v=58,70 +p=61,86 v=99,81 +p=42,67 v=20,80 +p=69,80 v=1,-15 +p=1,10 v=85,-55 +p=68,95 v=-16,16 +p=87,16 v=2,-17 +p=71,27 v=-73,14 +p=36,57 v=-82,83 +p=0,6 v=21,68 +p=41,67 v=-41,-99 +p=52,60 v=-52,-47 +p=11,32 v=-93,34 +p=31,27 v=64,53 +p=52,84 v=-73,36 +p=2,85 v=-62,46 +p=40,54 v=-97,-61 +p=59,58 v=-22,-70 +p=64,23 v=-87,-15 +p=42,34 v=-36,-44 +p=43,42 v=37,82 +p=40,75 v=-13,98 +p=65,18 v=38,-17 +p=53,51 v=-17,-70 +p=62,5 v=-91,7 +p=75,28 v=-19,-43 +p=87,101 v=-16,-2 +p=99,56 v=58,84 +p=69,101 v=89,-77 +p=5,12 v=91,-97 +p=34,35 v=-14,62 +p=52,87 v=85,-33 +p=18,13 v=86,-40 +p=51,37 v=55,61 +p=57,94 v=-13,-44 +p=87,100 v=-3,-13 +p=22,2 v=25,38 +p=8,97 v=-43,30 +p=55,27 v=14,-59 +p=33,13 v=68,-32 +p=97,55 v=-18,-14 +p=33,60 v=45,60 +p=88,29 v=-99,-31 +p=84,93 v=66,66 +p=28,83 v=31,27 +p=32,52 v=-69,-14 +p=100,93 v=80,92 +p=72,87 v=44,-32 +p=74,61 v=11,-52 +p=39,67 v=-14,37 +p=67,92 v=-17,-91 +p=26,26 v=38,-55 +p=50,15 v=19,36 +p=52,57 v=46,50 +p=78,19 v=48,-78 +p=56,42 v=-68,15 +p=43,17 v=14,48 +p=5,17 v=-94,95 +p=56,13 v=-49,7 +p=41,66 v=92,-1 +p=9,82 v=44,-10 +p=92,33 v=-16,66 +p=55,92 v=-45,50 +p=72,76 v=-68,-85 +p=40,69 v=4,-85 +p=29,84 v=54,8 +p=53,42 v=-27,-4 +p=32,80 v=-44,66 +p=87,3 v=53,-86 +p=0,48 v=-24,14 +p=76,84 v=25,55 +p=27,17 v=-14,72 +p=38,68 v=-82,54 +p=60,91 v=47,12 +p=74,17 v=-90,-73 +p=87,79 v=75,97 +p=73,70 v=-33,76 +p=21,83 v=66,17 +p=14,29 v=12,29 +p=61,38 v=-40,15 +p=0,59 v=-86,3 +p=27,19 v=27,-13 +p=14,37 v=4,14 +p=97,17 v=2,-59 +p=66,80 v=-99,85 +p=1,28 v=49,95 +p=28,99 v=4,7 +p=81,34 v=11,1 +p=8,63 v=67,33 +p=77,20 v=-79,-34 +p=76,92 v=66,-40 +p=1,54 v=3,-56 +p=15,66 v=73,-21 +p=86,45 v=33,-95 +p=94,35 v=59,-49 +p=22,9 v=-10,-3 +p=34,69 v=18,14 +p=99,45 v=12,89 +p=86,34 v=94,94 +p=87,16 v=91,70 +p=55,24 v=-48,-21 +p=75,44 v=-35,66 +p=85,77 v=66,-80 +p=30,72 v=60,98 +p=3,30 v=-86,-18 +p=24,60 v=-61,-10 +p=34,88 v=41,-20 +p=0,54 v=-66,28 +p=85,69 v=69,-5 +p=52,24 v=-60,3 +p=90,95 v=22,-81 +p=9,90 v=-75,59 +p=78,78 v=-76,-90 +p=66,61 v=-95,32 +p=24,70 v=-56,-71 +p=89,48 v=-80,-98 +p=74,7 v=-61,77 +p=51,66 v=-50,-1 +p=90,37 v=-58,20 +p=46,49 v=5,98 +p=95,0 v=57,-58 +p=44,6 v=79,-31 +p=41,53 v=-45,-2 +p=9,23 v=-6,-27 +p=35,69 v=-92,-99 +p=59,39 v=-30,-76 +p=23,36 v=-44,-82 +p=61,39 v=-39,-68 +p=3,3 v=-51,40 +p=23,79 v=-19,-11 +p=49,83 v=47,40 +p=84,64 v=-39,56 +p=57,91 v=19,17 +p=60,43 v=-73,73 +p=54,16 v=51,72 +p=54,10 v=-92,-99 +p=48,62 v=42,84 +p=45,42 v=42,-18 +p=72,99 v=86,48 +p=39,76 v=-9,-57 +p=83,61 v=-85,65 +p=67,80 v=98,-90 +p=28,27 v=-70,-37 +p=68,9 v=-40,34 +p=39,5 v=78,-40 +p=10,90 v=43,-86 +p=49,29 v=-8,39 +p=79,54 v=-8,-42 +p=79,71 v=-90,69 +p=100,50 v=35,28 +p=48,84 v=14,-85 +p=58,12 v=98,-26 +p=48,26 v=35,58 +p=40,11 v=78,-50 +p=82,51 v=71,-14 +p=52,4 v=-1,-77 +p=47,102 v=14,-68 +p=16,46 v=-47,-9 +p=14,63 v=95,9 +p=83,39 v=-34,-88 +p=2,51 v=-95,77 +p=57,20 v=-9,25 +p=87,1 v=90,-11 +p=55,40 v=-95,-83 +p=96,60 v=-2,-9 +p=86,12 v=58,70 +p=54,57 v=33,37 +p=16,88 v=-91,-83 +p=56,13 v=39,-38 +p=70,21 v=84,11 +p=4,17 v=90,-17 +p=45,18 v=-50,-50 +p=77,7 v=-63,-88 +p=2,59 v=-54,-59 +p=62,81 v=-22,8 +p=72,55 v=75,-47 +p=62,55 v=32,27 +p=57,56 v=-77,-89 +p=99,61 v=-54,65 +p=73,8 v=-81,-12 +p=12,96 v=-55,15 +p=44,41 v=8,30 +p=96,53 v=35,19 +p=26,65 v=64,-38 +p=14,85 v=15,45 +p=98,3 v=-2,35 +p=3,91 v=-56,-7 +p=48,6 v=-18,-44 +p=60,98 v=33,21 +p=43,45 v=-28,-60 +p=38,71 v=-46,13 +p=71,19 v=-86,-45 +p=99,48 v=31,61 +p=58,29 v=52,-31 +p=65,94 v=52,-12 +p=22,22 v=78,-78 +p=98,62 v=83,-11 +p=76,74 v=-72,93 +p=19,82 v=-39,58 +p=72,81 v=-9,41 +p=85,92 v=-62,17 +p=10,4 v=5,-61 +p=70,49 v=-40,79 +p=4,21 v=81,-31 +p=99,8 v=67,44 +p=85,34 v=-34,-19 +p=1,31 v=19,-19 +p=71,101 v=-12,-77 +p=15,81 v=-24,-90 +p=83,79 v=-17,-52 +p=48,91 v=97,-11 +p=51,57 v=-18,-5 +p=34,25 v=-92,25 +p=50,8 v=-96,-96 +p=71,5 v=94,86 +p=16,49 v=25,-88 +p=45,2 v=-37,39 +p=43,23 v=-79,67 +p=36,41 v=-73,70 +p=42,45 v=-26,-22 +p=29,100 v=4,-55 +p=60,98 v=-40,-27 +p=57,21 v=-7,-58 +p=96,22 v=21,-60 +p=36,97 v=-43,-1 +p=1,94 v=-94,91 +p=45,60 v=-78,37 +p=27,6 v=85,-51 +p=48,12 v=64,71 +p=89,75 v=-81,8 +p=76,51 v=-21,14 +p=71,13 v=20,-13 +p=90,9 v=43,-31 +p=33,4 v=-69,31 +p=54,65 v=-31,84 +p=61,87 v=56,-67 +p=34,42 v=-92,61 +p=2,5 v=-43,58 +p=44,2 v=55,-63 +p=66,50 v=-89,76 +p=24,77 v=-95,-71 +p=15,11 v=97,-88 +p=95,58 v=6,14 +p=70,72 v=-8,-57 +p=30,84 v=68,44 +p=8,67 v=-84,-19 +p=54,44 v=-59,-91 +p=69,42 v=-8,-93 +p=40,27 v=-18,-13 +p=17,38 v=-20,-93 +p=79,37 v=66,-51 +p=77,10 v=52,48 +p=18,77 v=85,71 +p=45,2 v=48,15 +p=93,98 v=26,53 +p=97,57 v=44,52 +p=91,80 v=94,83 +p=71,54 v=-5,91 +p=32,33 v=87,76 +p=21,59 v=31,56 +p=76,78 v=-30,-66 +p=39,45 v=-87,90 +p=46,84 v=-50,-76 +p=65,102 v=-77,-30 +p=5,46 v=-75,-84 +p=5,33 v=-94,6 +p=6,50 v=-51,-94 +p=74,26 v=-58,-27 +p=29,62 v=26,-32 +p=18,89 v=87,-63 +p=66,31 v=-94,46 +p=23,27 v=-42,67 +p=47,36 v=-87,-88 +p=11,92 v=-75,-76 +p=9,51 v=-1,19 +p=52,0 v=28,-35 +p=68,72 v=-63,10 +p=41,46 v=-32,75 +p=99,42 v=99,94 +p=98,29 v=3,45 +p=61,69 v=-45,-52 +p=50,66 v=66,8 +p=3,41 v=40,71 +p=83,45 v=-85,-23 +p=0,34 v=3,-93 +p=54,49 v=88,-23 +p=65,50 v=57,-38 +p=37,34 v=38,-86 +p=100,77 v=13,-23 +p=80,39 v=-13,32 +p=31,102 v=-1,16 +p=62,64 v=15,9 +p=48,13 v=-73,-87 +p=69,19 v=-95,16 +p=50,89 v=75,-85 +p=45,33 v=-96,-37 +p=13,75 v=77,60 +p=17,43 v=91,-41 +p=17,99 v=45,-86 +p=68,20 v=40,66 +p=47,11 v=-37,-12 +p=64,85 v=56,2 +p=46,30 v=92,43 +p=84,57 v=-99,-14 +p=55,18 v=46,-46 +p=42,87 v=64,-34 +p=94,96 v=57,16 +p=20,14 v=-24,2 +p=52,44 v=33,-9 +p=4,95 v=7,-76 +p=99,25 v=-74,87 +p=78,31 v=95,-69 +p=79,21 v=-55,32 +p=20,25 v=68,15 +p=77,102 v=2,-82 +p=45,29 v=-91,-31 +p=10,55 v=19,55 +p=74,0 v=75,-92 +p=38,64 v=-23,70 +p=10,99 v=58,-65 +p=8,73 v=97,41 +p=43,26 v=32,66 +p=73,96 v=20,-91 +p=30,27 v=-81,77 +p=13,0 v=-38,-82 +p=18,37 v=-42,15 +p=57,40 v=19,43 +p=25,68 v=82,32 +p=10,2 v=-80,-59 +p=0,85 v=12,-86 +p=60,18 v=68,21 +p=39,86 v=-69,-53 +p=98,0 v=26,-35 +p=58,17 v=1,-69 +p=38,44 v=-65,-86 +p=24,28 v=-47,-45 +p=20,27 v=9,-23 +p=0,65 v=77,-15 +p=95,71 v=94,78 +p=68,77 v=93,55 +p=77,3 v=-42,17 +p=94,4 v=53,68 +p=97,28 v=-29,-9 +p=54,84 v=50,67 +p=81,70 v=21,-52 +p=68,10 v=46,99 +p=29,34 v=-88,-64 +p=79,77 v=80,-6 +p=54,66 v=-13,-52 +p=67,50 v=7,60 +p=20,1 v=32,-96 +p=29,4 v=-83,49 +p=70,35 v=70,-13 +p=59,40 v=-22,80 +p=73,47 v=-3,66 +p=88,13 v=85,-36 +p=21,80 v=55,64 +p=37,28 v=-32,-97 +p=88,0 v=98,73 +p=68,59 v=29,-14 +p=90,87 v=62,31 +p=76,13 v=11,-31 +p=19,23 v=44,97 +p=25,46 v=77,-66 +p=95,41 v=70,-63 +p=4,11 v=35,-54 +p=83,30 v=-23,-31 +p=0,26 v=-97,53 +p=29,96 v=73,21 +p=5,51 v=81,28 +p=85,41 v=-62,-37 +p=60,22 v=-45,-13 +p=96,27 v=81,80 +p=46,37 v=-73,71 +p=46,30 v=65,19 +p=79,98 v=-12,-63 +p=99,33 v=-95,-45 +p=77,47 v=-44,-27 +p=48,97 v=-18,-44 +p=77,78 v=-2,11 +p=87,52 v=-44,5 +p=4,68 v=-15,23 +p=12,47 v=-79,5 +p=5,58 v=26,-61 +p=35,30 v=-84,17 +p=45,73 v=19,-33 +p=10,102 v=-97,-49 +p=70,67 v=75,9 +p=42,34 v=9,-41 +p=12,37 v=75,39 +p=1,5 v=-66,96 +p=78,60 v=-48,-5 +p=3,81 v=40,-15 +p=64,51 v=20,-23 +p=19,27 v=91,44 +p=66,4 v=43,40 +p=54,20 v=43,-17 +p=19,51 v=54,19 +p=97,70 v=-15,-47 +p=63,91 v=-6,-43 +p=72,50 v=-72,14 +p=48,29 v=70,86 +p=69,19 v=-26,95 +p=36,36 v=50,76 +p=51,2 v=-50,-54 +p=24,39 v=-78,-18 +p=50,89 v=65,-2 +p=27,34 v=-51,-65 +p=0,48 v=-7,-80 +p=12,58 v=8,51 +p=0,86 v=26,82 +p=15,92 v=-79,-40 +p=59,69 v=37,-62 +p=17,72 v=10,28 +p=76,42 v=-90,99 +p=59,45 v=-22,32 +p=59,45 v=-31,-51 +p=98,32 v=-29,47 +p=15,89 v=-1,-58 +p=23,90 v=22,36 +p=31,91 v=50,50 +p=20,69 v=-1,27 +p=18,80 v=36,74 +p=39,45 v=82,-88 +p=42,44 v=37,5 +p=37,69 v=-64,-80 +p=32,70 v=55,32 +p=96,102 v=62,12 +p=48,42 v=-76,-41 +p=47,65 v=14,-6 +p=27,97 v=25,12 +p=55,69 v=-55,22 +p=17,76 v=1,-61 +p=43,28 v=28,6 +p=29,25 v=73,-41 +p=64,10 v=34,-96 +p=63,41 v=93,15 +p=3,9 v=-52,77 +p=61,100 v=43,-5 +p=54,24 v=6,-79 +p=89,75 v=44,-85 +p=79,61 v=-90,-94 +p=99,102 v=-70,35 +p=39,65 v=-78,4 +p=5,19 v=63,71 +p=93,101 v=35,-44 +p=5,26 v=31,-80 +p=27,51 v=80,37 +p=62,88 v=-51,-77 +p=75,71 v=-12,27 +p=74,45 v=66,33 +p=22,42 v=34,67 \ No newline at end of file diff --git a/day_14/example_data.txt b/day_14/example_data.txt new file mode 100644 index 0000000..72a324a --- /dev/null +++ b/day_14/example_data.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 \ No newline at end of file diff --git a/day_14/solution.go b/day_14/solution.go new file mode 100644 index 0000000..d26393c --- /dev/null +++ b/day_14/solution.go @@ -0,0 +1,140 @@ +package day_14 + +import ( + "fmt" + "strings" +) + +type Robot struct { + pX, pY int + vX, vY int +} + +// task:https://adventofcode.com/2024/day/14 +func SolveBasic(input string) int { + return solveBasic(input, 101, 103) +} + +// task:https://adventofcode.com/2024/day/14 +func SolveBasicSmall(input string) int { + return solveBasic(input, 11, 7) +} + +func solveBasic(input string, width int, height int) int { + robots := loadRobots(input) + quads := []int{0, 0, 0, 0, 0} + for _, r := range robots { + r.pX = (r.pX + 100*r.vX) % width + r.pY = (r.pY + 100*r.vY) % height + if r.pX < 0 { + r.pX += width + } + if r.pY < 0 { + r.pY += height + } + quad := determineQuad(r, width, height) + quads[quad]++ + } + return quads[0] * quads[1] * quads[2] * quads[3] +} + +func determineQuad(r Robot, width int, height int) int { + if r.pX < width/2 && r.pY < height/2 { + return 0 + } + if r.pX > width/2 && r.pY < height/2 { + return 1 + } + if r.pX < width/2 && r.pY > height/2 { + return 2 + } + if r.pX > width/2 && r.pY > height/2 { + return 3 + } + return 4 +} + +func loadRobots(input string) []Robot { + var robots []Robot + for _, line := range strings.Split(input, "\n") { + r := Robot{} + _, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &r.pX, &r.pY, &r.vX, &r.vY) + if err != nil { + panic(err) + } + robots = append(robots, r) + } + return robots +} + +// task:https://adventofcode.com/2024/day/12#part2 +func SolveComplex(input string) int { + robots := loadRobots(input) + for i := 0; i < 100000; i++ { + if hasLine(robots) { + printRobots(i, robots) + } + step(robots) + } + return 0 +} + +func hasLine(robots []Robot) bool { + rows := map[int]int{} + cols := map[int]int{} + for _, r := range robots { + rows[r.pX]++ + cols[r.pY]++ + } + has10Row := 0 + has10Col := 0 + for _, v := range rows { + if v >= 15 { + has10Row++ + } + } + for _, v := range cols { + if v >= 15 { + has10Col++ + } + } + return has10Col >= 2 && has10Row >= 2 +} + +func step(robots []Robot) { + for i, r := range robots { + r.pX = (r.pX + r.vX) % 101 + r.pY = (r.pY + r.vY) % 103 + if r.pX < 0 { + r.pX += 101 + } + if r.pY < 0 { + r.pY += 103 + } + robots[i].pX = r.pX + robots[i].pY = r.pY + } +} + +func printRobots(day int, robots []Robot) { + field := createField() + for _, r := range robots { + field[r.pY][r.pX] = '#' + } + fmt.Printf("\n----\nDay: %d\n----\n", day) + for _, line := range field { + fmt.Printf("%s\n", string(line)) + } +} + +func createField() [][]rune { + field := [][]rune{} + for i := 0; i < 103; i++ { + line := make([]rune, 101) + for j := range line { + line[j] = '.' + } + field = append(field, line) + } + return field +} diff --git a/day_14/solution_test.go b/day_14/solution_test.go new file mode 100644 index 0000000..85708cc --- /dev/null +++ b/day_14/solution_test.go @@ -0,0 +1,15 @@ +package day_14_test + +import ( + "advent_of_code_2024/day_14" + "advent_of_code_2024/helpers" + _ "embed" + "testing" +) + +//go:embed example_data.txt +var ExampleData string + +func TestBasicSolutionExample(t *testing.T) { + helpers.Check(t, day_14.SolveBasicSmall, helpers.Format(ExampleData), 12) +} diff --git a/solutions.go b/solutions.go index 6eab4f5..7ef58c5 100644 --- a/solutions.go +++ b/solutions.go @@ -14,6 +14,7 @@ import ( "advent_of_code_2024/day_11" "advent_of_code_2024/day_12" "advent_of_code_2024/day_13" + "advent_of_code_2024/day_14" ) var solutions = map[string]Solution{ @@ -44,4 +45,6 @@ var solutions = map[string]Solution{ "12-complex-fast": {day_12.SolveComplexFast, Day12Data}, "13-basic": {day_13.SolveBasic, Day13Data}, "13-complex": {day_13.SolveComplex, Day13Data}, + "14-basic": {day_14.SolveBasic, Day14Data}, + "14-complex": {day_14.SolveComplex, Day14Data}, }