From defbdd867a065e5c8c0aec833b6421ba3fccadc7 Mon Sep 17 00:00:00 2001 From: doniacld Date: Thu, 15 Jul 2021 22:07:39 +0200 Subject: [PATCH 1/3] Day 9 - Part 1 --- cmd/main.go | 3 + puzzles/2020/01/targetsum/computetargetsum.go | 2 +- puzzles/2020/09/README.md | 92 ++ puzzles/2020/09/cypher/cypher.go | 25 + puzzles/2020/09/cypher/oddfinder.go | 47 + puzzles/2020/09/cypher/oddfinder_test.go | 27 + puzzles/2020/09/cypher/parser.go | 61 + puzzles/2020/09/cypher/parser_test.go | 28 + .../09/cypher/resources/input-test-small.txt | 4 + .../2020/09/cypher/resources/input-test.txt | 20 + puzzles/2020/09/day09.go | 28 + puzzles/2020/09/input-test.txt | 20 + puzzles/2020/09/input.txt | 1000 +++++++++++++++++ 13 files changed, 1356 insertions(+), 1 deletion(-) create mode 100644 puzzles/2020/09/README.md create mode 100644 puzzles/2020/09/cypher/cypher.go create mode 100644 puzzles/2020/09/cypher/oddfinder.go create mode 100644 puzzles/2020/09/cypher/oddfinder_test.go create mode 100644 puzzles/2020/09/cypher/parser.go create mode 100644 puzzles/2020/09/cypher/parser_test.go create mode 100644 puzzles/2020/09/cypher/resources/input-test-small.txt create mode 100644 puzzles/2020/09/cypher/resources/input-test.txt create mode 100644 puzzles/2020/09/day09.go create mode 100644 puzzles/2020/09/input-test.txt create mode 100644 puzzles/2020/09/input.txt diff --git a/cmd/main.go b/cmd/main.go index fd94479..e2c57f9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -9,6 +9,7 @@ import ( day04 "github.com/doniacld/adventofcode/puzzles/2020/04" day05 "github.com/doniacld/adventofcode/puzzles/2020/05" day06 "github.com/doniacld/adventofcode/puzzles/2020/06" + day09 "github.com/doniacld/adventofcode/puzzles/2020/09" "github.com/doniacld/adventofcode/puzzles/solver" "log" ) @@ -53,6 +54,8 @@ func main() { s = day05.New("./puzzles/2020/05/input.txt") case 6: s = day06.New("./puzzles/2020/06/input.txt") + case 9: + s = day09.New("./puzzles/2020/09/input.txt", 25) } out, err := s.Solve() diff --git a/puzzles/2020/01/targetsum/computetargetsum.go b/puzzles/2020/01/targetsum/computetargetsum.go index 2a7a535..88f3d85 100644 --- a/puzzles/2020/01/targetsum/computetargetsum.go +++ b/puzzles/2020/01/targetsum/computetargetsum.go @@ -40,7 +40,7 @@ func ComputeTargetSumWithTriplet(expenses []int, target int) int { } -// Compute the target sum by parsing twice the integer array +// FindIntruder the target sum by parsing twice the integer array // complexity time: O(n²) func computeBrutallyTargetSumWithPair(expenses []int, target int) int { for i := 0; i < len(expenses); i++ { diff --git a/puzzles/2020/09/README.md b/puzzles/2020/09/README.md new file mode 100644 index 0000000..217828c --- /dev/null +++ b/puzzles/2020/09/README.md @@ -0,0 +1,92 @@ +# Day 9: Encoding Error + +## Part One + +With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you. + +Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input). + +The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness. + +XMAS starts by transmitting a preamble of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair. + +For example, suppose your preamble consists of the numbers 1 through 25 in a random order. To be valid, the next number must be the sum of two of those numbers: + + 26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24). + 49 would be a valid next number, as it is the sum of 24 and 25. + 100 would not be valid; no two of the previous 25 numbers sum to 100. + 50 would also not be valid; although 25 appears in the previous 25 numbers, the two numbers in the pair must be different. + +Suppose the 26th number is 45, and the first number (no longer an option, as it is more than 25 numbers ago) was 20. Now, for the next number to be valid, there needs to be some pair of numbers among 1-19, 21-25, or 45 that add up to it: + + 26 would still be a valid next number, as 1 and 25 are still within the previous 25 numbers. + 65 would not be valid, as no two of the available numbers sum to it. + 64 and 66 would both be valid, as they are the result of 19+45 and 21+45 respectively. + +Here is a larger example which only considers the previous 5 numbers (and has a preamble of length 5): + + 35 + 20 + 15 + 25 + 47 + 40 + 62 + 55 + 65 + 95 + 102 + 117 + 150 + 182 + 127 + 219 + 299 + 277 + 309 + 576 + +In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is 127. + +The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is not the sum of two of the 25 numbers before it. What is the first number that does not have this property? + +Your puzzle answer was 1492208709. + +## Part Two + +The final step in breaking the XMAS encryption relies on the invalid number you just found: you must find a contiguous set of at least two numbers in your list which sum to the invalid number from step 1. + +Again consider the above example: + + 35 + 20 + 15 + 25 + 47 + 40 + 62 + 55 + 65 + 95 + 102 + 117 + 150 + 182 + 127 + 219 + 299 + 277 + 309 + 576 + +In this list, adding up all of the numbers from 15 through 40 produces the invalid number from step 1, 127. (Of course, the contiguous set of numbers in your actual list might be much longer.) + +To find the encryption weakness, add together the smallest and largest number in this contiguous range; in this example, these are 15 and 47, producing 62. + +What is the encryption weakness in your XMAS-encrypted list of numbers? + +Answer: + +Although it hasn't changed, you can still get your puzzle input. + +You can also [Shareon Twitter Mastodon] this puzzle. diff --git a/puzzles/2020/09/cypher/cypher.go b/puzzles/2020/09/cypher/cypher.go new file mode 100644 index 0000000..88274dd --- /dev/null +++ b/puzzles/2020/09/cypher/cypher.go @@ -0,0 +1,25 @@ +package cypher + +// Cypher holds the map for the preamble values and the list of numbers to parse +type Cypher struct { + Preamble Preamble + Numbers []int +} + +// Preamble holds pars to sum to get the target value +type Preamble map[int]struct{} + +// newCypher returns an initialized Cypher +func newCypher() Cypher { + return Cypher{newPreamble(), make([]int, 0)} +} + +// newPreamble returns an initialized map +func newPreamble() Preamble { + return make(map[int]struct{}, 0) +} + +// insert a new element in Preamble +func (p Preamble) insert(elt int) { + p[elt] = struct{}{} +} diff --git a/puzzles/2020/09/cypher/oddfinder.go b/puzzles/2020/09/cypher/oddfinder.go new file mode 100644 index 0000000..6b05602 --- /dev/null +++ b/puzzles/2020/09/cypher/oddfinder.go @@ -0,0 +1,47 @@ +package cypher + +import "math" + +// Find the values that is not the sum of two values is the preamble map +// the map contains the x previous values +// x is a range that begins at 0 and ends at the given preamble index +func (c Cypher) FindOdd(idxEndPreamble int) int { + idxStartPreamble := 0 + for i := idxEndPreamble; i < len(c.Numbers); i++ { + + elt := c.Numbers[i] + if !c.findPair(elt) { + return elt + } + + c.updatePreamble(idxStartPreamble, idxEndPreamble) + idxStartPreamble++ + } + // should not happened + return 0 +} + +// findPair returns the odd value if there is no pair summing it +func (c Cypher) findPair(elt int) bool { + for p := range c.Preamble { + toFind := elt - p + + f := math.Abs(float64(toFind)) + if _, ok := c.Preamble[int(f)]; ok { + if toFind != p { + return true + } + } + } + return false +} + +// updatePreamble deletes the first value of the preamble +// and adds the current one to the preamble +func (c Cypher) updatePreamble(idxToRemove, idxPreamble int) { + // remove the first element + delete(c.Preamble, c.Numbers[idxToRemove]) + // add the next element + c.Preamble[c.Numbers[idxToRemove+idxPreamble]] = struct{}{} +} + diff --git a/puzzles/2020/09/cypher/oddfinder_test.go b/puzzles/2020/09/cypher/oddfinder_test.go new file mode 100644 index 0000000..93e5458 --- /dev/null +++ b/puzzles/2020/09/cypher/oddfinder_test.go @@ -0,0 +1,27 @@ +package cypher + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCypher_FindOdd(t *testing.T) { + tt := []struct { + description string + input string + idxPreamble int + output int + }{ + {"nominal case", "./resources/input-test.txt", 5, 127}, + } + + for _, tc := range tt { + t.Run(tc.description, func(t *testing.T) { + c, err := ReadAndExtract(tc.input, tc.idxPreamble) + assert.NoError(t, err) + v := c.(tc.idxPreamble) + assert.Equal(t, tc.output, v) + }) + } +} diff --git a/puzzles/2020/09/cypher/parser.go b/puzzles/2020/09/cypher/parser.go new file mode 100644 index 0000000..f375d54 --- /dev/null +++ b/puzzles/2020/09/cypher/parser.go @@ -0,0 +1,61 @@ +package cypher + +import ( + "bufio" + "log" + "os" + "strconv" +) + +// ReadAndExtract reads the file line by line and applies the given function to extract what we want +func ReadAndExtract(path string, idx int) (Cypher, error) { + file, err := os.Open(path) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + // initialization + c := newCypher() + nbs := make([]int, 0) + i := 0 + + // scan lines + scanner := bufio.NewScanner(file) + for scanner.Scan() { + elt, err := convertToInt(scanner.Text()) + if err != nil { + return c, err + } + + // if we are in the preamble range, store the value in the preamble map + if i < idx { + err = c.fillPreamble(elt) + } + if err != nil { + return c, err + } + + // store all the numbers + nbs = append(nbs, elt) + i++ + } + + c.Numbers = nbs + return c, scanner.Err() +} + +// fills the preamble map +func (c Cypher) fillPreamble(elt int) error { + c.Preamble.insert(elt) + return nil +} + +// convertToInt converts the line into an int +func convertToInt(line string) (int, error) { + elt, err := strconv.Atoi(line) + if err != nil { + return elt, err + } + return elt, nil +} diff --git a/puzzles/2020/09/cypher/parser_test.go b/puzzles/2020/09/cypher/parser_test.go new file mode 100644 index 0000000..ff98789 --- /dev/null +++ b/puzzles/2020/09/cypher/parser_test.go @@ -0,0 +1,28 @@ +package cypher + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReadAndExtract(t *testing.T) { + tt := []struct { + description string + input string + idxPreamble int + output Cypher + }{ + {"nominal case", "./resources/input-test-small.txt", 2, + Cypher{Preamble: Preamble{1: {}, 2: {}}, Numbers: []int{3, 4}}, + }} + + for _, tc := range tt { + t.Run(tc.description, func(t *testing.T) { + + c, err := ReadAndExtract(tc.input, tc.idxPreamble) + assert.NoError(t, err) + assert.Equal(t, tc.output, c) + }) + } +} diff --git a/puzzles/2020/09/cypher/resources/input-test-small.txt b/puzzles/2020/09/cypher/resources/input-test-small.txt new file mode 100644 index 0000000..b178657 --- /dev/null +++ b/puzzles/2020/09/cypher/resources/input-test-small.txt @@ -0,0 +1,4 @@ +1 +2 +3 +4 \ No newline at end of file diff --git a/puzzles/2020/09/cypher/resources/input-test.txt b/puzzles/2020/09/cypher/resources/input-test.txt new file mode 100644 index 0000000..cda4246 --- /dev/null +++ b/puzzles/2020/09/cypher/resources/input-test.txt @@ -0,0 +1,20 @@ +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576 \ No newline at end of file diff --git a/puzzles/2020/09/day09.go b/puzzles/2020/09/day09.go new file mode 100644 index 0000000..8761af1 --- /dev/null +++ b/puzzles/2020/09/day09.go @@ -0,0 +1,28 @@ +package day09 + +import ( + "fmt" + + "github.com/doniacld/adventofcode/puzzles/2020/09/cypher" + "github.com/doniacld/adventofcode/puzzles/solver" +) + +func New(fileName string, idxPreamble int) solver.Solver { + return day09{fileName: fileName, idxPreamble: idxPreamble} +} + +type day09 struct { + fileName string + idxPreamble int +} + +func (d day09) Solve() (string, error) { + c, err := cypher.ReadAndExtract(d.fileName, d.idxPreamble) + if err != nil { + return err + } + + noSumValue := c.FindIntruder(d.idxPreamble) + out := fmt.Sprintf("First number to not respect the rule is: %d ", noSumValue) + return out, nil +} diff --git a/puzzles/2020/09/input-test.txt b/puzzles/2020/09/input-test.txt new file mode 100644 index 0000000..cda4246 --- /dev/null +++ b/puzzles/2020/09/input-test.txt @@ -0,0 +1,20 @@ +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576 \ No newline at end of file diff --git a/puzzles/2020/09/input.txt b/puzzles/2020/09/input.txt new file mode 100644 index 0000000..800d0e1 --- /dev/null +++ b/puzzles/2020/09/input.txt @@ -0,0 +1,1000 @@ +9 +1 +39 +30 +43 +2 +41 +50 +36 +21 +8 +37 +16 +13 +18 +4 +40 +33 +25 +11 +47 +10 +38 +29 +14 +53 +3 +49 +6 +31 +5 +12 +15 +56 +43 +57 +7 +8 +26 +9 +13 +16 +19 +22 +17 +39 +11 +10 +21 +24 +20 +14 +18 +63 +23 +25 +27 +28 +30 +33 +15 +29 +45 +32 +26 +31 +34 +35 +36 +53 +62 +60 +37 +41 +38 +47 +39 +68 +40 +46 +42 +43 +44 +51 +48 +91 +71 +67 +57 +115 +75 +69 +72 +73 +76 +85 +77 +78 +79 +114 +95 +143 +82 +84 +120 +86 +92 +99 +105 +123 +124 +126 +142 +129 +145 +152 +159 +148 +149 +219 +361 +157 +161 +163 +166 +176 +206 +234 +178 +304 +410 +324 +204 +228 +247 +253 +330 +271 +423 +293 +297 +306 +481 +487 +434 +323 +391 +327 +329 +342 +354 +499 +382 +475 +432 +451 +457 +497 +550 +500 +774 +564 +568 +665 +590 +603 +1064 +650 +681 +677 +652 +656 +1149 +671 +696 +736 +932 +833 +883 +1140 +997 +954 +1047 +1678 +1068 +1342 +1132 +1662 +1240 +1584 +1253 +1302 +1306 +1329 +1323 +1308 +1327 +2631 +2287 +1432 +2044 +1765 +1716 +2561 +1951 +3259 +2734 +2115 +2200 +2308 +2459 +2372 +3612 +4036 +2555 +2559 +2637 +2614 +5328 +3995 +2635 +2759 +3148 +6151 +3383 +3481 +3667 +4066 +4259 +4315 +8711 +4487 +4423 +4508 +5907 +6454 +8682 +8767 +5114 +9013 +6864 +5251 +17695 +6950 +5394 +7143 +6142 +7642 +15631 +9759 +7796 +13026 +12893 +8802 +8738 +8910 +12857 +8931 +10365 +15910 +12596 +10508 +12064 +13190 +10645 +11536 +11393 +12344 +14880 +26216 +13285 +14944 +22023 +19103 +19439 +16534 +17648 +17540 +17669 +17841 +35510 +20467 +19296 +21010 +24408 +21153 +23737 +22038 +23930 +22181 +22929 +24678 +25629 +34182 +53478 +30825 +36836 +47607 +34203 +38572 +40449 +35188 +35209 +84443 +54755 +39763 +40306 +79433 +55335 +43191 +44967 +44219 +45110 +68897 +75408 +69412 +64441 +56454 +91591 +65028 +67661 +69391 +70397 +72775 +122996 +74951 +123652 +117742 +109247 +82954 +80069 +111789 +203721 +87410 +88158 +182186 +143864 +101564 +125351 +329072 +126851 +124115 +121482 +132689 +134419 +197811 +139788 +143172 +210811 +155020 +157905 +204436 +181633 +168227 +379038 +167479 +175568 +188974 +220099 +280371 +223046 +281871 +293578 +349897 +261270 +245597 +254171 +338855 +290594 +359456 +310651 +328762 +298192 +323247 +416290 +439776 +335706 +436838 +412020 +581303 +343047 +364542 +409073 +624918 +578563 +468643 +698311 +499768 +582933 +536191 +755067 +670461 +621439 +588786 +608843 +626954 +737968 +633898 +848849 +967965 +957145 +752120 +776562 +707589 +931833 +811690 +773615 +908841 +1047206 +968411 +1004834 +1757690 +1088554 +1119124 +1553272 +1482151 +1222684 +1937403 +1707910 +1858896 +1584099 +1341487 +1682456 +1459709 +2020387 +1588252 +1481204 +1484151 +1519279 +1585305 +3072403 +1913675 +2131525 +3772571 +1973245 +3068250 +2207678 +2311238 +3522708 +2703888 +2564171 +3227189 +2801196 +2822691 +2860766 +2825638 +2926792 +4846025 +2940913 +3003430 +4556554 +2965355 +3397826 +3104584 +3498980 +3886920 +4121353 +6813712 +5434867 +4875409 +4911566 +5033316 +5015126 +5365367 +5268059 +5386862 +5623887 +5965350 +6045497 +7620333 +5752430 +8008273 +7950609 +7086708 +8119710 +6502410 +6069939 +6603564 +10663996 +7385900 +9154979 +8996762 +9890535 +9786975 +12995119 +12010847 +15200476 +10380493 +12888392 +12115436 +11432359 +11376317 +11717780 +16382662 +11822369 +12254840 +19501336 +19535472 +12572349 +22778927 +32530591 +20271028 +17979881 +21219334 +30911789 +25110555 +29426007 +21812852 +20167468 +21756810 +22098273 +22202862 +37481217 +41924278 +23491753 +22808676 +41292282 +23540149 +24827189 +32073685 +30234721 +50053566 +30552230 +43090436 +54287401 +38147349 +42369301 +55461098 +44015714 +105514664 +41980320 +44565486 +42265741 +42370330 +43855083 +44301135 +81237785 +67346836 +46300429 +96656702 +85996034 +48367338 +53774870 +109748499 +72922560 +60786951 +68699579 +84739631 +86831227 +91922219 +80127669 +119222989 +84246061 +84350650 +85835403 +189876168 +84636071 +86120824 +97629953 +88156218 +90601564 +94667767 +100075299 +155530806 +102142208 +122474449 +109154289 +138125520 +151388515 +172666630 +145423022 +148827248 +164373730 +189281958 +174952214 +164478319 +186492858 +187977611 +229773672 +178757782 +170756895 +172792289 +174277042 +206784242 +185269331 +334288588 +194743066 +209229588 +296885274 +224616657 +231628738 +247279809 +289514035 +294250270 +316179917 +319700064 +313200978 +328852049 +335235214 +389094976 +337270608 +398893699 +343549184 +397408946 +379986483 +358061620 +359546373 +369020108 +380012397 +394498919 +419359723 +514130692 +478908547 +613950334 +471896466 +521142773 +563459726 +583764305 +607451248 +686913669 +654935278 +642053027 +872192312 +672505822 +973496707 +680819792 +701610804 +1034921761 +717607993 +727081728 +728566481 +1235965548 +749032505 +774511316 +915641692 +891256189 +1608864182 +1264584097 +1085846800 +993039239 +1262386526 +1256270127 +1419218797 +1588147514 +1296988305 +1314558849 +1322872819 +1399587550 +2316713995 +1806897881 +1736532565 +1428692532 +1444689721 +1446174474 +2505065597 +1477598986 +1523543821 +2865134309 +2097384135 +1492208709 +1884295428 +2249309366 +2078886039 +2408719619 +2696575855 +2553258432 +2716207102 +2611547154 +2637431668 +3631272844 +2714146399 +3556485025 +3904282016 +5108871830 +3693999087 +2968233542 +2873382253 +2890864195 +6795146211 +5515568272 +3571094748 +3620927956 +6993167258 +3376504137 +3741518075 +8231775374 +4690433193 +4487605658 +5249834287 +5308123009 +5164805586 +5684440644 +5325693553 +5351578067 +5587528652 +5605010594 +5764246448 +7598281103 +8991352362 +10172046302 +15759574954 +6249886390 +15856486946 +6947598885 +6997432093 +7118022212 +12301290267 +8066937330 +7864109795 +8229123733 +9178038851 +15226555826 +10601412354 +13316771617 +13831183778 +23623684749 +14862479495 +15523624369 +10939106719 +14479010123 +11369257042 +12014132838 +13247318483 +24432596132 +13197485275 +13367908602 +13945030978 +18057128931 +14065621097 +16093233528 +18803216514 +17042148646 +15931047125 +22375524126 +26615227085 +19779451205 +21540519073 +21970669396 +35606140170 +26079753935 +22308363761 +22953239557 +23383389880 +24186425202 +24566742317 +24616575525 +25211618113 +26444803758 +57576809566 +26565393877 +27312939580 +43121902581 +29996668222 +43486952404 +32024280653 +49568466642 +32973195771 +41750120601 +47092390785 +44493758630 +67688644898 +47519981874 +46537411713 +47950132197 +45261603318 +45691753641 +47569815082 +86243879231 +48753167519 +62969863993 +49828193638 +51656421871 +53757743338 +53878333457 +56562062099 +95042522982 +74723316372 +76460148175 +64997476424 +79116671438 +114565943066 +118875809881 +92063573712 +91799015031 +97398008720 +90953356959 +92229165354 +92831418400 +105315229618 +141627208669 +161877291717 +96322982601 +116727607331 +98581361157 +260503018550 +114825670062 +105414165209 +211148652663 +146709751857 +152885044700 +359084379707 +139720792796 +163578837581 +144114147862 +170070028397 +189461582432 +222141772540 +182752371990 +203995526366 +183182522313 +185060583754 +209559025731 +189154401001 +335864152858 +194904343758 +293485704915 +201737147810 +213407031219 +220239835271 +245134958005 +249528313071 +463555733312 +331770335611 +402413357771 +283834940658 +303299630377 +307692985443 +327296670175 +417402557585 +408311374977 +365934894303 +513725540186 +444432656829 +398713426732 +368243106067 +525018553859 +503044730646 +522201013933 +396641491568 +465374793276 +587134571035 +832711539302 +433646866490 +735608045152 +720702187962 +533363253729 +630596300552 +649769834961 +591527926101 +610992615820 +634989655618 +675936091510 +693231564478 +762576385871 +764884597635 +1114037346466 +766956532799 +795354918300 +1198127186855 +1064243167042 +830288358058 +862016284844 +918842505501 +1200982838428 +899021659766 +967010120219 +1025174792591 +1044639482310 +1124891179830 +1163959554281 +1209299345239 +1202520541921 +1245982271438 +1226517581719 +1720575573820 +1974183942874 +1780858790345 +1944017298092 +1557931304171 +1531841130434 +1562311451099 +1597244890857 +1625643276358 +1817864165267 +1887191077435 +1797298478277 +2082802059782 +1866031779985 +1924196452357 +1943661142076 +2981823719548 +2069814274901 +2288850734111 +2327411721751 +2455281616677 +2758358712153 +4301595664625 +2472499853157 +3200701524593 +3089772434605 +3094152581533 +3120242755270 +3129086021291 +4269798331434 +4621613565039 +3809692922061 +3222888167215 +4232511876187 +3615162643544 +3663330258262 +3721494930634 +3790228232342 +6886218425477 +3867857594433 +6282192775218 +4358665009012 +6792416279553 +4616262455862 +4782693338428 +4927781469834 +5601585874448 +5562272287762 +6753102692867 +6210015189875 +6183925016138 +6214395336803 +6249328776561 +6351974188506 +7384825188896 +6838050810759 +7032581089276 +6944383097849 +7278492901806 +7453558490604 +13135547202038 +10039557008903 +7658085826775 +8226522603445 +8484120050295 +8974927464874 +14177241468449 +9544043925696 +9398955794290 +16152058487157 +10490053757596 +13158778434652 +11746197303900 +12393940206013 +12398320352941 +12433253792699 +12463724113364 +14329208286745 +13296357286355 +28550378840098 +21290241229596 +13976964187125 +14397941588453 +14732051392410 +15680081094049 +15884608430220 +49840620069694 +16142205877070 +25696102412853 +17459047515169 +39040432597694 +21862679907654 +24131007186700 +19889009551886 +33343655945389 +22236251061496 +24140137509913 +35773617982106 +33185366838241 +24831574145640 +27694298874808 +85614238051800 +52681386026798 +28976438380404 +50786629901594 +37174849659816 +28374905775578 +31856989103622 +39321727422823 +31564689524269 +32026814307290 +33601253392239 +47583308426694 +57553091516475 +65628067699529 +41751689459540 +56069204650386 +57316374024941 +42125260613382 +90738458354716 +48971711655553 +51834436384721 +53206479921218 +52525873020448 +76496577082639 +88881063549210 \ No newline at end of file From 8d20ab3aed7b5f17243a33934f6d8373e6a07049 Mon Sep 17 00:00:00 2001 From: doniacld Date: Thu, 15 Jul 2021 22:18:35 +0200 Subject: [PATCH 2/3] Fix uts --- puzzles/2020/09/cypher/oddfinder_test.go | 2 +- puzzles/2020/09/cypher/parser_test.go | 2 +- puzzles/2020/09/day09.go | 4 ++-- puzzles/2020/09/input-test.txt | 20 -------------------- 4 files changed, 4 insertions(+), 24 deletions(-) delete mode 100644 puzzles/2020/09/input-test.txt diff --git a/puzzles/2020/09/cypher/oddfinder_test.go b/puzzles/2020/09/cypher/oddfinder_test.go index 93e5458..3b8f752 100644 --- a/puzzles/2020/09/cypher/oddfinder_test.go +++ b/puzzles/2020/09/cypher/oddfinder_test.go @@ -20,7 +20,7 @@ func TestCypher_FindOdd(t *testing.T) { t.Run(tc.description, func(t *testing.T) { c, err := ReadAndExtract(tc.input, tc.idxPreamble) assert.NoError(t, err) - v := c.(tc.idxPreamble) + v := c.FindOdd(tc.idxPreamble) assert.Equal(t, tc.output, v) }) } diff --git a/puzzles/2020/09/cypher/parser_test.go b/puzzles/2020/09/cypher/parser_test.go index ff98789..69c0355 100644 --- a/puzzles/2020/09/cypher/parser_test.go +++ b/puzzles/2020/09/cypher/parser_test.go @@ -14,7 +14,7 @@ func TestReadAndExtract(t *testing.T) { output Cypher }{ {"nominal case", "./resources/input-test-small.txt", 2, - Cypher{Preamble: Preamble{1: {}, 2: {}}, Numbers: []int{3, 4}}, + Cypher{Preamble: Preamble{1: {}, 2: {}}, Numbers: []int{1, 2, 3, 4}}, }} for _, tc := range tt { diff --git a/puzzles/2020/09/day09.go b/puzzles/2020/09/day09.go index 8761af1..e608d5c 100644 --- a/puzzles/2020/09/day09.go +++ b/puzzles/2020/09/day09.go @@ -19,10 +19,10 @@ type day09 struct { func (d day09) Solve() (string, error) { c, err := cypher.ReadAndExtract(d.fileName, d.idxPreamble) if err != nil { - return err + return "", err } - noSumValue := c.FindIntruder(d.idxPreamble) + noSumValue := c.FindOdd(d.idxPreamble) out := fmt.Sprintf("First number to not respect the rule is: %d ", noSumValue) return out, nil } diff --git a/puzzles/2020/09/input-test.txt b/puzzles/2020/09/input-test.txt deleted file mode 100644 index cda4246..0000000 --- a/puzzles/2020/09/input-test.txt +++ /dev/null @@ -1,20 +0,0 @@ -35 -20 -15 -25 -47 -40 -62 -55 -65 -95 -102 -117 -150 -182 -127 -219 -299 -277 -309 -576 \ No newline at end of file From 7e5c09d45b45952bb501ed2b10d00ebdcb75974e Mon Sep 17 00:00:00 2001 From: doniacld Date: Sat, 17 Jul 2021 16:26:47 +0200 Subject: [PATCH 3/3] Day 9 - Part2 --- puzzles/2020/09/README.md | 6 +-- puzzles/2020/09/cypher/oddfinder.go | 7 ++-- puzzles/2020/09/cypher/oddfinder_test.go | 7 +++- puzzles/2020/09/cypher/sumfinder.go | 49 ++++++++++++++++++++++++ puzzles/2020/09/cypher/sumfinder_test.go | 30 +++++++++++++++ puzzles/2020/09/day09.go | 5 ++- 6 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 puzzles/2020/09/cypher/sumfinder.go create mode 100644 puzzles/2020/09/cypher/sumfinder_test.go diff --git a/puzzles/2020/09/README.md b/puzzles/2020/09/README.md index 217828c..4298b28 100644 --- a/puzzles/2020/09/README.md +++ b/puzzles/2020/09/README.md @@ -85,8 +85,4 @@ To find the encryption weakness, add together the smallest and largest number in What is the encryption weakness in your XMAS-encrypted list of numbers? -Answer: - -Although it hasn't changed, you can still get your puzzle input. - -You can also [Shareon Twitter Mastodon] this puzzle. +Your puzzle answer was 238243506. \ No newline at end of file diff --git a/puzzles/2020/09/cypher/oddfinder.go b/puzzles/2020/09/cypher/oddfinder.go index 6b05602..feda884 100644 --- a/puzzles/2020/09/cypher/oddfinder.go +++ b/puzzles/2020/09/cypher/oddfinder.go @@ -5,20 +5,20 @@ import "math" // Find the values that is not the sum of two values is the preamble map // the map contains the x previous values // x is a range that begins at 0 and ends at the given preamble index -func (c Cypher) FindOdd(idxEndPreamble int) int { +func (c Cypher) FindOdd(idxEndPreamble int) (int, int) { idxStartPreamble := 0 for i := idxEndPreamble; i < len(c.Numbers); i++ { elt := c.Numbers[i] if !c.findPair(elt) { - return elt + return elt, i } c.updatePreamble(idxStartPreamble, idxEndPreamble) idxStartPreamble++ } // should not happened - return 0 + return 0, 0 } // findPair returns the odd value if there is no pair summing it @@ -44,4 +44,3 @@ func (c Cypher) updatePreamble(idxToRemove, idxPreamble int) { // add the next element c.Preamble[c.Numbers[idxToRemove+idxPreamble]] = struct{}{} } - diff --git a/puzzles/2020/09/cypher/oddfinder_test.go b/puzzles/2020/09/cypher/oddfinder_test.go index 3b8f752..34129ec 100644 --- a/puzzles/2020/09/cypher/oddfinder_test.go +++ b/puzzles/2020/09/cypher/oddfinder_test.go @@ -12,16 +12,19 @@ func TestCypher_FindOdd(t *testing.T) { input string idxPreamble int output int + expectedIdx int }{ - {"nominal case", "./resources/input-test.txt", 5, 127}, + {"nominal case", "./resources/input-test.txt", 5, 127, 14}, } for _, tc := range tt { t.Run(tc.description, func(t *testing.T) { c, err := ReadAndExtract(tc.input, tc.idxPreamble) assert.NoError(t, err) - v := c.FindOdd(tc.idxPreamble) + v, i := c.FindOdd(tc.idxPreamble) assert.Equal(t, tc.output, v) + assert.Equal(t, tc.expectedIdx, i) + }) } } diff --git a/puzzles/2020/09/cypher/sumfinder.go b/puzzles/2020/09/cypher/sumfinder.go new file mode 100644 index 0000000..9e569a8 --- /dev/null +++ b/puzzles/2020/09/cypher/sumfinder.go @@ -0,0 +1,49 @@ +package cypher + +// FindSum browses the list of numbers until the retrieved value in part 1 and +// finds a list of contiguous values that sum up make this target value. +// It returns the sum of the min and max of this list. +func (c Cypher) FindSum(idx int) int { + target := c.Numbers[idx] + + sum := 0 + tmpArray := make([]int, 0) + for i := 0; i < idx-1; i++ { + + // goal reached + if sum == target { + return sumMinMax(tmpArray) + } + + // delete first value from the array until + // the sum is below the target + for sum > target { + sum -= tmpArray[0] + s := tmpArray[1:] + tmpArray = s + } + + // add the next value in the array + if sum < target { + tmpArray = append(tmpArray, c.Numbers[i]) + sum += c.Numbers[i] + } + } + return 0 +} + +// sumMinMax returns the sum of the min and max values from a given array +func sumMinMax(sum []int) int { + min, max := sum[0], 0 + for _, v := range sum { + + if v < min { + min = v + } + + if v > max { + max = v + } + } + return min + max +} diff --git a/puzzles/2020/09/cypher/sumfinder_test.go b/puzzles/2020/09/cypher/sumfinder_test.go new file mode 100644 index 0000000..75bc667 --- /dev/null +++ b/puzzles/2020/09/cypher/sumfinder_test.go @@ -0,0 +1,30 @@ +package cypher + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCypher_FindSum(t *testing.T) { + tt := []struct { + description string + file string + input int + output int + }{ + { + "nominal case", "./resources/input-test.txt", 14, 62, + }, + } + + for _, tc := range tt { + t.Run(tc.description, func(t *testing.T) { + c, err := ReadAndExtract(tc.file, 5) + assert.NoError(t, err) + out := c.FindSum(tc.input) + assert.Equal(t, tc.output, out) + }) + } + +} diff --git a/puzzles/2020/09/day09.go b/puzzles/2020/09/day09.go index e608d5c..f0a8c2f 100644 --- a/puzzles/2020/09/day09.go +++ b/puzzles/2020/09/day09.go @@ -22,7 +22,8 @@ func (d day09) Solve() (string, error) { return "", err } - noSumValue := c.FindOdd(d.idxPreamble) - out := fmt.Sprintf("First number to not respect the rule is: %d ", noSumValue) + noSumValue, idx := c.FindOdd(d.idxPreamble) + sumMinMax := c.FindSum(idx) + out := fmt.Sprintf("First number to not respect the rule is: %d at index %d and the sum of the min & max: %d", noSumValue, idx, sumMinMax) return out, nil }