From 56571eadf0ea7e737cec64c0691098ba70f77979 Mon Sep 17 00:00:00 2001 From: Alan R Evans Date: Mon, 4 Dec 2023 13:31:13 +0000 Subject: [PATCH] day3 and 4 --- 2023/go/day03/day03.go | 146 ++++++++++++++++++++++++++++ 2023/go/day03/day03_test.go | 37 +++++++ 2023/go/day03/input.txt | 140 +++++++++++++++++++++++++++ 2023/go/day04/day04.go | 98 +++++++++++++++++++ 2023/go/day04/day04_test.go | 29 ++++++ 2023/go/day04/input.txt | 188 ++++++++++++++++++++++++++++++++++++ 2023/go/go.mod | 1 + 2023/go/go.sum | 2 + 2023/go/main.go | 11 ++- 2023/go/utils/inputFile.go | 19 ++-- 10 files changed, 664 insertions(+), 7 deletions(-) create mode 100644 2023/go/day03/day03.go create mode 100644 2023/go/day03/day03_test.go create mode 100644 2023/go/day03/input.txt create mode 100644 2023/go/day04/day04.go create mode 100644 2023/go/day04/day04_test.go create mode 100644 2023/go/day04/input.txt diff --git a/2023/go/day03/day03.go b/2023/go/day03/day03.go new file mode 100644 index 0000000..8433764 --- /dev/null +++ b/2023/go/day03/day03.go @@ -0,0 +1,146 @@ +package day03 + +import ( + "adventofcode2023/utils" + _ "adventofcode2023/utils" + _ "adventofcode2023/utils/grid2d" + "adventofcode2023/utils/inputs" + "errors" + "fmt" + "unicode" +) + +type Coord struct { + x int + y int +} +type Part struct { + number string + location Coord +} + +func Part1(input string) int { + grid := inputs.ToGrid2D(input, "\n", "", ".", func(c string) string { return c}) + partLocs := []Coord{} + for j := 0; j < grid.SizeY(); j++ { + for i := 0; i < grid.SizeX(); i++ { + if !unicode.IsDigit(rune(grid.Get(i, j)[0])) { continue} + directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0},{-1,1},{1,1},{-1,-1},{1,-1}} + for _, dir := range directions { + x := i + dir[0] + y := j + dir[1] + if x < 0 || x >= grid.SizeX() || y < 0 || y >= grid.SizeY() { + continue + } + if !unicode.IsDigit(rune(grid.Get(x, y)[0])) && grid.Get(x, y) != "." { + fmt.Printf("partLoc x:%v y:%v %v\n", i, j, string(grid.Get(i, j))) + partLocs = append(partLocs, Coord{i, j}) + break + } + } + } + } + fmt.Println(partLocs) + sum := 0 + for _, partLoc := range partLocs { + if grid.Get(partLoc.x, partLoc.y) == "." { + continue + } + partNum := grid.Get(partLoc.x, partLoc.y) + for i:=1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i++{ + partNum += grid.Get(partLoc.x+i, partLoc.y) + grid.Set(partLoc.x+i, partLoc.y, ".") + } + for i:=-1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i--{ + partNum = grid.Get(partLoc.x+i, partLoc.y) + partNum + grid.Set(partLoc.x+i, partLoc.y, ".") + } + fmt.Println(partNum) + sum += utils.MustAtoi(partNum) + } + return sum +} + +func Part2(input string) int { + grid := inputs.ToGrid2D(input, "\n", "", ".", func(c string) string { return c}) + partLocs := []Coord{} + gearLocs := []Coord{} + for j := 0; j < grid.SizeY(); j++ { + for i := 0; i < grid.SizeX(); i++ { + if grid.Get(i, j) == "*" { + gearLocs = append(gearLocs, Coord{i, j}) + } + if !unicode.IsDigit(rune(grid.Get(i, j)[0])) { continue} + directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0},{-1,1},{1,1},{-1,-1},{1,-1}} + for _, dir := range directions { + x := i + dir[0] + y := j + dir[1] + if x < 0 || x >= grid.SizeX() || y < 0 || y >= grid.SizeY() { + continue + } + if !unicode.IsDigit(rune(grid.Get(x, y)[0])) && grid.Get(x, y) != "." { + partLocs = append(partLocs, Coord{i, j}) + break + } + } + } + } + fmt.Println(partLocs) + fmt.Println(gearLocs) + + parts := []Part{} + for _, partLoc := range partLocs { + l := 0 + if grid.Get(partLoc.x, partLoc.y) == "." { + continue + } + partNum := grid.Get(partLoc.x, partLoc.y) + for i:=1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i++{ + partNum += grid.Get(partLoc.x+i, partLoc.y) + grid.Set(partLoc.x+i, partLoc.y, ".") + } + l = partLoc.x + for i:=-1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i--{ + partNum = grid.Get(partLoc.x+i, partLoc.y) + partNum + grid.Set(partLoc.x+i, partLoc.y, ".") + l = partLoc.x+i + } + fmt.Println(partNum) + parts = append(parts, Part{number: partNum, location: Coord{x: l, y: partLoc.y}}) + } + sum := 0 + for _, gearLoc := range gearLocs { + var gearedParts [2]Part + + directions := []Coord{{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1},{-1,-1},{1,-1}} + for _, dir := range directions { + part, err := getPart(gearLoc, dir, parts) + if err != nil {continue} + if gearedParts[0] == (Part{}) { + gearedParts[0] = part + } else if gearedParts[0] == part { + fmt.Println("duplicate part found") + } else { + gearedParts[1] = part + } + } + fmt.Println(gearedParts) + if gearedParts[0] != (Part{}) && gearedParts[1] != (Part{}) { + sum += utils.MustAtoi(gearedParts[0].number) * utils.MustAtoi(gearedParts[1].number) + } + } +// fmt.Println(parts) + return sum +} + +func getPart(gearloc Coord, offset Coord, parts []Part) (Part, error) { + coord := Coord{x: gearloc.x + offset.x, y: gearloc.y + offset.y} + for _, part := range parts { + if coord.x >= part.location.x && + coord.x <= part.location.x + len(part.number) -1 && + coord.y == part.location.y { + return part, nil + } + } + return Part{}, errors.New("Not Found") +} \ No newline at end of file diff --git a/2023/go/day03/day03_test.go b/2023/go/day03/day03_test.go new file mode 100644 index 0000000..63cdd2f --- /dev/null +++ b/2023/go/day03/day03_test.go @@ -0,0 +1,37 @@ +package day03 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPart1(t *testing.T) { + r := Part1( +`467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..`) + require.Equal(t, 4361, r) +} + +func TestPart2(t *testing.T) { + r := Part2( +`467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..`) + require.Equal(t, 467835, r) +} diff --git a/2023/go/day03/input.txt b/2023/go/day03/input.txt new file mode 100644 index 0000000..74cae63 --- /dev/null +++ b/2023/go/day03/input.txt @@ -0,0 +1,140 @@ +....937..........309.............191..............745.................913......................................256................891....... +................*..................-..113.......3*..............219.........495%................40....97.......*.......670.60..../.......... +......919..928..511......................................%......#...&...................................*.................*................. +........*.....*.....605..990....765.....&..120...159.....691.........1.....67*84.92..........687.580.....56.408..677.........$.........624.. +25.......911.525...........#......*...985..........-.&...........645.............*.......408....*..................=...546..994.......*..... +......................340*....&..821......504........60.11...........*967......114..........*......545...................*........=....518.. +.548....810.....898........934...........-.......808....*.......323....................40..260........@.......827.......27.....903.......... +....*....*......../..................11.........*........691.....*..450..........#....................................................564... +791.546..944.848......588.138.................875..772........587.....*.98.$....941....*638..........534*271.../..........73...56........... +.....................*................................*.............787.#..173......139.......&..360............368...838*.......*547....... +.658..358......985.257..@..645....636........542....24..........................+.............99.-...502....................371............. +.........*.............897..*.....*....628..*.............................#....13..853.442.............*.............586*......*............ +....619.4.......635..........827.......&....217...........164..........@.205........*...*..............229...............656....243....538.. +...........603.*......./..........167...........96..615....*.........512.........494..689....$696...............423.................35.*.... +.......625*.....221..424.............$....=......@.....*..167...........................................127........*591.......505...*....... +823......................804......*.......987..*.....819..........754.970*541................%......584*......911............/......559..... +...=.....423*297.....400....*..999.781.........472...........................................618..........+..*.........=.......#152......315 +.....52.............@....320.............................922........977*899.............................646..162.544..441................... +......*...542*54...............=.........698.229*836.......$......................-..........752..................*........*.......238...... +....608.............-.748.......524...-...*............241........911.813.529.....618..309.....*.................730..@.....8.........*423.. +..........%689...514..*.....255.......28...........665*.....+......*.....*...................539....146+...974.........432.......546........ +680..*77.............391...................994............950.......58......59......700..................*..*...727-...............*..585... +................980.......858.........+..........820*351........./........../.........*..................54..69...........*......949........ +196..132.......@.............*...../...481.-822...............535....378...............203.......541.............644-..111.890.......@...... +....*.....926........%413.686......512...............722.............*.......................231....*.648*940........................747.... +....872..........749............/.............930...*.....498.....784.....892...................*.190.........625.=90..115...............277 +........236..604*......890*981..401............*....981...*........................635*634...901..............*.........................*... +.......*...............................310......552.....893..........146...930.............................44.635.129...54...........751.... +.......6...41.307...763.......549.........*....................#23........%.........927*......774+.....=...*........*...........335......... +...........*.....$...@...984.*....590......452..........%..........39...................960...........765..730....859.&....242.............. +...557.....129............*..796.*......3.............405................422....+...........61&..178..................539..*....786......... +...%............./.......330...............%.193............246......663...$...694.....836..............610.................848./...129..... +..........239....219..............*824...762..*................*.....*...................$..254......................796$..............*.... +......440.............623......781................164........445......529.+.......692..........*............................517.........80.. +........*....538.....................................*231.................34.798...........942.960...492...........579...$........26........ +.........609....*...758......*....-............@.789.......+52...116................153...=.............*...975...*....709..899..*.......... +..319.........595..&..........700.497.......826....*.............................../.........535......959....*...288..........*..280........ +.......797.............................105......*5.......402*249..50...@..979.846.......460.................302............537.............. +........*................=................%..449...720.............*.486..%......*.........*.624...209.100.........227.................792.. +.........512............448............@.............&...816..................756...825..379..*...*...................*442.....465......@... +.....752...............................494..................*....474@.....575.........*.......83..338............................*...=...... +.....*...144.258..373...&......997.............431....874...382................295...422....*............67.......$.............895..40.394. +.....287....*.....-......296....$......................*................./................676.......254%..*....707......131*704............. +.226.................562........../.............#....751.....268......108.....380..#.....................685........32...................... +....=.789........684..*............910.58....232.........786.....723......121.../.23.......968*970.446.=..............-.......301........... +.........*425......@..20......712.................701...*...............................................165....475.....................61... +.209$...........................*......644./493.....*.94...#............144..260..69......134....................*...751.........981....*... +......788.......#614.....904..292...................1....377.............@........*.......*......../.......919..948.....*..513...*......796. +......=.................&.............941*.............$.........14........85.434..556...572....745....282...*.......621......+...709....... +..............364...........................377+....254...............111/..&....*.....................*......403.........339............... +..531+....666..%..148.......39.905*459..505.......................................991.159..............660.........=......*......8.936*..... +.............*...*.....685../............*..........-497.....805*317.....853+...............332...............989.577.....558....*.......... +........204.667..629......=.............114...............%.....................@...........*.......&...........................632......... +......#.....................929..................978...349..........771......764.........875.....274.......316........*.......%.......*709.. +..807.960...........894....-..................39................979....*....................................*....883..916...378............. +...*........#................633................*977.............*..990.........205...................438...429....................956#..... +.646...%....2.........$...............229%.265..............84...................=......746.......443*..............688*339................. +......63........86+.75........593.............*215...........*........478...&.............=..............27.152...%....................331.. +........................985....*...................932....640.............218..................676*......#..*....710..+121........227.%..... +................................884.475.........................118*645.@.........30.400*.125......290......698...............910*.......... +...725.........401.......990.........*.....525...389.....................475..............*............*322......933.....14..............719 +...*......772.....*......*.......261..99...&.....*........96.......*...............594....429.......870..........*.........*................ +..198..-..*.......873.396..........*..........726...........*70..775.........690...%..........83/........%...274.829........156...227....... +......386.99..............@....53.....104.............................738.......+....................164..83............476.......%......... +....................../....353..=.457*....275..24....................*......384....579..............$............182.......*416......550.... +................-.....937....................*.*..........884.....401..961..+......*........................21...#......*............*...... +....462......521..........#....532*...+125.771.401..........+...........+........619...=................955*.........630.151.506....343..... +........................242.....................................696..................786..881.........=............-.........*.............. +...652...=.%26...................967.........@..............331*....746........./............*.....126...+...=...71.....+....975............ +.....%.726............48.493......*......@.239.......212.........../.............110....$551.............545.121.....45..436..........=..... +.........................*.......489..780...............$............................47.........315...................*..............412.... +665.................425...930...................649.581.....=..385.............975...*.....103..*......217.443......763..................... +.....-................*..........615........703..*.........532...+..857....542..=..989....*..............%......................542......... +.....279.......621....481./786..#..............*..75.@................#...*..............222.......30.......728......&495.=905.......124.206 +..........509...*..........................-..........817..................34...593..................*........*..937........................ +.148.........*.772...........539*199........73.............733.................*.................23.298.....596....*...................141.. +...@......=.............331.........................589...*.....70...........72............402...#..............948....790........89/.*..... +..........578..338........................161..............352....*.....464..................+...........257..............*512........808... +...........................103.............................................*.899*580........................*.....................595....... +.....*776.....700.............#...580.....521*845.......29*67.............37.........-178.......=...........970.......569..686*............. +..997........*.....656....351........*662............30............392.......272.............926.......................*.......933.......... +......458...544.....*.....-....137+............83...%.............@..........-.....................268.756........=.640...............526... +.....#...............5...............453..752.........................................#600..........*......172..997...........539*.......... +.........355$....79...........376...*...................424...337.166..927...711....................920..............+818.........173....884 +.71*53...........*............&....938........599.18...*.........*...............187=...........599...........................496.......#... +.........../....997..........................*....-..237.....303....256...807.............#393.*....454...............*397......#..697...... +.....@.....576.......748.184........650.....429..........220*.........*....*...................15...*.....$...146..831.....*................ +..976../................../........@................@..................154..598..........328.............799....*.......755................. +........23.......564=..........771...631*.....893.540.......................................*...597...........458..................%....871. +...226...........................#.......612....+...............57&...&.............#12..115...@......967..............78..823.....763...... +.........777.-.......211.886.421.........................42.........799....284.99.................#......*..238#.......#...*................ +..........*...564..../..........$.&889...649...............=..................*...585.265..........855..781........-........865............. +....433..90...............................+.........456...........................*.....=.......................175....*482.........332..... +.......*.....................306.....*.............-.................@..........212...........879...@...............725......552...#........ +........2.......529......914*......256...........................999..840....................%.....5...........43....................882.... +......*.........@......................40.653.........681...................984.580...944...................=...........-884.......&..*..... +...959.76.#867.....419.............*.....*.............*.....765&.............%..........*......516........410...............481.463..71.... +...............501*........895..178.433..........291...137......................#.........658....*....672*.....9........216..*.............. +.........478...............*..............660......................748.590*193...851.85........939........320............*...227..+......... +806......*......548.......309............*......$....91..918..........................*.............................758/.524.......653...... +.........588.......*58........494.....291....97.231................932.......398.......457............905................................... +................&................#............*........942............-..267...............214...........*..931..........816....$........... +........&......5..614.=180..................60..........*...#...308.....*.....397......67....*........805..................=...178.542&..... +..671...574.........*........+.......559..........551..370..322....*.210.........=..51...*..803............../.........$.................... +.....*.......507..360.........679...=.....421*378.+..............51...................&.344..........676......606...564......360....+.839... +....935.......*.......159..................................530-.......480...112.................954../....214.............../.....981..*.... +........932..512..143*....................=.374...%249..................*.....$.352...............*...........124.11*380.................... +..........*............482..............963....*.......363.....*557......34......%................499...../...................%............. +....368...174......536....%......#..............513......*..847......................*575.485..............578..............1.625......556.. +.......*..............%.......5.794.......401.........877........26....*..........195.......%.....................316.381..........419*..... +......437.......583.........../.......214.*......................*......433....................@797.......174*524...+.........989........... +...................*778.&643.........*....187..#.....%...@.......86.........178.......629.............................................*..... +.....................................822........691.557.1..................*..........@...................204-.618.205.............418.631.. +.....659.............@.................................................892..811...689.......56....................*.....@.611............... +.....*..........*876.780.43..567.........325....437..896.........@........*........*.........*........................824..*..6............. +...159.......672..........*.&.......599.....*....+...*.....$...210......232........47....................*586..794........90......212....... +..................427.......................696......131.470................507.................696....95.........*...927.........*......... +.............925$.............887.....799.......332...................395..*......................*.........261-..441..........691......857. +....834..................728....*........#.%212....%....41*...681......*...881....604.......%..977.........................185.........-.... +..................../..2*......401...............................*...492........&....*829..928..............................*.....*256...... +...716.46=.........217....897......975.........561.......839...38................441........................................................ +.............=.............*.........*...........*......#..........&......211@.......475..........766..........................357*567...... +.500.........81..../......915............=......857..#....102...409.....................*301...15.-....725.........608..213................. +...*................192........736....877.............529...............994.....................*.......*.....399...*.....*................. +298...734...827............500*...........................802......819..........................448..257......*....240....264....527*285.... +........#.....*.....256*..............643.........957.70..*......3..........276...........677.................753........................... +.............634........765...741....../...-..344.....*...512....*............*.......499*.............283..............*....157............ +.....887............150...................217..-..968..94........376.......812....1.@......142@...-....*....277..720.115.210....=...692..... +....*......464........#...........571............*...........750.....128*........*..724...........325.71...*.......*...............-.....392 +.....679....*....378......+355.69....+....428...844......$......*487.....395..278............*335........62.....335......724..704........... +..........907......=..55*....................*.........795................................397..............................*..$.....$...#... +560......................753.........*....260...............................149*656.................526......426..390.....98......286.519... +...$................*123..........803.749........572.........642..620...696..............$...........=.........&.-.......................... +...........646...331.......................245......*...637+.*......*...%....606..........707................#.............................. +............*.................$...............=..998..........391..955......*.................582.....*822...148....%....388.....406..893... +........770..901...183.260.836..594........................................653...................*.239.............286..%...........$.*..... +....710*..............*.................709..948..............504$..............624.......%...214..............................303.....752.. +548...............642...393*469.@.......$....*......................172.........@......860..........170......15.............-.....*......... +...@...........*.....*...........155..........998.........657.851-.....*...............................*.....&.........972.751.249.......... +......898...561.186...207....270.....................................968...231..181..................324.........696........................ \ No newline at end of file diff --git a/2023/go/day04/day04.go b/2023/go/day04/day04.go new file mode 100644 index 0000000..b0b5e9d --- /dev/null +++ b/2023/go/day04/day04.go @@ -0,0 +1,98 @@ +package day04 + +import ( + "math" + "regexp" + "strings" + + "fmt" + + "adventofcode2023/utils" + + mapset "github.com/deckarep/golang-set/v2" +) + +type Game struct { + ID int + matches int + count int + winners mapset.Set[int] + cards mapset.Set[int] +} + +func Part1(input string) int { + games, err := parseGames(input) + if err != nil { + fmt.Println("Error:", err) + return -1 + } + + points := 0.0 + for _, game := range games { + matches := len(game.cards.Intersect(game.winners).ToSlice()) + if matches > 0 { + points += math.Pow(2, float64(matches - 1)) + } + } + return int(points) +} + +func Part2(input string) int { + games, err := parseGames(input) + if err != nil { + fmt.Println("Error:", err) + return -1 + } + + for i, game := range games { + matches := game.cards.Intersect(game.winners).ToSlice() + games[i].matches = len(matches) + games[i].count = 1 + } + + for i:=0;i // assumes input is in day/input.txt @@ -23,6 +25,12 @@ func main() { case 2: fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d))) fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d))) + case 3: + fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d))) + fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d))) + case 4: + fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d))) + fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d))) default: panic(fmt.Errorf("no such day: %d", d)) } @@ -30,7 +38,7 @@ func main() { // Reads day from os.Args. func day() int { - latest := 1 + latest := 3 if len(os.Args) == 1 { return latest } @@ -83,5 +91,6 @@ func TestPart2(t *testing.T) { } `, n)) fmt.Printf("wrote day%02d/day%02d_test.go\n", n, n) + utils.GenInputFile(n) } \ No newline at end of file diff --git a/2023/go/utils/inputFile.go b/2023/go/utils/inputFile.go index 893b134..b78e1be 100644 --- a/2023/go/utils/inputFile.go +++ b/2023/go/utils/inputFile.go @@ -16,15 +16,24 @@ func GenInputFile(day int) string { } pwd, _ := os.Getwd() - path := fmt.Sprintf("%s\\day%s\\input.txt", pwd, d) + path := fmt.Sprintf("%s/day%s/input.txt", pwd, d) fi, _ := os.Stat(path) if fi != nil { return path } - fmt.Println("Creating new input file...") - f, _ := os.OpenFile(path, os.O_CREATE, 0700) - f.WriteString(readHttp(2022, day)) + fmt.Printf("Creating new input file %v...", path) + f, err := os.Create(path) + if err != nil { + fmt.Println(err) + } else { + defer f.Close() + data := readHttp(2023, day) + _, err := f.WriteString(data) + if err != nil { + fmt.Println(err) + } + } return path } @@ -33,7 +42,6 @@ func readHttp(year, day int) string { url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", year, day) session := os.Getenv("sessionAoC") - req, err := http.NewRequest("GET", url, nil) if err != nil { panic(err) @@ -50,7 +58,6 @@ func readHttp(year, day int) string { if err != nil { panic(err) } - return string(body) }