Code Monkey home page Code Monkey logo

advent-of-code-2021's Introduction

advent-of-code-2021

AOC in go

Template for Playground

I use golang playground to avoid auto-completion from Github CoPilot.

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	fmt.Println("part1 (example):", part1(example))
	//fmt.Println("part1 (input):", part1(input))
	//fmt.Println("part2 (example):", part2(example))
	//fmt.Println("part2 (input):", part2(input))
}

func parse(input string) []string {
	input = strings.TrimSpace(input)
	lines := strings.Split(input, "\n")
	result := make([]string, len(lines))
	for i, line := range lines {
		result[i] = strings.TrimSpace(line)
	}
	return result
}

func part1(input string) int {
	lines := parse(input)
	_ = lines
	return 0
}

func part2(input string) int {
	lines := parse(input)
	_ = lines
	return 0
}

func toInt(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return n
}

var example = ``
var input = ``

Template V2 for Playground

Since there's always a lot of logic reuse between part 1 and part 2, I end up copying the logic rather than writing functions to reuse. Testing if using a single struct to share state would be better.

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	s := NewSolver()
	fmt.Println("part1 (example):", s.WithInput(example).Part1()) // 0
	//fmt.Println("part1 (input):", s.WithInput(input).Part1())     // 0
	//fmt.Println("part2 (example):", s.WithInput(example).Part2()) // 0
	//fmt.Println("part2 (input):", s.WithInput(input).Part2())     // 0
}

type Solver struct {
	input string
	state []string
}

func NewSolver() *Solver {
	return &Solver{}
}

func (s *Solver) WithInput(input string) *Solver {
	s.input = input
	s.state = parseLines(parse(s.input))
	return s
}

func (s *Solver) Part1() int {
	state := s.state
	return len(state)
}

func (s *Solver) Part2() int {
	state := s.state
	return len(state)
}

func parse(input string) []string {
	input = strings.TrimSpace(input)
	lines := strings.Split(input, "\n")
	result := make([]string, len(lines))
	for i, line := range lines {
		result[i] = strings.TrimSpace(line)
	}
	return result
}

func parseLines(lines []string) []string {
	return lines
}

func toInt(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		panic(err)
	}
	return n
}

var (
	example = ``
	input   = ``
)

advent-of-code-2021's People

Contributors

alextanhongpin avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.