Skip to content

Commit

Permalink
puzzles/solutions/2015: Implement day01 part2
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Feb 9, 2021
1 parent 9b3ef6b commit 566c9ae
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 39 deletions.
41 changes: 41 additions & 0 deletions internal/puzzles/solutions/2015/day01/elevator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package day01

type move string

const (
up move = "("
down move = ")"

basement = -1
)

type elevator struct {
floor int
}

func newElevator() *elevator {
return &elevator{
floor: 0,
}
}

func (e *elevator) Up() {
e.floor++
}

func (e *elevator) Down() {
e.floor--
}

func (e elevator) Floor() int {
return e.floor
}

func (e *elevator) Move(m move) {
switch m {
case up:
e.Up()
case down:
e.Down()
}
}
65 changes: 37 additions & 28 deletions internal/puzzles/solutions/2015/day01/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ type solution struct {
name string
}

const (
up = "("
down = ")"
)
func (s solution) Name() string {
return s.name
}

func (s solution) Year() string {
return s.year
}

func init() {
puzzles.Register(solution{
year: year,
name: puzzleName,
})
}

func (s solution) Part1(in io.Reader) (string, error) {
reader := bufio.NewReader(in)

var floor int
e := newElevator()

for {
r, _, err := reader.ReadRune()
Expand All @@ -40,34 +50,33 @@ func (s solution) Part1(in io.Reader) (string, error) {
return "", fmt.Errorf("read rune: %w", err)
}

s := string(r)

switch s {
case up:
floor++
case down:
floor--
}
e.Move(move(r))
}

return strconv.Itoa(floor), nil
return strconv.Itoa(e.Floor()), nil
}

func (s solution) Part2(_ io.Reader) (string, error) {
return "", puzzles.ErrNotImplemented
}
func (s solution) Part2(in io.Reader) (string, error) {
reader := bufio.NewReader(in)

func (s solution) Name() string {
return s.name
}
e := newElevator()

func (s solution) Year() string {
return s.year
}
var pos int

func init() {
puzzles.Register(solution{
year: year,
name: puzzleName,
})
for e.Floor() != basement {
r, _, err := reader.ReadRune()
if err != nil {
if errors.Is(err, io.EOF) {
break
}

return "", fmt.Errorf("read rune: %w", err)
}

pos++

e.Move(move(r))
}

return strconv.Itoa(pos), nil
}
17 changes: 13 additions & 4 deletions internal/puzzles/solutions/2015/day01/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ func Test_solution_Part1(t *testing.T) {
}

func Test_solution_Part2(t *testing.T) {
t.Skip()

type fields struct {
name string
}
Expand All @@ -170,9 +168,20 @@ func Test_solution_Part2(t *testing.T) {
name: "",
},
args: args{
input: strings.NewReader(""),
input: strings.NewReader(")"),
},
want: "1",
wantErr: false,
},
{
name: "",
fields: fields{
name: "",
},
args: args{
input: strings.NewReader("()())"),
},
want: "",
want: "5",
wantErr: false,
},
}
Expand Down
27 changes: 20 additions & 7 deletions internal/puzzles/solutions/2015/day01/spec.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# --- Day 1: Not Quite Lisp ---

## Part1
## Part One
Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars,
and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.

Expand All @@ -20,10 +20,23 @@ The apartment building is very tall, and the basement is very deep; he will neve

For example:

- `(())` and `()()` both result in floor `0`.
- `(((` and `(()(()(` both result in floor `3`.
- `))(((((` also results in floor `3`.
- `())` and `))(` both result in floor `-1` (the first basement level).
- `)))` and `)())())` both result in floor `-3`.
- `(())` and `()()` both result in floor `0`.
- `(((` and `(()(()(` both result in floor `3`.
- `))(((((` also results in floor `3`.
- `())` and `))(` both result in floor `-1` (the first basement level).
- `)))` and `)())())` both result in floor `-3`.

To what floor do the instructions take Santa?

## --- Part Two ---
Now, given the same instructions, find the position of the first character that causes him to enter the
basement (floor -1).
The first character in the instructions has position 1, the second character has position 2, and so on.

For example:

- `)` causes him to enter the basement at character position `1`.
- `()())` causes him to enter the basement at character position `5`.

What is the position of the character that causes Santa to first enter the basement?

To what floor do the instructions take Santa?

0 comments on commit 566c9ae

Please sign in to comment.