diff --git a/2024/day14/input b/2024/day14/input new file mode 100644 index 0000000..850dcd8 --- /dev/null +++ b/2024/day14/input @@ -0,0 +1,500 @@ +p=52,66 v=37,34 +p=0,87 v=41,-19 +p=92,102 v=-26,86 +p=7,12 v=-16,-17 +p=85,34 v=81,38 +p=80,28 v=88,-61 +p=25,20 v=27,92 +p=28,45 v=-80,66 +p=72,86 v=47,-85 +p=36,2 v=28,30 +p=36,29 v=28,69 +p=84,102 v=-26,86 +p=52,73 v=68,-91 +p=26,38 v=-68,-69 +p=54,100 v=65,-70 +p=99,77 v=-1,-42 +p=50,59 v=-94,87 +p=65,34 v=47,-96 +p=87,100 v=-78,86 +p=25,31 v=-62,7 +p=78,8 v=67,-69 +p=19,59 v=-28,9 +p=85,61 v=-17,84 +p=20,28 v=-25,13 +p=25,21 v=-68,-50 +p=34,30 v=9,32 +p=91,15 v=-4,45 +p=2,58 v=-60,56 +p=52,89 v=-21,-79 +p=59,11 v=-88,23 +p=64,45 v=-91,72 +p=42,102 v=-15,-92 +p=64,56 v=99,-36 +p=69,36 v=10,-15 +p=22,74 v=-74,-13 +p=74,32 v=-11,-15 +p=49,37 v=-9,-48 +p=31,55 v=70,-78 +p=15,75 v=-65,-38 +p=69,38 v=54,-69 +p=32,36 v=93,68 +p=36,96 v=67,23 +p=57,96 v=-82,-67 +p=6,88 v=48,30 +p=13,67 v=76,87 +p=32,34 v=-67,-87 +p=87,41 v=-57,11 +p=1,14 v=54,-99 +p=88,51 v=34,50 +p=73,80 v=38,56 +p=19,95 v=88,27 +p=80,71 v=-66,-82 +p=79,30 v=84,54 +p=24,22 v=91,-55 +p=56,27 v=-27,1 +p=69,80 v=4,46 +p=50,78 v=-85,84 +p=7,27 v=51,29 +p=71,78 v=47,96 +p=86,90 v=-23,58 +p=31,37 v=6,19 +p=47,31 v=-95,16 +p=33,91 v=-89,80 +p=36,64 v=-3,6 +p=80,50 v=-38,25 +p=54,84 v=28,21 +p=77,26 v=58,81 +p=43,43 v=68,56 +p=60,95 v=-48,71 +p=95,25 v=5,-57 +p=80,95 v=-45,78 +p=66,29 v=-20,82 +p=99,76 v=-7,-91 +p=81,31 v=-69,-88 +p=76,57 v=-75,94 +p=82,17 v=-66,-5 +p=86,13 v=-13,21 +p=12,94 v=-89,51 +p=21,30 v=61,72 +p=7,98 v=-35,-79 +p=64,92 v=-41,27 +p=16,54 v=-27,-53 +p=69,56 v=-35,-67 +p=51,84 v=-33,99 +p=87,98 v=78,64 +p=10,25 v=33,-75 +p=57,12 v=13,42 +p=9,98 v=-83,-23 +p=97,66 v=32,-75 +p=86,50 v=-29,22 +p=43,0 v=18,21 +p=90,23 v=29,-49 +p=18,63 v=77,-51 +p=61,58 v=-60,-44 +p=69,94 v=-94,33 +p=67,100 v=77,-8 +p=12,50 v=30,47 +p=57,96 v=-24,2 +p=60,95 v=25,-51 +p=9,4 v=3,-46 +p=4,22 v=94,60 +p=79,16 v=38,-93 +p=62,57 v=78,-72 +p=78,0 v=70,92 +p=7,91 v=-93,-25 +p=83,48 v=-57,34 +p=73,35 v=-51,-90 +p=5,32 v=94,-9 +p=89,36 v=-23,44 +p=36,33 v=-27,60 +p=90,98 v=-66,2 +p=81,9 v=93,58 +p=27,101 v=92,36 +p=72,39 v=96,-15 +p=48,75 v=-77,-52 +p=64,1 v=10,45 +p=31,75 v=-31,-99 +p=54,50 v=-92,-40 +p=82,78 v=23,46 +p=92,48 v=34,-65 +p=91,54 v=20,-84 +p=90,67 v=69,-88 +p=72,35 v=-60,-40 +p=18,76 v=55,-44 +p=25,101 v=-92,-73 +p=64,15 v=7,-77 +p=42,42 v=-67,66 +p=47,38 v=-95,44 +p=54,46 v=-45,-19 +p=25,0 v=37,-27 +p=0,55 v=23,-62 +p=41,66 v=43,-94 +p=58,89 v=-61,11 +p=41,90 v=-58,52 +p=29,83 v=-37,2 +p=50,45 v=16,6 +p=59,6 v=65,-83 +p=78,84 v=37,73 +p=2,24 v=-58,-46 +p=100,30 v=8,-96 +p=13,55 v=64,65 +p=84,83 v=78,-1 +p=16,46 v=79,38 +p=94,34 v=-1,75 +p=36,82 v=-58,-85 +p=4,52 v=25,-64 +p=31,101 v=49,5 +p=89,6 v=26,36 +p=46,97 v=58,-64 +p=65,5 v=74,55 +p=24,37 v=-25,41 +p=40,89 v=67,24 +p=84,5 v=29,-48 +p=26,94 v=81,98 +p=51,8 v=-21,-84 +p=10,82 v=-51,-32 +p=56,89 v=50,-6 +p=53,13 v=-58,68 +p=47,36 v=-73,85 +p=59,68 v=16,-60 +p=1,83 v=54,-63 +p=43,6 v=-67,-49 +p=2,68 v=51,9 +p=58,42 v=40,-75 +p=10,71 v=-22,-26 +p=17,79 v=-80,59 +p=28,12 v=24,-11 +p=1,100 v=26,84 +p=58,99 v=16,67 +p=54,8 v=-79,-92 +p=69,26 v=81,-58 +p=11,7 v=47,-41 +p=87,15 v=-99,26 +p=60,20 v=-40,-24 +p=71,92 v=3,37 +p=97,82 v=-41,40 +p=54,12 v=-30,-27 +p=32,17 v=61,82 +p=54,95 v=-38,-16 +p=42,26 v=-67,57 +p=22,45 v=73,69 +p=18,0 v=-22,-92 +p=70,39 v=19,95 +p=63,72 v=10,21 +p=26,88 v=24,46 +p=4,101 v=75,43 +p=31,31 v=30,-12 +p=71,10 v=43,-86 +p=68,42 v=-2,94 +p=90,76 v=-38,68 +p=71,76 v=63,-13 +p=35,76 v=-21,6 +p=95,28 v=35,-77 +p=22,7 v=-52,-8 +p=23,56 v=-8,69 +p=54,45 v=-76,-90 +p=56,64 v=-91,-75 +p=28,39 v=-41,77 +p=42,51 v=-12,19 +p=28,67 v=12,-44 +p=86,52 v=75,-84 +p=69,54 v=71,90 +p=42,70 v=-52,-25 +p=14,48 v=16,-96 +p=1,65 v=-26,-72 +p=80,34 v=-39,-6 +p=78,15 v=41,-33 +p=13,5 v=-7,64 +p=28,80 v=-40,18 +p=43,15 v=40,-8 +p=98,68 v=-6,82 +p=16,32 v=86,11 +p=66,56 v=65,75 +p=84,58 v=48,-33 +p=65,22 v=-63,-84 +p=93,52 v=-75,72 +p=17,23 v=-68,-74 +p=77,65 v=-8,-66 +p=61,37 v=-42,69 +p=86,8 v=66,-20 +p=4,7 v=-44,70 +p=15,82 v=9,-23 +p=67,95 v=-95,57 +p=5,24 v=69,4 +p=58,96 v=99,46 +p=38,21 v=50,56 +p=60,76 v=-64,74 +p=29,0 v=-55,-25 +p=24,13 v=65,71 +p=48,76 v=-67,37 +p=19,39 v=-62,29 +p=33,58 v=6,28 +p=29,38 v=-98,-68 +p=50,55 v=-55,65 +p=5,77 v=91,71 +p=99,38 v=-8,77 +p=58,47 v=16,44 +p=43,45 v=12,-31 +p=21,102 v=45,-20 +p=79,99 v=83,-46 +p=52,56 v=83,16 +p=56,27 v=74,-68 +p=17,20 v=82,-77 +p=62,8 v=13,95 +p=58,24 v=-48,-2 +p=86,68 v=-3,-37 +p=2,24 v=63,-8 +p=1,81 v=-41,-88 +p=15,40 v=-25,66 +p=77,97 v=-23,5 +p=48,4 v=34,42 +p=41,74 v=-3,-91 +p=90,53 v=44,-76 +p=50,42 v=-79,13 +p=77,67 v=17,-53 +p=69,3 v=-8,3 +p=89,60 v=-8,19 +p=15,93 v=54,-32 +p=11,24 v=36,63 +p=22,6 v=-52,79 +p=11,101 v=-80,-79 +p=79,87 v=-8,77 +p=46,97 v=13,61 +p=90,25 v=-45,7 +p=46,95 v=13,-23 +p=89,62 v=-72,-94 +p=32,100 v=-49,58 +p=58,30 v=68,-40 +p=97,28 v=11,37 +p=76,61 v=-88,-31 +p=6,45 v=-7,38 +p=79,34 v=-69,-90 +p=96,76 v=57,-29 +p=9,100 v=-7,55 +p=59,23 v=50,43 +p=19,7 v=33,-74 +p=45,43 v=86,66 +p=50,19 v=56,33 +p=28,65 v=-9,-89 +p=14,2 v=-74,-33 +p=91,98 v=-50,99 +p=93,80 v=94,-48 +p=55,81 v=-7,-20 +p=57,100 v=-30,-39 +p=62,76 v=68,-38 +p=15,20 v=-77,-90 +p=37,33 v=89,60 +p=18,101 v=-3,20 +p=56,65 v=71,-60 +p=26,26 v=-58,-99 +p=33,75 v=-49,-91 +p=10,71 v=-56,-34 +p=25,83 v=39,-6 +p=13,17 v=37,61 +p=98,95 v=2,80 +p=26,83 v=-49,-35 +p=9,27 v=-72,46 +p=27,73 v=-9,40 +p=71,5 v=-94,-89 +p=60,6 v=-21,30 +p=10,68 v=-82,-31 +p=87,45 v=-38,66 +p=55,46 v=-68,-93 +p=34,77 v=55,-63 +p=5,51 v=38,31 +p=3,65 v=94,-16 +p=70,24 v=-48,10 +p=89,78 v=-75,40 +p=32,39 v=3,66 +p=28,94 v=40,-36 +p=91,38 v=-35,66 +p=29,66 v=-94,-50 +p=90,80 v=63,74 +p=15,31 v=-95,78 +p=50,19 v=7,-71 +p=58,64 v=-5,68 +p=16,64 v=65,-35 +p=46,93 v=-59,-22 +p=65,5 v=53,-33 +p=10,83 v=84,47 +p=23,12 v=49,-76 +p=58,14 v=-36,-80 +p=49,4 v=28,17 +p=62,43 v=-39,56 +p=61,11 v=-36,-52 +p=60,72 v=71,87 +p=44,56 v=-85,-76 +p=37,88 v=-3,27 +p=10,35 v=-90,-18 +p=14,40 v=30,-34 +p=22,95 v=27,95 +p=32,92 v=-37,2 +p=54,3 v=-54,-82 +p=98,5 v=11,-24 +p=39,76 v=37,90 +p=69,15 v=47,45 +p=56,40 v=-79,13 +p=80,41 v=63,32 +p=11,85 v=-7,74 +p=24,45 v=46,-50 +p=98,49 v=-38,-81 +p=26,45 v=-26,-80 +p=2,87 v=60,6 +p=93,74 v=26,-16 +p=24,70 v=-28,-85 +p=59,21 v=-63,62 +p=44,74 v=-7,14 +p=75,94 v=47,-86 +p=84,16 v=6,41 +p=17,79 v=33,-44 +p=5,72 v=18,58 +p=96,11 v=8,17 +p=34,48 v=62,14 +p=17,36 v=-42,-64 +p=87,55 v=93,65 +p=84,1 v=75,52 +p=30,74 v=-5,-33 +p=34,22 v=-32,-91 +p=85,94 v=-51,17 +p=37,90 v=-9,55 +p=9,85 v=-58,-37 +p=58,30 v=-2,72 +p=90,55 v=-53,-78 +p=34,48 v=-8,-33 +p=76,62 v=13,18 +p=16,21 v=76,-99 +p=42,71 v=46,68 +p=95,9 v=-75,64 +p=63,99 v=62,-39 +p=73,97 v=68,92 +p=79,17 v=-17,-39 +p=16,54 v=27,90 +p=48,34 v=9,-49 +p=88,72 v=51,59 +p=94,24 v=62,-43 +p=93,37 v=22,24 +p=90,80 v=-23,15 +p=10,98 v=88,80 +p=60,9 v=65,64 +p=44,80 v=21,68 +p=32,3 v=-95,83 +p=83,13 v=78,1 +p=20,49 v=70,-94 +p=51,82 v=-30,71 +p=31,12 v=61,86 +p=67,51 v=4,3 +p=19,25 v=67,76 +p=96,6 v=-99,-11 +p=35,13 v=-98,-42 +p=89,13 v=-29,95 +p=73,73 v=-60,87 +p=50,4 v=77,98 +p=5,86 v=61,49 +p=14,56 v=-7,40 +p=97,4 v=8,-64 +p=19,67 v=-37,-47 +p=61,23 v=-88,60 +p=23,11 v=-59,11 +p=11,16 v=33,-9 +p=90,29 v=-12,75 +p=81,62 v=-9,51 +p=34,60 v=58,84 +p=46,16 v=58,67 +p=47,12 v=74,67 +p=33,50 v=-31,87 +p=76,32 v=-52,-66 +p=56,39 v=-79,88 +p=92,18 v=-75,63 +p=9,17 v=36,70 +p=70,42 v=-11,74 +p=11,8 v=46,69 +p=30,19 v=64,-5 +p=78,42 v=-45,-46 +p=98,64 v=-99,-66 +p=83,31 v=14,-39 +p=97,66 v=73,96 +p=33,55 v=46,-38 +p=67,59 v=59,3 +p=19,34 v=82,-37 +p=9,77 v=45,46 +p=88,100 v=29,55 +p=77,2 v=-54,-42 +p=59,49 v=-79,-53 +p=22,83 v=94,99 +p=75,40 v=99,-15 +p=44,33 v=-28,2 +p=83,42 v=44,-65 +p=61,13 v=-7,-8 +p=39,25 v=-90,93 +p=18,11 v=32,98 +p=49,24 v=3,32 +p=75,30 v=99,85 +p=48,21 v=52,40 +p=5,23 v=91,-52 +p=40,41 v=92,-62 +p=14,51 v=-34,-41 +p=17,86 v=30,27 +p=77,15 v=-11,-80 +p=26,18 v=-77,73 +p=26,42 v=26,-25 +p=10,97 v=27,80 +p=24,37 v=-40,-65 +p=75,40 v=-81,-43 +p=6,19 v=57,38 +p=16,82 v=-74,-85 +p=87,81 v=-32,15 +p=36,91 v=53,-60 +p=25,32 v=6,-71 +p=11,69 v=-41,65 +p=31,81 v=-22,33 +p=7,51 v=-88,-93 +p=75,18 v=84,-30 +p=59,65 v=-33,-22 +p=53,19 v=87,-14 +p=53,98 v=-30,55 +p=69,88 v=-27,-12 +p=34,93 v=-37,52 +p=16,17 v=11,65 +p=65,53 v=-33,-28 +p=81,45 v=-66,-5 +p=97,92 v=18,-58 +p=88,18 v=72,1 +p=72,62 v=-34,64 +p=79,70 v=29,26 +p=64,66 v=7,75 +p=48,65 v=-89,-68 +p=47,18 v=-7,-64 +p=50,89 v=-73,-64 +p=13,91 v=33,29 +p=63,6 v=-45,42 +p=84,22 v=84,-71 +p=68,100 v=53,5 +p=89,66 v=-55,59 +p=66,19 v=-17,95 +p=49,16 v=61,-60 +p=16,15 v=58,70 +p=38,12 v=-37,47 +p=98,44 v=81,-75 +p=34,72 v=-3,28 +p=36,54 v=16,31 +p=48,63 v=55,6 +p=14,68 v=33,90 +p=18,67 v=-1,-53 +p=10,35 v=58,59 +p=13,67 v=57,-38 +p=48,69 v=83,90 +p=88,17 v=-75,26 +p=69,72 v=-98,54 +p=87,12 v=-48,16 +p=99,2 v=14,64 +p=71,97 v=47,39 +p=69,64 v=14,-3 +p=36,82 v=38,-93 +p=24,77 v=21,-10 +p=10,21 v=-34,-30 +p=34,44 v=-98,47 +p=38,9 v=-39,-74 +p=76,21 v=44,60 +p=66,75 v=-33,-66 +p=26,31 v=9,-40 diff --git a/2024/day14/main.go b/2024/day14/main.go new file mode 100644 index 0000000..840586f --- /dev/null +++ b/2024/day14/main.go @@ -0,0 +1,164 @@ +package main + +import ( + "fmt" + "math" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + fmt.Println() + part2(inp) +} + +func part1(inp []string) { + fmt.Println("# Part 1") + // Main Input size + sizeX, sizeY := 101, 103 + bots := parseInput(inp, sizeX, sizeY) + for i := 0; i < 100; i++ { + for j := range bots { + bots[j].Move() + } + } + for i := range bots { + fmt.Println(bots[i]) + } + qCnt := make([]int, 4) + for y := 0; y < sizeY; y++ { + for x := 0; x < sizeX; x++ { + if x == sizeX/2 || y == sizeY/2 { + fmt.Print(" ") + continue + } + bCnt := 0 + for i := range bots { + if x == bots[i].X && y == bots[i].Y { + bCnt++ + if x < sizeX/2 { + if y < sizeY/2 { + qCnt[0]++ + } else { + qCnt[2]++ + } + } else { + if y < sizeY/2 { + qCnt[1]++ + } else { + qCnt[3]++ + } + } + } + } + if bCnt > 0 { + fmt.Print(bCnt) + } else { + fmt.Print(".") + } + } + fmt.Println() + } + fmt.Println("Safety Factor:", (qCnt[0] * qCnt[1] * qCnt[2] * qCnt[3])) +} + +func part2(inp []string) { + fmt.Println("# Part 2") + // Main Input size + sizeX, sizeY := 101, 103 + bots := parseInput(inp, sizeX, sizeY) + printState := func() { + for y := 0; y < sizeY; y++ { + for x := 0; x < sizeX; x++ { + btCnt := 0 + for _, b := range bots { + if b.X == x && b.Y == y { + btCnt++ + } + } + if btCnt == 0 { + fmt.Print(".") + } else if btCnt < 10 { + fmt.Print(btCnt) + } else { + fmt.Print("#") + } + } + fmt.Println() + } + } + // My hypothesis: most bots will be close to each other for the answer + lowest := h.MAX_INT + var lowStdX, lowStdY float64 + for steps := 0; steps < 10000000; steps++ { + xs := make([]int, 0, len(bots)) + ys := make([]int, 0, len(bots)) + for j := range bots { + bots[j].Move() + xs = append(xs, bots[j].X) + ys = append(ys, bots[j].Y) + } + // After finding it, I added this to print it out :D + if steps == 7036 { + printState() + } + wrkX, wrkY := stdev(xs), stdev(ys) + if lowest == h.MAX_INT || (wrkX < lowStdX && wrkY < lowStdY) { + fmt.Println(lowest, ":", lowStdX, lowStdY) + lowStdX, lowStdY, lowest = wrkX, wrkY, steps + } + } + fmt.Println("Steps to Easter Egg:", lowest) +} + +func stdev(vals []int) float64 { + m := 0.0 + for _, w := range vals { + m += float64(w) + } + m /= float64(len(vals)) + v := 0.0 + for _, w := range vals { + diff := float64(w) - m + v += diff * diff + } + return math.Sqrt(v / float64(len(vals))) +} + +var botRaw = "p=%d,%d v=%d,%d" + +type Bot struct { + X, Y, Vx, Vy int + mX, mY int +} + +func NewBot(inp string) *Bot { + b := Bot{} + fmt.Sscanf(inp, botRaw, &b.X, &b.Y, &b.Vx, &b.Vy) + return &b +} + +func (b *Bot) Move() { + b.X = (b.X + b.Vx + b.mX) % b.mX + b.Y = (b.Y + b.Vy + b.mY) % b.mY +} + +func (b *Bot) Overlaps(bt *Bot) bool { + return b.X == bt.X && b.Y == bt.Y +} + +func (b Bot) String() string { + return fmt.Sprintf(botRaw, b.X, b.Y, b.Vx, b.Vy) +} + +func parseInput(inp []string, mX, mY int) []*Bot { + var ret []*Bot + for i := range inp { + b := NewBot(inp[i]) + b.mX, b.mY = mX, mY + ret = append(ret, b) + } + return ret +} diff --git a/2024/day14/testinput b/2024/day14/testinput new file mode 100644 index 0000000..2455da4 --- /dev/null +++ b/2024/day14/testinput @@ -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