-
Notifications
You must be signed in to change notification settings - Fork 0
Day 9 - Part 1 & 2 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# 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? | ||
|
||
Your puzzle answer was 238243506. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{}{} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||
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, int) { | ||||||
idxStartPreamble := 0 | ||||||
for i := idxEndPreamble; i < len(c.Numbers); i++ { | ||||||
|
||||||
elt := c.Numbers[i] | ||||||
if !c.findPair(elt) { | ||||||
return elt, i | ||||||
} | ||||||
|
||||||
c.updatePreamble(idxStartPreamble, idxEndPreamble) | ||||||
idxStartPreamble++ | ||||||
} | ||||||
// should not happened | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return 0, 0 | ||||||
} | ||||||
|
||||||
// findPair returns the odd value if there is no pair summing it | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it returns a bool |
||||||
func (c Cypher) findPair(elt int) bool { | ||||||
for p := range c.Preamble { | ||||||
toFind := elt - p | ||||||
|
||||||
f := math.Abs(float64(toFind)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or you could write your own |
||||||
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{}{} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i supppose this will always work, but is there any safety net somewhere? c.Numbers is an array, after all. |
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 | ||
expectedIdx int | ||
}{ | ||
{"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, i := c.FindOdd(tc.idxPreamble) | ||
assert.Equal(t, tc.output, v) | ||
assert.Equal(t, tc.expectedIdx, i) | ||
|
||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, basically, always return elt, err ? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{1, 2, 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) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1 | ||
2 | ||
3 | ||
4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
35 | ||
20 | ||
15 | ||
25 | ||
47 | ||
40 | ||
62 | ||
55 | ||
65 | ||
95 | ||
102 | ||
117 | ||
150 | ||
182 | ||
127 | ||
219 | ||
299 | ||
277 | ||
309 | ||
576 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or |
||
} | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's better to also initialise max to sum[0]. just in case you encounter negative numbers |
||
for _, v := range sum { | ||
|
||
if v < min { | ||
min = v | ||
} | ||
|
||
if v > max { | ||
max = v | ||
} | ||
} | ||
return min + max | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not the name of the func