2021 Day 7 Complete!
This commit is contained in:
parent
dba8cc0e0f
commit
5b7a73e883
1
2021/day07/input
Normal file
1
2021/day07/input
Normal file
@ -0,0 +1 @@
|
||||
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,334,7,6,1172,110,184,1322,1248,516,0,259,331,1291,51,753,878,27,664,859,391,41,43,317,884,529,168,502,1236,908,1203,676,349,117,268,364,668,256,216,435,296,568,298,600,53,775,3,782,387,305,1648,254,1477,488,234,349,1410,55,456,202,394,29,578,400,188,1172,306,375,320,561,167,1078,643,279,339,12,1004,290,26,49,359,139,218,129,139,742,239,290,471,1899,1446,1046,657,895,90,196,467,1105,723,269,638,72,363,199,704,769,658,124,1001,1160,1150,806,154,407,238,155,499,1170,779,156,1041,728,124,349,397,916,24,1079,1275,222,1852,180,24,126,475,228,464,5,133,130,1119,108,895,396,1285,670,52,107,714,58,1143,714,144,1193,1754,157,802,1,1000,1001,1116,823,256,746,16,281,341,1363,233,26,46,474,91,1035,422,681,6,316,1564,31,162,194,116,904,1223,192,1552,74,1282,398,776,343,187,45,193,1311,111,161,27,660,1283,124,215,331,158,35,65,852,290,1199,285,555,630,309,375,8,301,32,706,74,722,303,474,857,12,211,54,391,171,66,1373,931,1272,294,619,470,75,195,40,386,98,127,332,183,741,1502,383,714,148,213,1053,130,491,66,1010,820,48,867,1462,901,866,1110,171,1095,147,206,1220,1242,839,1187,90,918,399,262,821,52,159,3,750,1464,1772,128,43,888,225,488,1257,281,597,321,281,1240,821,205,384,66,331,23,241,327,348,122,218,867,114,246,19,698,59,1218,28,61,238,135,773,64,54,401,297,1148,465,159,1339,45,650,702,861,0,340,5,129,80,171,55,135,1021,1420,22,330,199,0,288,1318,725,147,233,586,1530,268,512,19,94,497,389,530,1852,102,1175,38,1053,551,971,65,1254,32,409,756,531,2,758,678,351,9,788,590,176,1367,389,495,1608,489,75,341,198,68,27,3,109,635,457,381,571,978,16,359,1474,668,80,133,1337,608,1087,96,690,9,375,1012,1016,173,71,1482,64,1334,231,439,670,1092,1327,1637,1971,174,174,367,594,827,1497,296,128,13,348,780,1682,202,1060,191,1602,83,1437,91,519,601,242,179,70,158,84,220,869,1363,1119,648,185,162,961,281,479,621,13,1303,208,1117,373,141,995,584,82,994,1032,899,654,762,606,532,1108,277,89,515,422,696,275,1693,93,864,146,56,352,182,439,6,331,39,83,23,1880,724,87,59,129,889,9,1598,57,215,876,25,837,323,403,1229,761,34,1014,7,65,3,238,588,327,1307,1069,1272,166,162,1613,92,169,16,19,272,613,1069,861,452,1070,330,667,660,49,1178,183,600,1038,290,857,1505,1029,146,504,284,175,73,485,909,615,251,176,843,394,934,1056,71,290,199,258,67,4,404,912,10,1564,807,865,6,550,1022,506,988,195,480,13,981,18,220,156,341,491,220,326,480,1251,235,200,1398,179,196,187,316,1299,997,504,104,189,419,347,458,172,48,744,88,56,183,233,337,1107,475,337,54,753,49,33,612,301,157,73,261,1122,304,352,264,265,879,13,96,1153,262,40,653,196,326,1523,394,79,297,10,1326,21,411,789,553,4,712,123,392,286,671,141,28,168,544,9,537,490,396,1210,1800,354,228,35,1217,784,223,551,290,386,106,152,309,2,777,558,813,380,184,187,629,841,233,1027,823,1104,205,1528,981,197,674,517,1537,512,175,1117,730,397,803,1380,216,611,142,85,680,35,22,598,1530,44,236,265,358,1227,1189,1831,886,107,314,325,219,724,619,862,0,146,1358,172,94,53,225,648,152,583,666,927,99,394,150,113,959,528,1233,65,1408,181,114,38,550,282,742,936,1278,748,178,344,180,375,1003,97,623,98,309,248,277,902,466,1298,878,804,2,697,509,809,847,133,1215,573,1497,255,99,58,605,841,849,22,120,409,339,937,1333,943,823,1146,435,454,264,26,25,848,225,139,208,1125,12,242,241,2,382,258,579,5,1194,402,1069,874,1302,191,307,313,643,1080,505,1032,1099,267,160,821,520,55,30,362,77,270,648,177,805,977,464,872,326,1176,102,47,1254,92,217,399,184,283,770,282,1488,456,691,508,243,856,112,294,948,938,27,91,262,549,165,985,21,968,380,143,118,365,1562,318,610,1167,112,750,279,212,710,331,1022,1143,1566,17,357,672,1391,877,1051,105,21,219,1392,78,857,223,676,350,117,82,703,460,1332,129,235,25,133,195,474,791,223,170,1482,139,63,967,286,73,343,230,551,1035,44,1428,1589,542,592,7
|
79
2021/day07/main.go
Normal file
79
2021/day07/main.go
Normal file
@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := strings.Split(h.StdinToString(), ",")
|
||||
part1(inp)
|
||||
fmt.Println()
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
func part1(inp []string) {
|
||||
fmt.Println("# Part 1")
|
||||
var crabs []int
|
||||
min := h.MAX_INT
|
||||
max := h.MIN_INT
|
||||
crabMap := make(map[int]int)
|
||||
for i := range inp {
|
||||
c := h.Atoi(inp[i])
|
||||
if c < min {
|
||||
min = c
|
||||
}
|
||||
if c > max {
|
||||
max = c
|
||||
}
|
||||
crabMap[c] = crabMap[c] + 1
|
||||
crabs = append(crabs, c)
|
||||
}
|
||||
fuelMap := make(map[int]int)
|
||||
var minFuelMov int
|
||||
minFuelVal := h.MAX_INT
|
||||
for i := min; i <= max; i++ {
|
||||
for c := range crabMap {
|
||||
fuelMap[i] += crabMap[c] * h.Abs(i-c)
|
||||
}
|
||||
if fuelMap[i] < minFuelVal {
|
||||
minFuelMov = i
|
||||
minFuelVal = fuelMap[i]
|
||||
}
|
||||
}
|
||||
fmt.Println("Minimum Fuel Requirement:", minFuelVal, "at", minFuelMov)
|
||||
}
|
||||
|
||||
func part2(inp []string) {
|
||||
fmt.Println("# Part 2")
|
||||
var crabs []int
|
||||
min := h.MAX_INT
|
||||
max := h.MIN_INT
|
||||
crabMap := make(map[int]int)
|
||||
for i := range inp {
|
||||
c := h.Atoi(inp[i])
|
||||
if c < min {
|
||||
min = c
|
||||
}
|
||||
if c > max {
|
||||
max = c
|
||||
}
|
||||
crabMap[c] = crabMap[c] + 1
|
||||
crabs = append(crabs, c)
|
||||
}
|
||||
fuelMap := make(map[int]int)
|
||||
var minFuelMov int
|
||||
minFuelVal := h.MAX_INT
|
||||
for i := min; i <= max; i++ {
|
||||
for c := range crabMap {
|
||||
fuelMap[i] += crabMap[c] * h.Fact(h.Abs(i-c))
|
||||
}
|
||||
if fuelMap[i] < minFuelVal {
|
||||
minFuelMov = i
|
||||
minFuelVal = fuelMap[i]
|
||||
}
|
||||
}
|
||||
fmt.Println("Minimum Fuel Requirement:", minFuelVal, "at", minFuelMov)
|
||||
}
|
1
2021/day07/testinput
Normal file
1
2021/day07/testinput
Normal file
@ -0,0 +1 @@
|
||||
16,1,2,0,4,2,7,1,2,14
|
@ -30,6 +30,23 @@ const (
|
||||
SHRUG = "¯\\_(ツ)_/¯"
|
||||
)
|
||||
|
||||
// Fact returns the factorial of the given int
|
||||
func Fact(x int) int {
|
||||
var ret int
|
||||
for i := 0; i <= x; i++ {
|
||||
ret += i
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Abs returns the absolute value of the given int
|
||||
func Abs(x int) int {
|
||||
if x < 0 {
|
||||
return x * -1
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// Gcd Finds the greatest common denominator
|
||||
func Gcd(x, y int) int {
|
||||
for y != 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user