diff --git a/2021/day12/input.txt b/2021/day12/input.txt new file mode 100644 index 0000000..4c6c3d1 --- /dev/null +++ b/2021/day12/input.txt @@ -0,0 +1,24 @@ +yb-start +de-vd +rj-yb +rj-VP +OC-de +MU-de +end-DN +vd-end +WK-vd +rj-de +DN-vd +start-VP +DN-yb +vd-MU +DN-rj +de-VP +yb-OC +start-rj +oa-MU +yb-de +oa-VP +jv-MU +yb-MU +end-OC \ No newline at end of file diff --git a/2021/day12/main.go b/2021/day12/main.go new file mode 100644 index 0000000..b8fc05c --- /dev/null +++ b/2021/day12/main.go @@ -0,0 +1,139 @@ +package main + +import ( + "fmt" + "strings" + "unicode" + + "github.com/mbonnafon/AdventOfCode/helpers" +) + +type Node struct { + prev []*Node + next []*Node + key string +} + +func (n Node) isBigCave() bool { + return unicode.IsUpper(rune(n.key[0])) +} + +type List struct { + head *Node + tail *Node + elems map[string]*Node +} + +func newList(head, tail string) List { + headNode := &Node{key: "start"} + tailNode := &Node{key: "end"} + elems := map[string]*Node{ + head: headNode, + tail: tailNode, + } + return List{ + head: headNode, + tail: tailNode, + elems: elems, + } +} + +func (l *List) Insert(curr, next string) { + var currNode *Node + if l.elems[curr] == nil { + currNode = &Node{key: curr} + l.elems[curr] = currNode + } else { + currNode = l.elems[curr] + } + + var nextNode *Node + if l.elems[next] == nil { + nextNode = &Node{key: next} + l.elems[next] = nextNode + } else { + nextNode = l.elems[next] + } + + currNode.next = append(currNode.next, nextNode) + nextNode.prev = append(nextNode.prev, currNode) +} + +func (l List) visualize(curr string) { + node := l.elems[curr] + for _, n := range node.next { + fmt.Printf("%s => %s (%s)\n", curr, n.key, n.isBigCave()) + l.visualize(n.key) + } +} + +func main() { + lines, _ := helpers.StringLines("./input.txt") + fmt.Println("Part 1. :", pt1(lines)) + fmt.Println("Part 2. :", pt2(lines)) +} + +func pt1(lines []string) int { + return 0 + list := newList("start", "end") + for _, v := range lines { + splitted := strings.Split(v, "-") + list.Insert(splitted[0], splitted[1]) + } + + path := make(map[string]int) + for k, v := range list.elems { + if !v.isBigCave() { + path[k] = 1 + } + } + return list.doPath(*list.head, false, path) +} + +func pt2(lines []string) int { + list := newList("start", "end") + for _, v := range lines { + splitted := strings.Split(v, "-") + list.Insert(splitted[0], splitted[1]) + } + + path := make(map[string]int) + return list.doPath(*list.head, false, path) +} + +func (l *List) doPath(current Node, alreadyVisitedTwice bool, visited map[string]int) (count int) { + newlyVisited := make(map[string]int) + for k, v := range visited { + newlyVisited[k] = v + } + + if !current.isBigCave() && newlyVisited[current.key] > 0 { + alreadyVisitedTwice = true + } + newlyVisited[current.key]++ + + nextNodes := make(map[*Node]bool) + for _, n := range current.next { + nextNodes[n] = true + } + for _, n := range current.prev { + nextNodes[n] = true + + } + + for n := range nextNodes { + if n.key == l.head.key { + continue + } + if n.key == l.tail.key { + count++ + continue + } + if !n.isBigCave() && alreadyVisitedTwice && newlyVisited[n.key] > 0 { + continue + } + count += l.doPath(*n, alreadyVisitedTwice, newlyVisited) + } + + return +} diff --git a/2021/day13/input.txt b/2021/day13/input.txt new file mode 100644 index 0000000..82b90d8 --- /dev/null +++ b/2021/day13/input.txt @@ -0,0 +1,910 @@ +724,201 +596,511 +883,560 +637,47 +869,537 +251,505 +851,618 +980,544 +818,661 +1093,390 +641,95 +1006,621 +447,434 +820,865 +475,847 +788,72 +1066,829 +1303,882 +1165,696 +164,229 +202,333 +244,485 +261,840 +1116,581 +976,513 +550,635 +1151,728 +1211,570 +228,422 +223,539 +406,369 +306,758 +1247,894 +760,859 +826,737 +340,637 +1293,47 +99,187 +264,553 +927,523 +940,189 +586,313 +525,67 +296,581 +353,694 +55,264 +582,491 +264,154 +512,394 +1101,225 +555,187 +1021,535 +805,772 +783,891 +403,402 +1243,94 +495,593 +1031,695 +13,472 +705,637 +459,618 +939,322 +251,82 +899,523 +301,131 +873,891 +880,374 +894,737 +306,593 +1257,280 +88,885 +621,655 +38,640 +514,539 +1222,885 +1257,135 +1191,303 +1034,715 +649,738 +719,103 +1131,367 +907,402 +169,297 +482,408 +977,866 +1280,432 +1087,803 +473,810 +182,546 +785,155 +1257,871 +1091,483 +310,52 +758,712 +870,715 +1211,772 +1173,40 +73,460 +117,312 +228,243 +918,822 +475,50 +765,3 +1113,410 +127,544 +880,422 +291,675 +1088,187 +957,506 +591,791 +566,437 +1178,593 +159,728 +1237,684 +490,665 +929,311 +641,267 +966,766 +1081,253 +735,649 +857,750 +999,367 +989,221 +0,185 +445,395 +108,168 +586,761 +1203,595 +1115,89 +838,665 +487,301 +289,647 +903,133 +251,53 +887,800 +1093,443 +775,684 +1026,481 +601,311 +661,476 +1198,627 +0,753 +1231,786 +410,821 +358,742 +1179,780 +763,435 +1077,133 +927,182 +681,144 +497,597 +1088,483 +649,476 +1202,415 +440,583 +1198,14 +104,775 +619,443 +410,660 +1034,656 +115,575 +601,728 +338,222 +84,301 +1111,891 +1211,122 +653,614 +1290,282 +209,582 +440,43 +929,583 +53,166 +1004,301 +263,572 +818,233 +1183,319 +228,472 +1257,166 +656,329 +53,135 +669,267 +815,593 +1111,767 +802,576 +1226,660 +1215,800 +341,218 +181,422 +1046,565 +159,280 +119,303 +1027,303 +147,417 +571,859 +1173,261 +1054,885 +239,302 +228,131 +85,217 +1231,226 +137,709 +21,131 +1290,612 +718,845 +837,782 +164,141 +632,467 +749,313 +525,379 +858,561 +851,786 +1228,857 +33,239 +868,670 +1183,3 +248,548 +328,345 +898,436 +438,893 +823,301 +820,589 +937,771 +875,58 +790,617 +64,504 +95,675 +508,576 +729,434 +186,91 +1092,392 +403,166 +87,514 +1261,114 +1141,597 +217,390 +689,151 +457,515 +585,805 +1131,360 +1212,472 +669,464 +318,682 +485,73 +1154,644 +738,37 +1297,870 +361,207 +932,238 +880,264 +376,738 +87,380 +174,94 +411,819 +845,633 +1288,721 +189,261 +1294,374 +72,267 +649,642 +156,644 +517,406 +705,562 +1059,614 +813,856 +623,702 +619,462 +403,728 +344,52 +1178,634 +1275,238 +656,47 +1176,169 +244,821 +328,121 +1092,255 +872,1 +845,185 +251,707 +79,674 +659,705 +1048,520 +1121,709 +107,768 +427,334 +1257,311 +112,14 +997,351 +552,429 +1064,159 +1114,453 +398,306 +1208,182 +187,175 +654,719 +1225,665 +735,245 +1078,245 +544,598 +691,238 +1168,164 +264,740 +855,833 +1260,267 +661,156 +333,194 +1212,870 +555,803 +445,11 +1275,443 +1067,35 +783,787 +785,379 +162,141 +661,866 +873,3 +321,476 +1136,800 +673,47 +723,800 +1129,84 +127,891 +927,371 +781,647 +1099,247 +1275,3 +691,891 +218,639 +731,626 +1093,504 +550,735 +1019,880 +477,142 +147,508 +392,822 +542,432 +209,235 +410,234 +895,113 +107,798 +7,23 +119,591 +709,364 +1059,340 +1198,148 +179,360 +226,49 +43,98 +195,640 +452,561 +49,52 +359,761 +1307,749 +1235,392 +556,355 +547,58 +790,725 +8,683 +1261,724 +301,185 +69,89 +545,339 +957,759 +509,84 +1235,564 +663,12 +1017,743 +739,332 +1213,392 +1067,444 +291,616 +969,851 +353,819 +669,95 +902,436 +127,575 +887,143 +671,79 +796,539 +865,11 +289,247 +465,709 +605,724 +194,581 +1223,788 +810,771 +932,208 +333,225 +1308,96 +1257,23 +709,311 +87,123 +845,807 +845,574 +552,630 +1226,234 +16,374 +1247,0 +381,311 +957,11 +393,392 +545,219 +523,332 +820,661 +472,229 +1079,542 +99,570 +311,847 +1130,500 +519,135 +207,434 +435,235 +373,499 +731,368 +800,348 +373,771 +904,861 +687,192 +1297,24 +681,131 +545,555 +705,170 +1009,200 +63,828 +1193,582 +211,647 +982,495 +1206,159 +1014,581 +284,481 +1294,394 +5,156 +641,627 +744,885 +900,212 +557,887 +908,289 +786,856 +785,515 +527,61 +73,684 +698,637 +522,294 +79,226 +843,175 +754,315 +798,606 +25,730 +790,169 +947,686 +509,646 +157,38 +994,451 +31,702 +622,453 +390,742 +256,558 +1274,448 +950,833 +619,238 +723,94 +67,94 +490,383 +447,658 +654,686 +407,313 +1054,9 +982,175 +1215,766 +1198,880 +1054,306 +1305,156 +407,245 +328,208 +273,787 +693,807 +939,751 +232,245 +1047,770 +1063,133 +87,828 +1285,409 +787,803 +1056,715 +654,701 +1201,56 +865,728 +1000,318 +546,31 +671,63 +490,29 +863,658 +234,841 +940,481 +7,460 +1213,495 +411,75 +1280,856 +949,599 +743,106 +301,435 +390,497 +416,157 +411,523 +1057,12 +907,492 +1138,486 +132,301 +383,571 +833,304 +271,323 +788,238 +726,112 +30,432 +1288,313 +688,441 +605,649 +982,345 +174,652 +527,721 +284,257 +927,571 +623,254 +1082,243 +977,669 +472,665 +705,724 +947,768 +559,800 +951,133 +207,460 +279,247 +360,833 +1223,514 +63,0 +182,5 +522,520 +17,495 +555,324 +390,397 +827,882 +107,683 +430,856 +1150,656 +579,788 +769,170 +164,677 +1082,651 +883,894 +982,656 +641,198 +576,320 +863,210 +857,144 +984,549 +672,766 +67,800 +35,443 +412,682 +326,529 +304,621 +556,539 +117,669 +918,72 +654,47 +219,388 +654,753 +1246,56 +654,641 +1250,848 +562,681 +72,403 +1228,305 +641,422 +758,856 +1082,472 +701,256 +1077,761 +820,511 +755,324 +658,509 +960,733 +801,10 +769,378 +430,38 +647,12 +129,291 +201,750 +522,374 +837,422 +75,564 +977,225 +609,256 +855,350 +1128,5 +1245,495 +1240,530 +591,551 +555,154 +45,275 +381,359 +922,253 +1026,257 +251,280 +629,798 +739,371 +492,708 +719,551 +508,318 +678,427 +1109,302 +754,539 +884,472 +705,245 +654,453 +256,530 +1184,793 +79,201 +731,788 +1076,53 +430,264 +999,54 +691,432 +837,810 +669,696 +181,810 +132,520 +885,641 +117,480 +729,460 +437,891 +242,212 +1263,507 +353,75 +316,787 +179,24 +967,815 +490,305 +214,583 +801,646 +1278,441 +1163,392 +485,521 +845,709 +104,159 +276,715 +85,229 +522,145 +214,851 +377,257 +490,661 +1285,485 +278,828 +492,661 +363,686 +709,82 +970,705 +535,882 +256,364 +131,114 +427,635 +440,179 +890,681 +1029,571 +360,61 +78,413 +746,179 +1128,889 +1288,173 +1213,47 +321,700 +137,40 +720,315 +1241,89 +137,807 +99,539 +104,287 +527,891 +415,116 +229,253 +574,605 +276,686 +329,838 +1309,728 +95,667 +899,819 +561,805 +669,430 +1206,735 +1006,313 +231,542 +438,669 +1211,355 +947,880 +783,168 +93,581 +415,302 +195,254 +231,784 +442,670 +872,225 +800,98 +817,58 +1079,94 +341,340 +131,780 +426,315 +1211,826 +1247,707 +363,219 +406,817 +820,383 +465,633 +547,459 +334,605 +1115,254 +927,164 +5,866 +239,778 +97,847 +271,730 +75,392 +438,225 +326,686 +570,301 +564,179 +195,192 +1225,217 +751,367 +477,116 +1116,805 +801,84 +247,674 +189,126 +1101,499 +1161,570 +989,418 +400,491 +810,123 +468,397 +1235,523 +803,607 +247,133 +0,847 +1146,305 +884,315 +164,569 +913,313 +412,212 +112,627 +209,312 +711,523 +427,894 +1235,140 +131,803 +1232,413 +199,891 +49,724 +947,14 +313,351 +898,10 +1277,655 +887,334 +982,719 +1230,829 +316,222 +209,728 +592,845 +1285,803 +490,589 +903,245 +283,591 +1197,516 +197,827 +619,451 +509,884 +1297,43 +1141,73 +1131,63 +584,530 +820,29 +107,836 +251,841 +529,247 +400,715 +607,387 +301,759 +1048,374 +227,707 +281,771 +711,607 +1049,392 +304,313 +1235,626 +209,883 +452,333 +30,408 +403,492 +510,98 +933,68 +161,58 +1242,693 +251,389 +1191,322 +1131,527 +1213,439 +586,201 +904,369 +783,319 +929,359 +838,348 +878,411 +109,443 +1082,131 +350,621 +1048,889 +326,208 +763,96 +132,593 +84,350 +1059,82 +678,467 +1091,836 +1235,371 +582,715 +641,464 +55,795 +293,687 +723,336 +341,472 +371,322 +555,602 +840,500 +199,833 +455,833 +169,73 +97,392 +783,385 +623,640 +1111,840 +765,891 +1046,553 +383,164 +907,812 +1131,870 +1067,892 +1129,472 +246,436 +644,670 +624,38 +353,135 +666,227 +835,399 +112,148 +247,859 +383,99 +535,684 +338,555 +552,805 +594,17 +880,472 +801,436 +997,472 +559,667 +341,676 +247,35 +1193,225 +1,56 +545,3 +1149,58 +455,710 +753,887 +687,640 +1220,379 +1115,724 +1203,574 +465,320 +84,772 +1273,359 +873,339 +783,3 +199,54 +1193,669 +264,329 +246,513 +33,655 +1293,495 +432,187 +601,871 +803,47 +825,373 +259,110 +586,245 +412,436 +927,347 +345,775 +970,257 +870,311 + +fold along x=655 +fold along y=447 +fold along x=327 +fold along y=223 +fold along x=163 +fold along y=111 +fold along x=81 +fold along y=55 +fold along x=40 +fold along y=27 +fold along y=13 +fold along y=6 \ No newline at end of file diff --git a/2021/day13/main.go b/2021/day13/main.go new file mode 100644 index 0000000..0d8fc53 --- /dev/null +++ b/2021/day13/main.go @@ -0,0 +1,157 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/mbonnafon/AdventOfCode/helpers" +) + +type coord helpers.Coord + +type Fold struct { + order string + size int +} + +type Grid struct { + coords []coord + foldCoords []Fold + Xmax int + Ymax int +} + +func (g *Grid) calcMax() { + var maxX, maxY int + for _, c := range g.coords { + if c.X > maxX { + maxX = c.X + } + if c.Y > maxY { + maxY = c.Y + } + } + g.Xmax = maxX + g.Ymax = maxY +} + +func newGrid(lines []string) Grid { + g := Grid{} + + var i int + for i = 0; i < len(lines); i++ { + if lines[i] == "" { + i++ + break + } + c := strings.Split(lines[i], ",") + g.coords = append(g.coords, coord{X: helpers.ToInt(c[0]), Y: helpers.ToInt(c[1])}) + } + + var foldCoords []Fold + for i = i; i < len(lines); i++ { + var matched string + r := strings.NewReader(lines[i]) + fmt.Fscanf(r, "fold along %s", &matched) + m := strings.Split(matched, "=") + foldCoords = append(foldCoords, Fold{order: m[0], size: helpers.ToInt(m[1])}) + } + g.foldCoords = foldCoords + g.calcMax() + return g +} + +func main() { + lines, _ := helpers.StringLines("./input.txt") + fmt.Println("Part 1. :", pt1(newGrid(lines))) + fmt.Println("Part 2. :", pt2(newGrid(lines))) +} + +func pt1(g Grid) int { + size := g.foldCoords[0].size + if g.foldCoords[0].order == "x" { + g.foldX(size) + } else { + g.foldY(size) + } + count := make(map[coord]bool) + for _, v := range g.coords { + count[v] = true + } + return len(count) +} + +func pt2(g Grid) int { + for _, f := range g.foldCoords { + size := f.size + if f.order == "x" { + g.foldX(size) + } else { + g.foldY(size) + } + } + display := make([][]bool, g.Ymax) + for _, v := range g.coords { + if display[v.Y] == nil { + display[v.Y] = make([]bool, g.Xmax) + } + display[v.Y][v.X] = true + } + for i := 0; i < g.Ymax; i++ { + for j := 0; j < g.Xmax; j++ { + if display[i][j] == true { + fmt.Print("#") + } else { + fmt.Print(" ") + } + } + fmt.Println() + } + count := make(map[coord]bool) + for _, v := range g.coords { + count[v] = true + } + return len(count) +} + +func (g *Grid) foldX(x int) { + var newCoords []coord + for _, c := range g.coords { + if c.X > x { + newX := x - (c.X - x) + if newX < 0 { + continue + } + newC := coord{ + X: x - (c.X - x), + Y: c.Y, + } + newCoords = append(newCoords, newC) + continue + } + newCoords = append(newCoords, c) + } + g.coords = newCoords + g.Xmax = x +} + +func (g *Grid) foldY(y int) { + var newCoords []coord + for _, c := range g.coords { + if c.Y > y { + newY := y - (c.Y - y) + if newY < 0 { + continue + } + newC := coord{ + X: c.X, + Y: y - (c.Y - y), + } + newCoords = append(newCoords, newC) + continue + } + newCoords = append(newCoords, c) + } + g.coords = newCoords + g.Ymax = y +} diff --git a/2021/day14/input.txt b/2021/day14/input.txt new file mode 100644 index 0000000..499e218 --- /dev/null +++ b/2021/day14/input.txt @@ -0,0 +1,102 @@ +VCOPVNKPFOOVPVSBKCOF + +NO -> K +PO -> B +HS -> B +FP -> V +KN -> S +HV -> S +KC -> S +CS -> B +KB -> V +OB -> V +HN -> S +OK -> N +PC -> H +OO -> P +HF -> S +CB -> C +SB -> V +FN -> B +PH -> K +KH -> P +NB -> F +KF -> P +FK -> N +FB -> P +FO -> H +CV -> V +CN -> P +BN -> N +SC -> N +PB -> K +VS -> N +BP -> P +CK -> O +PS -> N +PF -> H +HB -> S +VN -> V +OS -> V +OC -> O +BB -> F +SK -> S +NF -> F +FS -> S +SN -> N +FC -> S +BH -> N +HP -> C +VK -> F +CC -> N +SV -> H +SO -> F +HH -> C +PK -> P +NV -> B +KS -> H +NP -> H +VO -> C +BK -> V +VV -> P +HK -> B +CF -> B +BF -> O +OV -> B +OH -> C +PP -> S +SP -> S +CH -> B +OF -> F +NK -> F +FV -> F +KP -> O +OP -> O +SS -> P +CP -> H +BO -> O +KK -> F +HC -> N +KO -> V +CO -> F +NC -> P +ON -> P +KV -> C +BV -> K +HO -> F +PV -> H +VC -> O +NH -> B +PN -> H +VP -> O +NS -> N +NN -> S +BS -> H +SH -> P +VB -> V +VH -> O +FH -> K +FF -> H +SF -> N +BC -> H +VF -> P \ No newline at end of file diff --git a/2021/day14/main.go b/2021/day14/main.go new file mode 100644 index 0000000..0a7f303 --- /dev/null +++ b/2021/day14/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/mbonnafon/AdventOfCode/helpers" +) + +func main() { + lines, _ := helpers.StringLines("./input.txt") + initial := lines[0] + transform := make(map[string]string) + for i := 2; i < len(lines); i++ { + v := strings.Split(lines[i], " -> ") + transform[v[0]] = v[1] + } + fmt.Println("Part 1. :", pt1(initial, transform)) + fmt.Println("Part 2. :", pt2(initial, transform)) +} + +func pt1(initial string, transform map[string]string) int { + for i := 0; i < 10; i++ { + initial = transformS(initial, transform) + } + return max(initial) - min(initial) +} + +func pt2(initial string, transform map[string]string) int { + occurences := make(map[string]int) + for i := 0; i < len(initial)-1; i++ { + v := string(initial[i]) + string(initial[i+1]) + occurences[v]++ + } + for i := 0; i < 40; i++ { + occurences = transformN(occurences, transform) + } + letters := make(map[string]int) + for k, v := range occurences { + letters[string(k[0])] += v + } + letters[string(initial[len(initial)-1])]++ + + var max, min int + for _, v := range letters { + if v > max { + max = v + } + if min == 0 { + min = v + } + if v < min { + min = v + } + } + return max - min +} + +func transformN(occurences map[string]int, transform map[string]string) map[string]int { + new := make(map[string]int) + for k, v := range occurences { + v1 := string(k[0]) + transform[k] + v2 := transform[k] + string(k[1]) + new[v1] += v + new[v2] += v + } + return new +} + +func transformS(initial string, transform map[string]string) string { + var new string + max := len(initial) + for i := 0; i < max; i++ { + if i == max-1 { + new = new + string(initial[i]) + break + } + v := string(initial[i]) + string(initial[i+1]) + new = new + string(initial[i]) + transform[v] + } + return new +} + +func max(s string) (max int) { + occurences := make(map[string]int) + for _, v := range s { + occurences[string(v)]++ + } + for _, v := range occurences { + if v > max { + max = v + } + } + return +} + +func min(s string) (min int) { + occurences := make(map[string]int) + for _, v := range s { + occurences[string(v)]++ + } + fmt.Println(occurences) + for _, v := range occurences { + if min == 0 { + min = v + } + if v < min { + min = v + } + } + return +} diff --git a/2021/day15/input.txt b/2021/day15/input.txt new file mode 100644 index 0000000..1789a66 --- /dev/null +++ b/2021/day15/input.txt @@ -0,0 +1,100 @@ +8656175343119439947311919179279377257689197682985752287249881788527977345924133329967132199899199876 +4281288396984115627338957853913949146812568775798196289991176529889498798259196976277683981565984526 +8169838335918963371547591999342191683995972519179929683978126291855319511168245582651697938949414963 +9911589184911198759466719191189995157885898771177913669999436998997781742478168921196176418888127932 +2332856729917591158935782911513226531793881992469468248251991718456991551657621249229316797949388969 +6488738222981127824819626661531168733698299313631126969162927725778914183239351484699921454131481322 +3456499223985116399353928681116846939429849384129296119943791119497551946593566977823982927391143399 +9297612841849646148511299569971197927971682598351883892842936977227795411959211782251399118676896328 +1685688918999598249622886442487951289969917848948815999749154287969297396481891239821819247914959943 +2855192597719945162116189119381795158862162959133647579913886639586438893695967226719685616432291318 +3893139873964939791913261811173979825213968972939996181992593113695976322782892995939891783998623789 +5964671772545917912328949994999514157618987654288269789931394913375316999489275494179994919117119365 +3216339828898396212733674969295762442389789119366989996931919596282992864471192938639162947919972647 +7879826444123912118829914949989194219763533694839431198212319893181151696157671388992581982743988967 +7876122492176935838133394839673532581399474449569117478121989273123412313151952891988698552384251637 +8786999269197821985731819155537259938991261919979365751539876866146174376911518943778479997131942722 +7239919942299391891898171771589178983916281118889294494829292752139999754571391999826947791975394972 +2727617215141959982544247383138498917281833932371546394891963994434638187617528821379728933335772342 +6712949984729176138535147942577311588399869767114795759883948915177671474313191789814718292236422124 +8689792116597916131779139899373471282726812338914773516383771158691551851991689929199689197212298649 +6228175894997691471313338136819712699979449476188196729899628851391971728199959981585798691881191415 +9491157288112631922166642933446197292729817888649185515198768537925149995158353829229269894798938589 +5199389142294542461535132814487861987422485947698739199748986652229272998164982399192268248929567161 +6881119581991538187539159493621999896486112182779617795198955921614753699796171837819179999654231813 +6996595113784151975521879415898217119639999975184518599369743127684295298268259495226623211591871888 +1779949977648145993713949919359399998812143513534984334127763318649997689919849157137418761137512977 +9223292971993981422528813498119387921238194638215641911218326517839287886478991147999899894724396962 +7451983891767995938972123465939918261682281929689989464297789895588752221375859586787938391672994381 +1579954176878141996516584483611183926131694919877929897576151469657864935819598781814826881644187861 +8934595321937914584995177237653199951582515953621782173159453287575529819242531191989113518599339361 +3214791137819868237121897913928328682928239268681975567994149694512799167598489194267554981611157817 +3161289322443822496458312797912957538349962328948955764669152944278622928929944477635195993465821714 +3769985892295265479971729196428585411214871889139731131516448815999122871374996159138497416978598848 +4926877381218811127939655759733739111889455359949289979523168797571871445191441719179859376969393872 +7159871151274214979637977982939997337182139971896511115912187982698278842961545125121242636339621282 +8423118867997391616931374681582965659184916625321536161483641999294114251928796446618889979683993218 +6824788915976897879897933391998341399228914119912289149511515799142181679965255395897688149876629889 +4794395837416911945432167619882718499181629123717219977896911277339851349162195299228963994667691947 +1465122112768881153591399679274839985297692314947884169719151181986711822112796246416415955915718319 +7114797833579933611249299912348294873921288555119486979959982462111791467394161781467316779796883775 +9767795467854144194712742294766968549329117618283754959873952489784997597669593796889571883816748771 +1759974879458746414925558868197241214751687661841649999221178613679929261984417289292556869111324941 +9557955429999672993612951916598187741611893112948813816999484953994151249931191942493691497559992158 +1469791219499218437828194894539893191199115998162227659757698173867329816328599398544492677817272998 +6696472832532492295279572315976884291727999732161858916985514872395367168971969865959582923387918951 +9722652752119479169481198119314728131429449798379179119171791999189878174319864498112912224658495939 +9581794449119116165418178711351481129242636919987948291451161695541253977174231393582734959682898495 +4757727865924728111221629983989222587789999361791192917315287219922119718827676188689966872358484669 +1982319969915242121935864989286462118395187369193933592791414764965279817872197863615512262358899223 +1811379712999278717898361849316989959114612857179582117839285399618929182259624916911439331479239488 +1377519658968884779897799292198595917148591218821239614925192183128211371959295299959221257811239131 +2627929612263297913663198357736594275199769318118546371628241929618769991214845296132728799271417791 +7576972138942696587693261199467749912936563131454674591315468112333812659454789393699615918167338715 +1778985995949591574849625759667137792111923771976687288498928179271746938986792658149697999719931492 +1497174992519427759719169714342998311421419197412331399469329847692794191181111339933357161599799597 +7158163932199423259572847121198873491117119999919996985599149129919588579223518511588378639685891315 +6219361855573892912131718991998196749251949481814844299182119621344738991784149679694476483378729929 +1398599388779918861183115593522489331874978727922373471991219247115944295163699192776967648639268659 +8246143812623927544154898631717811662213981175534335932638718171969421398421692892399791789186618487 +4457134139841167885881924949364884427839314973843366971997383189398399657998961185159781215111619878 +2889934549617841969369167119338852267887359654637229634658717722728239549585928482358431813296859898 +9968328746979669249145999585143117898196251944175462877716129989773292878848966999379983436559934934 +6493994193996595991299891992438695916166928199197254269991979292986895799985938128881229638182945947 +7266928918675112795879219714929968471352931229592623394713393869269941181318972192169271548316892574 +2174196776119149368951321419512925818641848291921584561885586189445194773817792112569311611884895234 +5617593683181229369984148191819568774125471789395181874847932736429929334265516997661987796259692831 +7882228973393795938729341656949619511272988446634311621871394678132831899299832311614174939261965429 +1131881592984545192978449637151892697525357924197922698921549791127287595929496515499876251183628176 +9922954913893559597663824459627321943979599969474976998899879429185892995511392872381956558981867119 +5166385448694129439797196837181355547743133114829285275149516986685925813416161972979872853196155916 +3292899922794159291851551956738911971988929383861879921354871787791695284999126375594873179426338749 +3971894752927483651131869717199971871964396958991799413921821198121691265888799954912311963139425236 +5778565318498395151591219315499879273943817921588123199611531989674238272218198865913747444419629892 +1148956123134785152496887782841669964225944272953163717879528194631986997718971481186872943816456591 +5982998155291779554662769986899318991622943125199743569463317913185991442772122148595119965486877247 +1592622221626611159996955945922614981222438358385913259412688295754161193293194979914999974739594393 +1735995389872189896282749844299933869588931688619645726996959149185997128397635694766944945458588326 +8969839951998899979864239155832982419271118982882915197745124695321464198748765361499779978719441246 +6625484228731161195646227911299498172947526327866775111148173468799729895761595437743769876911891521 +6742199381937628999297557371399872217983581247138899399239252389649491949578491998528132446741686888 +9212328164223895178547771855976415127193137191217188318191949238429129518281919843229126971963111111 +8589368771155989697277997492471969978917117589192916193887975787949771588121551699971132275182798898 +1321997349438946615383211392181979153964221243634922639112684529286794272849757186396714797832698913 +5499455959999492339698359789213323519479768626457859213487938838995348379392231623595879353861382977 +4612129999347379911982695394862437936254989311441326295994617191963497895691479282499959828989987571 +3953949985681978999897341375236798381973326952579319464522624134872231113676167279146985521828793431 +2547232255918288952326674861524529119719429148626615319818611241832868529862499874951986568635799957 +2181959929952711246913363617991144311613441799129546223661433628933579819732979148127949974997119951 +1369158892917379123998253242599392893199998853969933983592129813969821127281951849151627687114757833 +9629699176294886577773394197932998769278865351155516385519239971147785457838922269188195227329168368 +1788912961677399663173822433232514884848179868666798987368382881226468481869127998236137881926332779 +4811241997249159619241887142461943467819366815858371379792287198271563986876852729186927761281942192 +2121993996798639492562961751217922319969898368461919218459919713413366292171628639922554153774327219 +3489591127775295131639799492971117993637992434115331278511281997587996291111198123428588991837368589 +1283798883941983912985615991541417994114453747887836936186431822993881123911799962749981389292178586 +8392631553657936578372199916912973193995216848776811924799769959318889717919172521769363698292447429 +9587697134368994212916856845984875416273418561198913859115983139967143891991617853289936814265113512 +9111571922348271733199797953979536915998195916688317991255916216419817129423728913622269119972958686 +9959599568987335314713638999897919449512991684446347789183915679567141547281548619496728636288445972 +8391247911139217869276338998734496391173478717461631572924754792241695917352991662734191814376765142 \ No newline at end of file diff --git a/2021/day15/main.go b/2021/day15/main.go new file mode 100644 index 0000000..61e0fe2 --- /dev/null +++ b/2021/day15/main.go @@ -0,0 +1,255 @@ +package main + +import ( + "fmt" + "math" + "strconv" + "strings" + + "github.com/mbonnafon/AdventOfCode/helpers" +) + +func main() { + lines, _ := helpers.StringLines("./input.txt") + fmt.Println("Part 1. :", pt1(ParseCells(lines))) + fmt.Println("Part 2. :", pt2(ParseCells(lines))) +} + +func pt1(lines [][]int) int { + g := NewGraph(lines) + node := g.AStar() + //printGraph(node) + + var score int + for node.parent != nil { + score += node.coord.weight + node = node.parent + } + return score +} + +func pt2(lines [][]int) int { + fullLines := make([][]int, len(lines)*5) + for i := range fullLines { + fullLines[i] = make([]int, len(lines)*5) + } + for i := 0; i < 5; i++ { + for j := 0; j < 5; j++ { + for iLines := 0; iLines < len(lines); iLines++ { + for jLines := 0; jLines < len(lines); jLines++ { + value := (lines[iLines][jLines] + i + j) % 9 + if value == 0 { + value = 9 + } + fullLines[(len(lines)*i)+iLines][(len(lines)*j)+jLines] = value + } + } + } + } + g := NewGraph(fullLines) + node := g.AStar() + var score int + for node.parent != nil { + score += node.coord.weight + node = node.parent + } + return score +} + +func printGraph(node *Node) { + grid := make([][]string, 5) + for i := 0; i < 50; i++ { + grid[i] = make([]string, 5) + for j := 0; j < 50; j++ { + grid[i][j] = " " + } + } + for { + if node == nil { + break + } + grid[node.coord.x][node.coord.y] = strconv.Itoa(node.coord.weight) + node = node.parent + } + for _, v := range grid { + fmt.Println(v) + } +} + +///////// + +type Coord struct { + x, y int + weight int +} + +// Graph contains all prerequisites for A* +type Graph struct { + start Node + end Node + cells [][]int + width int + height int + openNodes map[Node]bool + closedNodes map[Coord]bool +} + +// NewGraph build a graph +func NewGraph(cells [][]int) Graph { + height := len(cells) + width := len(cells[0]) + startNode := Node{coord: Coord{x: 0, y: 0, weight: cells[0][0]}} + endNode := Node{coord: Coord{x: height - 1, y: width - 1, weight: cells[height-1][width-1]}} + return Graph{ + start: startNode, + end: endNode, + cells: cells, + height: len(cells), + width: len(cells[0]), + openNodes: map[Node]bool{startNode: true}, + closedNodes: make(map[Coord]bool), + } +} + +func ParseCells(lines []string) [][]int { + var cells [][]int + for _, v := range lines { + lineInt := helpers.ToIntSlice(strings.Split(v, "")) + cells = append(cells, lineInt) + } + return cells +} + +func (g Graph) lowestNodeFromOpenNodes() *Node { + var node *Node + for n := range g.openNodes { + if node == nil { + node = ptrNode(n) + continue + } + if n.F < node.F { + node = ptrNode(n) + } + if n.F == node.F && n.H < node.H { // if F cost is the same, select the H cost with the lowest value + node = ptrNode(n) + } + } + return node +} + +func (g Graph) nodeInOpenList(c Coord) *Node { + for n := range g.openNodes { + if n.coord == c { + return &n + } + } + return nil +} + +// Node A* node +type Node struct { + G int // G cost (score of the node) + H int // H cost (distance from the end node) or heuristic + F int // F cost (sum of G and H) + coord Coord + parent *Node +} + +func (g Graph) newNode(coord Coord) Node { + gCost := g.cells[coord.x][coord.y] + hCost := manhattanDistance(coord, g.end.coord) + return Node{ + G: gCost, + H: hCost, + F: gCost + hCost, + coord: coord, + } +} + +func manhattanDistance(a, b Coord) int { + return int(math.Abs(float64(a.x-b.x)) + math.Abs(float64(a.y-b.y))) +} + +func ptrNode(n Node) *Node { + return &n +} + +//// GAME +func (g *Graph) AStar() *Node { + for { + current := g.lowestNodeFromOpenNodes() + if current == nil { + fmt.Println("NO NODES") + return nil + } + delete(g.openNodes, *current) + g.closedNodes[*¤t.coord] = true + + if current.coord == g.end.coord { + return current + } + + for _, n := range g.getdAdjacentNotDiag(*current) { + if g.closedNodes[n.coord] { + continue + } + if nOpenList := g.nodeInOpenList(n.coord); nOpenList != nil { + if n.F < nOpenList.F { + nOpenList.F = n.F + nOpenList.G = n.G + nOpenList.H = n.H + nOpenList.parent = current + } + continue + } + g.openNodes[n] = true + } + } +} + +/////// +func (g *Graph) InGrid(i, j int) bool { + return i >= 0 && i < g.width && j >= 0 && j < g.height +} + +func (g Graph) getdAdjacentNotDiag(n Node) []Node { + if !g.InGrid(n.coord.x, n.coord.y) { + return nil + } + + i, j := n.coord.x, n.coord.y + var neighbors []Node + //up + if i > 0 { + node := g.newNode(Coord{x: i - 1, y: j, weight: g.cells[i-1][j]}) + node.G += n.G + node.F = node.G + node.H + node.parent = &n + neighbors = append(neighbors, node) + } + //down + if (i + 1) < g.height { + node := g.newNode(Coord{x: i + 1, y: j, weight: g.cells[i+1][j]}) + node.G += n.G + node.F = node.G + node.H + node.parent = &n + neighbors = append(neighbors, node) + } + //left + if j > 0 { + node := g.newNode(Coord{x: i, y: j - 1, weight: g.cells[i][j-1]}) + node.G += n.G + node.F = node.G + node.H + node.parent = &n + neighbors = append(neighbors, node) + } + //right + if (j + 1) < g.width { + node := g.newNode(Coord{x: i, y: j + 1, weight: g.cells[i][j+1]}) + node.G += n.G + node.F = node.G + node.H + node.parent = &n + neighbors = append(neighbors, node) + } + return neighbors +} diff --git a/2021/day16/input.txt b/2021/day16/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/2021/day16/main.go b/2021/day16/main.go new file mode 100644 index 0000000..cda230b --- /dev/null +++ b/2021/day16/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + + "github.com/mbonnafon/AdventOfCode/helpers" +) + +func main() { + lines, _ := helpers.StringLines("./input.txt") + fmt.Println("Part 1. :", pt1(lines)) + fmt.Println("Part 2. :", pt2(lines)) +} + +func pt1(lines []string) int { + return 0 +} + +func pt2(lines []string) int { + return 0 +} diff --git a/2021/day20/input.txt b/2021/day20/input.txt new file mode 100644 index 0000000..49c9ad7 --- /dev/null +++ b/2021/day20/input.txt @@ -0,0 +1,13 @@ +..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..## +#..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.### +.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#. +.#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#..... +.#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.. +...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#..... +..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# + +#..#. +#.... +##..# +..#.. +..### \ No newline at end of file diff --git a/2021/day20/main.go b/2021/day20/main.go new file mode 100644 index 0000000..f85a814 --- /dev/null +++ b/2021/day20/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + + "github.com/mbonnafon/AdventOfCode/helpers" +) + +const lightPixels = "#" +const darkPixels = "." + +func main() { + lines, _ := helpers.StringLines("./input.txt") + fmt.Println("Part 1. :", pt1(lines)) + fmt.Println("Part 2. :", pt2(lines)) +} + +func pt1(lines []string) int { + return 0 +} + +func pt2(lines []string) int { + return 0 +} diff --git a/2021/day5/main.go b/2021/day5/main.go index e07be87..d71777a 100644 --- a/2021/day5/main.go +++ b/2021/day5/main.go @@ -147,12 +147,11 @@ func parseVents(lines []string) []Vent { return vents } -func score(m map[string]int) int { - var counter int +func score(m map[string]int) (counter int) { for _, v := range m { if v > 1 { counter++ } } - return counter + return } diff --git a/go.mod b/go.mod index 8a7ca8d..87fb668 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/mbonnafon/AdventOfCode go 1.15 + +require github.com/jinzhu/copier v0.3.5 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..787ad11 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/jinzhu/copier v0.3.4 h1:mfU6jI9PtCeUjkjQ322dlff9ELjGDu975C2p/nrubVI= +github.com/jinzhu/copier v0.3.4/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= +github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg= +github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= diff --git a/helpers/grid.go b/helpers/grid.go index b5a50d2..ba42c26 100644 --- a/helpers/grid.go +++ b/helpers/grid.go @@ -40,6 +40,31 @@ func (g *Grid) GetNeigh(i, j int) []Coord { return neighbors } +func (g Grid) GetCoordAdjacentNoDiag(i, j int) []Coord { + if !g.InGrid(i, j) { + return nil + } + + var neighbors []Coord + //up + if i > 0 { + neighbors = append(neighbors, Coord{X: i - 1, Y: j}) + } + //down + if (i + 1) < g.Height { + neighbors = append(neighbors, Coord{X: i + 1, Y: j}) + } + //left + if j > 0 { + neighbors = append(neighbors, Coord{X: i, Y: j - 1}) + } + //right + if (j + 1) < g.Width { + neighbors = append(neighbors, Coord{X: i, Y: j + 1}) + } + return neighbors +} + func (g Grid) IsSmallestComparedToNeigh(i, j int) bool { ref := g.Cells[i][j] //up diff --git a/scala/src/main/resources/2021/Day10.txt b/scala/src/main/resources/2021/Day10.txt new file mode 100644 index 0000000..2f182d8 --- /dev/null +++ b/scala/src/main/resources/2021/Day10.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] \ No newline at end of file diff --git a/scala/src/main/scala/com/mbonnafon/adventofcode/Runner.scala b/scala/src/main/scala/com/mbonnafon/adventofcode/Runner.scala index 8f9ec06..8c2f760 100644 --- a/scala/src/main/scala/com/mbonnafon/adventofcode/Runner.scala +++ b/scala/src/main/scala/com/mbonnafon/adventofcode/Runner.scala @@ -8,7 +8,8 @@ object Runner extends App { (2021, 1) -> year2021.Day01, (2021, 2) -> year2021.Day02, (2021, 3) -> year2021.Day03, - (2021, 4) -> year2021.Day04 + (2021, 4) -> year2021.Day04, + (2021, 10) -> year2021.Day10 ) puzzleMap.get(year, day) match { diff --git a/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day04.scala b/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day04.scala index 902cfeb..13e309d 100644 --- a/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day04.scala +++ b/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day04.scala @@ -6,7 +6,7 @@ object Day04 extends Puzzle { def part1(input: List[String]): Int = { val draws = input.head.split(",").map(_.toInt).toList - val boards = input.drop(2).print(boards) + // val boards = input.drop(2).print(boards) // val game = new Game(draws, ) return 0 } diff --git a/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day10.scala b/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day10.scala new file mode 100644 index 0000000..a484097 --- /dev/null +++ b/scala/src/main/scala/com/mbonnafon/adventofcode/year2021/Day10.scala @@ -0,0 +1,25 @@ +package com.mbonnafon.adventofcode.year2021 + +import com.mbonnafon.adventofcode.{Puzzle, Reader} + +import scala.collection.mutable.Stack + +object Day10 extends Puzzle { + + def part1(input: List[String]): Int = { + input.foreach { line => + val s = Stack[Char]() + line.foreach { c => + c.toString match { + case "(" | "[" | "{" | "<" => s.push(c) + case _ => print(s.pop) + } + } + } + 9 + } + + def part2(input: List[String]): Int = + 0 + +}