This year, I’m posting my solutions to the puzzles in the Advent of Code. Each post will consist of pseudocode, with a link to the actual code I used to solve the puzzle on Github.

And now, on with the solution:

### Day 3, part 1

Simplified problem: You are given a design document with specifications for triangles:

1 2 3 4 5 6 |
101 301 501 102 302 502 103 303 503 201 401 601 202 402 602 203 403 603 |

Each line represents the lengths of three sides of a triangle.

Given the rule that **a valid triangle is a triangle where the sum of any two sides is greater than the length of the remaining side**, how many of these triangles are valid?

#### Solution

Create a *Triangle* object:

*Lengths=List of 3 lengths**LongestLength=Maximum(Lengths)**SumOfRemainingSides=*(the sum of the remaining 2 lengths in*Lengths*)*IsValidTriangle=SumOfRemainingSides > LongestLength*

- Set
*Triangles=new List of Triangle* - Set
*input*=(input) - For each
*line*in*input*- Split line using the
**space**character as the separator, to get*lengths=*(an array of 3 numbers) - Set
*triangle=new Triangle(Lengths=lengths)* - Add
*triangle*to*Triangles*

- Split line using the
- End For
- Return number of
*triangles**Triangles*where*IsValidTriangle=true*

### Day 3, part 2

Using the same input, the specifications for each triangle is listed in columns, not rows. Given the input above, this would create 6 triangles, each containing lengths in the same hundreds (101,102,103 etc).

#### Solution

- Set
*Triangles=new List of Triangle* - Set
*input*=(input) - Set
*triangleLengths*=(array containing 3 numbers) - Set
*LengthSide=0* - For
*i=1 to 3*(loop through columns)- For each
*line*in*input*- Split line using the
**space**character as the separator, to get*lengths=*(an array of 3 numbers) - Set
*triangleLengths[0]=lengths[i]*(i=1 to 3, 1st to 3rd column) - Set
*lengthSide = lengthSide+1* - If
*lengthSide=3*(triangleLengths now has three sides – add triangle and reset for next triangle)- Set
*lengthSide=0* - Set
*triangle=new Triangle(Lengths=triangleLengths)* - Add
*triangle*to*Triangles*

- Set
- End If

- Split line using the
- End For

- For each
- End For
- Return number of
*triangles**Triangles*where*IsValidTriangle=true*

You can find the full solution on Github