Skip to content
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

Implement Day03 solution #6

Merged
merged 20 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
script:
- echo "Run unit tests..."
- go vet $(go list ./...)
- go test -v -race $(go list ./...)
- make test-cover
after_success:
- bash <(curl -s https://codecov.io/bash)

- stage: Build
go:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Build Status](https://travis-ci.com/oleg-balunenko/advent-of-code.svg?branch=master)](https://travis-ci.com/oleg-balunenko/advent-of-code)
[![Go Report Card](https://goreportcard.com/badge/github.com/oleg-balunenko/advent-of-code)](https://goreportcard.com/report/github.com/oleg-balunenko/advent-of-code)
[![GoDoc](https://godoc.org/github.com/oleg-balunenko/advent-of-code?status.svg)](https://godoc.org/github.com/oleg-balunenko/advent-of-code)

# advent-of-code
Expand All @@ -15,4 +16,4 @@ Run it and follow instructions

### Demo

![cli demo](./.assets/demo.png)
![cli demo](./.assets/demo.png)
10 changes: 6 additions & 4 deletions cmd/aoc-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"

"github.com/manifoldco/promptui"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/oleg-balunenko/advent-of-code/puzzles"
// register all solutions
Expand Down Expand Up @@ -165,9 +164,12 @@ func run(puzzle string, inputdir string) (puzzles.Result, error) {
return puzzles.Result{}, errors.Wrap(err, "failed to get solver")
}

input := filepath.Clean(
filepath.Join(inputdir, fmt.Sprintf("%s.txt", s.Name())),
input, err := os.Open(filepath.Clean(
filepath.Join(inputdir, fmt.Sprintf("%s.txt", s.Name()))),
)
if err != nil {
return puzzles.Result{}, errors.Wrap(err, "failed to open input data")
}

res, err := puzzles.Run(s, input)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions input/2019/day03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
R1008,U336,R184,D967,R451,D742,L235,U219,R57,D439,R869,U207,L574,U670,L808,D675,L203,D370,L279,U448,L890,U297,R279,D613,L411,D530,L372,D88,R986,U444,R319,D95,L385,D674,R887,U855,R794,U783,R633,U167,L587,D545,L726,D196,R681,U609,R677,U881,R153,D724,L63,U246,R343,U315,R580,U872,L516,U95,R463,D809,R9,U739,R540,U670,L434,D699,L158,U47,L383,D483,L341,U61,R933,D269,R816,D589,R488,D169,R972,D534,L995,D277,L887,D657,R628,D322,R753,U813,L284,D237,R676,D880,L50,D965,L401,D619,R858,U313,L156,U535,R664,U447,L251,U168,L352,U881,L734,U270,L177,D903,L114,U998,L102,D149,R848,D586,L98,D157,R942,U496,R857,U362,R398,U86,R469,U358,L721,D631,R176,D365,L112,U472,L557,D153,R97,D639,L457,U566,R570,U106,R504,D292,L94,U499,R358,U653,L704,D627,R544,D24,L407,U513,R28,U643,L510,U579,R825,D376,L867,U999,R134,D734,R654,D989,L920,U872,R64,U626,R751,D425,R620,U274,L471,D83,R979,U577,L43,D320,R673,D187,R300,U134,L451,D717,R857,U576,R570,U988,R745,U840,R799,U809,R573,U354,L208,D976,L417,U473,L555,U563,L955,U823,R712,D869,L145,D735,L780,D74,R421,U42,L158,U689,R718,D455,L670,U128,L744,U401,R149,U102,L122,U832,R872,D40,R45,D325,L553,U980,L565,D497,L435,U647,L209,D822,L593,D28,R936,U95,R349,U511,L243,U895,R421,U336,L986,U264,R376,D183,R480,D947,R416,D706,R118,D799,R424,D615,R384,U185,L338,U14,R576,D901,L734,D417,L62,D254,R784,D973,R987,D848,R32,D72,L535,D633,L668,D664,R308,D474,L418,D39,L473,U388,L518,D544,R118,D948,L844,D956,R605,U14,L948,D78,L689,U443,L996,U932,R81,D879,R556,D633,R131
L993,U227,L414,U228,L304,U53,R695,U765,R162,D264,L530,U870,R771,D395,R27,D200,L235,D834,L559,D128,R284,U912,L959,U358,R433,U404,L539,U799,R271,D734,L104,U261,R812,D15,L474,U887,R606,U366,L694,U156,R385,D667,L329,D434,R745,U776,L319,U756,R208,D457,R705,U999,R284,U98,L657,U214,R639,D937,R675,U444,L891,D587,L914,D4,R294,D896,R534,D584,L887,U878,L807,U202,R505,U234,L284,D5,R667,U261,L127,D482,R777,D223,L707,D468,L606,U345,L509,D967,R437,U995,R28,D376,L2,D959,L814,U702,L38,D154,L79,D439,L259,U143,L376,U700,R894,U165,L300,D58,R631,D47,R684,U935,R262,D857,R797,D599,L705,U792,R439,D444,R398,D887,L81,D40,R671,U332,L820,U252,R691,U412,L794,D661,R810,U157,R858,U566,R892,D543,R10,D725,L653,D812,R733,D804,R816,U862,R994,U221,L33,U271,R766,D591,R575,D970,R152,D693,L916,U404,L658,D847,L605,D433,L583,U587,L129,D103,R407,U780,L901,D676,L846,D687,L9,D47,R295,D597,L808,U134,L186,D676,L62,U305,L73,D369,L468,U30,L472,U280,L413,U961,L98,D966,R308,D178,L21,D789,L871,D671,R665,U927,L906,U633,L135,D894,R110,D205,R324,D665,R143,D450,L978,U385,R442,D853,L518,U542,R211,U857,R119,D872,L246,U380,L874,U463,R153,U982,R832,D784,L652,U545,R71,U386,R427,D827,R986,U870,R959,U232,R509,U675,R196,U389,R944,U149,R152,U571,R527,U495,L441,U511,L899,D996,L707,D455,L358,D423,L14,D427,R144,D703,L243,U157,R876,D538,R26,D577,L385,U622,L149,D852,L225,U475,L811,D520,L226,U523,L338,D79,R565,U766,L609,U496,L189,D446,R63,U396,L629,U312,L841,D639,R466,U808,L60,D589,L146,U114,R165,U96,L809,D704,L61
5 changes: 0 additions & 5 deletions puzzles/solutions/2019/day02/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/oleg-balunenko/advent-of-code/puzzles"
)
Expand Down Expand Up @@ -62,10 +61,6 @@ func (s solution) Part2(input io.Reader) (string, error) {
}

if res == 19690720 {
log.WithFields(log.Fields{
"noun": i,
"verb": j,
}).Info("Solved at positions")
return strconv.Itoa(nounVerb(i, j)), nil
}
}
Expand Down
218 changes: 218 additions & 0 deletions puzzles/solutions/2019/day03/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package day03

import (
"bufio"
"io"
"sort"
"strconv"
"strings"

"github.com/pkg/errors"

"github.com/oleg-balunenko/advent-of-code/puzzles"
)

func init() {
puzzleName, err := puzzles.MakeName("2019", "day03")
if err != nil {
panic(err)
}

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

type solution struct {
name string
}

func (s solution) Part1(input io.Reader) (string, error) {
wires, err := runWires(input)
if err != nil {
return "", err
}

cross := findCross(wires[0], wires[1])

mds := make([]int, 0, len(cross))

for _, p := range cross {
md := p.manhattan()
mds = append(mds, md)
}

sort.Ints(mds)

return strconv.Itoa(mds[0]), nil
}

func (s solution) Part2(input io.Reader) (string, error) {
wires, err := runWires(input)
if err != nil {
return "", err
}

cross := findCross(wires[0], wires[1])

stps := make([]int, 0, len(cross))

for _, c := range cross {
sum := wires[0][c] + wires[1][c]
stps = append(stps, sum)
}

sort.Ints(stps)

return strconv.Itoa(stps[0]), nil
}

func (s solution) Name() string {
return s.name
}

type wire struct {
pos pos
step stepper
m map[pos]int
}

type pos struct {
x int
y int
}

type stepper struct {
steps int
}

func (s *stepper) add() {
s.steps++
}

func (s stepper) get() int {
return s.steps
}

func makeWire() wire {
return wire{
pos: pos{
x: 0,
y: 0,
},
m: make(map[pos]int),
}
}

const (
moveUp = "U"
moveDown = "D"
moveRight = "R"
moveLeft = "L"
)

func (w *wire) run(input string) error {
moves := strings.Split(input, ",")
for _, m := range moves {
move := m

act := string(move[0])

steps, err := strconv.Atoi(move[1:])
if err != nil {
return errors.New("failed to parse steps")
}

switch act {
case moveDown:
w.down(steps)
case moveUp:
w.up(steps)
case moveLeft:
w.left(steps)
case moveRight:
w.right(steps)
}
}

return nil
}

func (w *wire) up(n int) {
for i := 0; i < n; i++ {
w.pos.y++
w.storePosition()
}
}

func (w *wire) down(n int) {
for i := 0; i < n; i++ {
w.pos.y--
w.storePosition()
}
}

func (w *wire) left(n int) {
for i := 0; i < n; i++ {
w.pos.x--
w.storePosition()
}
}

func (w *wire) right(n int) {
for i := 0; i < n; i++ {
w.pos.x++
w.storePosition()
}
}

func (w *wire) storePosition() {
w.step.add()
w.m[w.pos] = w.step.get()
}

func (p pos) manhattan() int {
x, y := p.x, p.y

if x < 0 {
x = -x
}

if y < 0 {
y = -y
}

return x + y
}

func findCross(wm1 map[pos]int, wm2 map[pos]int) []pos {
res := make([]pos, 0, len(wm1))

for p := range wm1 {
p := p

if _, exist := wm2[p]; exist {
res = append(res, p)
}
}

return res
}

func runWires(input io.Reader) ([]map[pos]int, error) {
scanner := bufio.NewScanner(input)
res := make([]map[pos]int, 0, 2)

for scanner.Scan() {
line := scanner.Text()

w := makeWire()
if err := w.run(line); err != nil {
return nil, errors.Wrap(err, "failed to run wire")
}

res = append(res, w.m)
}

return res, nil
}
Loading