From 566c9aed295cd105eb769dc65f549f2d1edb6b1f Mon Sep 17 00:00:00 2001 From: Oleg Balunenko Date: Wed, 10 Feb 2021 01:10:13 +0300 Subject: [PATCH] puzzles/solutions/2015: Implement day01 part2 --- .../puzzles/solutions/2015/day01/elevator.go | 41 ++++++++++++ .../puzzles/solutions/2015/day01/solution.go | 65 +++++++++++-------- .../solutions/2015/day01/solution_test.go | 17 +++-- internal/puzzles/solutions/2015/day01/spec.md | 27 ++++++-- 4 files changed, 111 insertions(+), 39 deletions(-) create mode 100644 internal/puzzles/solutions/2015/day01/elevator.go diff --git a/internal/puzzles/solutions/2015/day01/elevator.go b/internal/puzzles/solutions/2015/day01/elevator.go new file mode 100644 index 00000000..f30b9128 --- /dev/null +++ b/internal/puzzles/solutions/2015/day01/elevator.go @@ -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() + } +} diff --git a/internal/puzzles/solutions/2015/day01/solution.go b/internal/puzzles/solutions/2015/day01/solution.go index af0f0a91..2d4f21df 100644 --- a/internal/puzzles/solutions/2015/day01/solution.go +++ b/internal/puzzles/solutions/2015/day01/solution.go @@ -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() @@ -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 } diff --git a/internal/puzzles/solutions/2015/day01/solution_test.go b/internal/puzzles/solutions/2015/day01/solution_test.go index 8838b7e7..06b629a1 100644 --- a/internal/puzzles/solutions/2015/day01/solution_test.go +++ b/internal/puzzles/solutions/2015/day01/solution_test.go @@ -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 } @@ -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, }, } diff --git a/internal/puzzles/solutions/2015/day01/spec.md b/internal/puzzles/solutions/2015/day01/spec.md index 01c1886c..142038f5 100644 --- a/internal/puzzles/solutions/2015/day01/spec.md +++ b/internal/puzzles/solutions/2015/day01/spec.md @@ -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. @@ -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? \ No newline at end of file