├── 2021 ├── day01 │ └── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day02 │ └── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go └── day03 │ └── enrichman │ ├── input.txt │ ├── main.go │ ├── parts.go │ └── parts_test.go ├── 2022 ├── day01 │ ├── 8lall0 │ │ ├── advent.go │ │ ├── advent_test.go │ │ ├── input.txt │ │ └── main.go │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ └── parts.go │ ├── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── omissis │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day02 │ ├── 8lall0 │ │ ├── advent.go │ │ ├── advent_test.go │ │ ├── input.txt │ │ └── main.go │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day03 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day04 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day05 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ └── parts.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day06 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day07 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── elf-directory.go │ │ ├── elf-file.go │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day08 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day09 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── giulianopz │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day10 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day11 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ ├── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── monkey.go │ │ ├── parts.go │ │ └── parts_test.go │ └── misterdelle │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day12 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── p_queue.go │ │ ├── parts.go │ │ ├── parts_test.go │ │ ├── short_path.go │ │ ├── tile.go │ │ └── world.go │ ├── lucianoq │ │ └── README.md │ └── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day13 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ └── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parse_old2.go │ │ ├── parts.go │ │ ├── parts_new.go │ │ ├── parts_old.go │ │ └── parts_test.go ├── day14 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── alessio-perugini │ │ ├── go.mod │ │ ├── go.sum │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ └── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day15 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ ├── lucianoq │ │ └── README.md │ └── mastrogiovanni │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go ├── day16 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── p_queue.go │ │ ├── parts.go │ │ ├── parts_test.go │ │ └── short_path.go │ └── lucianoq │ │ └── README.md ├── day17 │ ├── Al-Pragliola │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── lucianoq │ │ └── README.md ├── day18 │ └── lucianoq │ │ └── README.md ├── day19 │ └── lucianoq │ │ └── README.md ├── day20 │ └── lucianoq │ │ └── README.md ├── day21 │ └── lucianoq │ │ └── README.md ├── day22 │ └── lucianoq │ │ └── README.md ├── day23 │ └── lucianoq │ │ └── README.md ├── day24 │ └── lucianoq │ │ └── README.md └── day25 │ └── lucianoq │ └── README.md ├── 2023 ├── day01 │ ├── enrichman │ │ ├── input.txt │ │ ├── main.go │ │ ├── parts.go │ │ └── parts_test.go │ └── lucianoq │ │ └── README.md ├── day02 │ └── lucianoq │ │ └── README.md ├── day03 │ └── lucianoq │ │ └── README.md ├── day04 │ └── lucianoq │ │ └── README.md ├── day05 │ └── lucianoq │ │ └── README.md ├── day06 │ └── lucianoq │ │ └── README.md ├── day07 │ └── lucianoq │ │ └── README.md ├── day08 │ └── lucianoq │ │ └── README.md ├── day09 │ └── lucianoq │ │ └── README.md ├── day10 │ └── lucianoq │ │ └── README.md ├── day11 │ └── lucianoq │ │ └── README.md ├── day12 │ └── lucianoq │ │ └── README.md ├── day13 │ └── lucianoq │ │ └── README.md ├── day14 │ └── lucianoq │ │ └── README.md ├── day15 │ └── lucianoq │ │ └── README.md └── day16 │ └── lucianoq │ └── README.md ├── .env.sample ├── .github └── workflows │ ├── ci.yml │ └── readme.yml ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── assets └── README.md.tmpl ├── cmd └── aoc │ └── main.go ├── go.mod ├── go.sum ├── internal └── challenge │ ├── challenge.go │ ├── teams.go │ ├── template.go │ └── users.go ├── pkg └── aoc │ └── aoc.go ├── scripts └── new-puzzle.sh ├── teams.yaml ├── template ├── main.go ├── parts.go └── parts_test.go └── utils └── utils.go /.env.sample: -------------------------------------------------------------------------------- 1 | USERNAME= 2 | session= 3 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: [ "main" ] 7 | pull_request: 8 | branches: [ "main" ] 9 | 10 | permissions: 11 | contents: write 12 | 13 | jobs: 14 | 15 | build: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - name: Set up Go 21 | uses: actions/setup-go@v3 22 | with: 23 | go-version: 1.19 24 | 25 | - name: Build 26 | run: make 27 | 28 | - name: Test 29 | run: make test 30 | -------------------------------------------------------------------------------- /.github/workflows/readme.yml: -------------------------------------------------------------------------------- 1 | name: Update README.md 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: "*/15 * * * *" 7 | push: 8 | branches: [ "main" ] 9 | 10 | permissions: 11 | contents: write 12 | 13 | jobs: 14 | 15 | build: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - name: Set up Go 21 | uses: actions/setup-go@v3 22 | with: 23 | go-version: 1.19 24 | 25 | - name: Build 26 | run: make 27 | 28 | - name: Test 29 | run: make test 30 | 31 | - name: Run 32 | run: ./aoc 33 | env: 34 | SESSION: ${{ secrets.SESSION }} 35 | 36 | - name: Commit README 37 | run: | 38 | git config --global user.name 'Enrico Candino' 39 | git config --global user.email 'enrico.candino@gmail.com' 40 | if [[ `git status --porcelain` ]]; then 41 | git commit -am "Automated commit" 42 | git push 43 | fi 44 | 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .vscode 3 | /aoc 4 | -------------------------------------------------------------------------------- /2021/day01/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2021/day01/enrichman/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func PartOne(input []string) string { 10 | measurements, err := utils.Convert(input, utils.StringSliceToIntSliceConverter) 11 | utils.CheckErr(err) 12 | 13 | count := 0 14 | 15 | // start from the second 16 | for i := 1; i < len(measurements); i++ { 17 | prev := measurements[i-1] 18 | curr := measurements[i] 19 | if prev < curr { 20 | count++ 21 | } 22 | } 23 | 24 | return strconv.Itoa(count) 25 | } 26 | 27 | func PartTwo(input []string) string { 28 | measurements, err := utils.Convert(input, utils.StringSliceToIntSliceConverter) 29 | utils.CheckErr(err) 30 | 31 | count := 0 32 | 33 | // start from the second window 34 | for i := 3; i < len(measurements); i++ { 35 | prevWindow := measurements[i-3] + measurements[i-2] + measurements[i-1] 36 | currWindow := measurements[i-2] + measurements[i-1] + measurements[i] 37 | if prevWindow < currWindow { 38 | count++ 39 | } 40 | } 41 | 42 | return strconv.Itoa(count) 43 | } 44 | -------------------------------------------------------------------------------- /2021/day01/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 199 11 | 200 12 | 208 13 | 210 14 | 200 15 | 207 16 | 240 17 | 269 18 | 260 19 | 263 20 | ` 21 | 22 | func TestPartOne(t *testing.T) { 23 | tt := []struct { 24 | name string 25 | input string 26 | expected string 27 | }{ 28 | { 29 | name: "example", 30 | input: sample, 31 | expected: "7", 32 | }, 33 | } 34 | 35 | for _, tc := range tt { 36 | t.Run(tc.name, func(t *testing.T) { 37 | input := utils.SplitInput(tc.input) 38 | if got := PartOne(input); got != tc.expected { 39 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 40 | } 41 | }) 42 | } 43 | } 44 | 45 | func TestPartTwo(t *testing.T) { 46 | tt := []struct { 47 | name string 48 | input string 49 | expected string 50 | }{ 51 | { 52 | name: "example", 53 | input: sample, 54 | expected: "5", 55 | }, 56 | } 57 | 58 | for _, tc := range tt { 59 | t.Run(tc.name, func(t *testing.T) { 60 | input := utils.SplitInput(tc.input) 61 | if got := PartTwo(input); got != tc.expected { 62 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 63 | } 64 | }) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /2021/day02/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2021/day02/enrichman/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | 7 | "github.com/golangroma/aoc/utils" 8 | ) 9 | 10 | type Command struct { 11 | Direction Direction 12 | Units int 13 | } 14 | 15 | type Direction string 16 | 17 | const ( 18 | Up Direction = "up" 19 | Down Direction = "down" 20 | Forward Direction = "forward" 21 | ) 22 | 23 | type Submarine1 struct { 24 | Horizontal int 25 | Depth int 26 | } 27 | 28 | func (s *Submarine1) Move(cmd Command) { 29 | switch cmd.Direction { 30 | case Up: 31 | s.Depth -= cmd.Units 32 | case Down: 33 | s.Depth += cmd.Units 34 | case Forward: 35 | s.Horizontal += cmd.Units 36 | } 37 | } 38 | 39 | func PartOne(input []string) string { 40 | commands, err := utils.Convert(input, CommandDonverter) 41 | utils.CheckErr(err) 42 | 43 | submarine := &Submarine1{} 44 | for _, cmd := range commands { 45 | submarine.Move(cmd) 46 | } 47 | 48 | return strconv.Itoa(submarine.Horizontal * submarine.Depth) 49 | } 50 | 51 | func CommandDonverter(lines []string) ([]Command, error) { 52 | commands := []Command{} 53 | 54 | for _, line := range lines { 55 | arr := strings.Split(line, " ") 56 | units, err := strconv.Atoi(arr[1]) 57 | if err != nil { 58 | return nil, err 59 | } 60 | 61 | commands = append(commands, Command{ 62 | Direction: Direction(arr[0]), 63 | Units: units, 64 | }) 65 | } 66 | 67 | return commands, nil 68 | } 69 | 70 | type Submarine2 struct { 71 | Horizontal int 72 | Depth int 73 | Aim int 74 | } 75 | 76 | func (s *Submarine2) Move(cmd Command) { 77 | switch cmd.Direction { 78 | case Up: 79 | s.Aim -= cmd.Units 80 | case Down: 81 | s.Aim += cmd.Units 82 | case Forward: 83 | s.Horizontal += cmd.Units 84 | s.Depth += s.Aim * cmd.Units 85 | } 86 | } 87 | 88 | func PartTwo(input []string) string { 89 | commands, err := utils.Convert(input, CommandDonverter) 90 | utils.CheckErr(err) 91 | 92 | submarine := &Submarine2{} 93 | for _, cmd := range commands { 94 | submarine.Move(cmd) 95 | } 96 | 97 | return strconv.Itoa(submarine.Horizontal * submarine.Depth) 98 | } 99 | -------------------------------------------------------------------------------- /2021/day02/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | forward 5 11 | down 5 12 | forward 8 13 | up 3 14 | down 8 15 | forward 2` 16 | 17 | func TestPartOne(t *testing.T) { 18 | tt := []struct { 19 | name string 20 | input string 21 | expected string 22 | }{ 23 | { 24 | name: "example", 25 | input: sample, 26 | expected: "150", 27 | }, 28 | } 29 | 30 | for _, tc := range tt { 31 | t.Run(tc.name, func(t *testing.T) { 32 | input := utils.SplitInput(tc.input) 33 | if got := PartOne(input); got != tc.expected { 34 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func TestPartTwo(t *testing.T) { 41 | tt := []struct { 42 | name string 43 | input string 44 | expected string 45 | }{ 46 | { 47 | name: "example", 48 | input: sample, 49 | expected: "900", 50 | }, 51 | } 52 | 53 | for _, tc := range tt { 54 | t.Run(tc.name, func(t *testing.T) { 55 | input := utils.SplitInput(tc.input) 56 | if got := PartTwo(input); got != tc.expected { 57 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /2021/day03/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2021/day03/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 00100 11 | 11110 12 | 10110 13 | 10111 14 | 10101 15 | 01111 16 | 00111 17 | 11100 18 | 10000 19 | 11001 20 | 00010 21 | 01010` 22 | 23 | func TestPartOne(t *testing.T) { 24 | tt := []struct { 25 | name string 26 | input string 27 | expected string 28 | }{ 29 | { 30 | name: "example", 31 | input: sample, 32 | expected: "198", 33 | }, 34 | } 35 | 36 | for _, tc := range tt { 37 | t.Run(tc.name, func(t *testing.T) { 38 | input := utils.SplitInput(tc.input) 39 | if got := PartOne(input); got != tc.expected { 40 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 41 | } 42 | }) 43 | } 44 | } 45 | 46 | func TestPartTwo(t *testing.T) { 47 | tt := []struct { 48 | name string 49 | input string 50 | expected string 51 | }{ 52 | { 53 | name: "example", 54 | input: sample, 55 | expected: "230", 56 | }, 57 | } 58 | 59 | for _, tc := range tt { 60 | t.Run(tc.name, func(t *testing.T) { 61 | input := utils.SplitInput(tc.input) 62 | if got := PartTwo(input); got != tc.expected { 63 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 64 | } 65 | }) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /2022/day01/8lall0/advent.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "sort" 8 | "strconv" 9 | ) 10 | 11 | type Elf struct { 12 | Num int 13 | Calories int 14 | } 15 | 16 | func (e Elf) String() string { 17 | return fmt.Sprintf("Elf n° %d with %d calories", e.Num, e.Calories) 18 | } 19 | 20 | func part1(r io.Reader) (Elf, error) { 21 | var curElf Elf 22 | 23 | scanner := bufio.NewScanner(r) 24 | nElf := 1 25 | sum := 0 26 | 27 | for scanner.Scan() { 28 | txt := scanner.Text() 29 | 30 | if txt == "" { 31 | if curElf.Calories < sum { 32 | curElf.Num = nElf 33 | curElf.Calories = sum 34 | } 35 | nElf++ 36 | sum = 0 37 | } else { 38 | val, err := strconv.Atoi(txt) 39 | if err != nil { 40 | return Elf{}, err 41 | } 42 | 43 | sum += val 44 | } 45 | } 46 | 47 | if curElf.Calories < sum { 48 | curElf.Num = nElf 49 | curElf.Calories = sum 50 | } 51 | 52 | return curElf, nil 53 | } 54 | 55 | func part2(r io.Reader) ([]Elf, error) { 56 | elves := make([]Elf, 0) 57 | 58 | scanner := bufio.NewScanner(r) 59 | nElf := 1 60 | sum := 0 61 | 62 | for scanner.Scan() { 63 | txt := scanner.Text() 64 | 65 | if txt == "" { 66 | elves = append(elves, Elf{Num: nElf, Calories: sum}) 67 | nElf++ 68 | sum = 0 69 | } else { 70 | val, err := strconv.Atoi(txt) 71 | if err != nil { 72 | return nil, err 73 | } 74 | 75 | sum += val 76 | } 77 | } 78 | 79 | elves = append(elves, Elf{Num: nElf, Calories: sum}) 80 | 81 | sort.Slice(elves, func(i, j int) bool { 82 | return elves[i].Calories > elves[j].Calories 83 | }) 84 | 85 | return elves[0:3], nil 86 | } 87 | -------------------------------------------------------------------------------- /2022/day01/8lall0/advent_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "testing" 7 | ) 8 | 9 | var input = `1000 10 | 2000 11 | 3000 12 | 13 | 4000 14 | 15 | 5000 16 | 6000 17 | 18 | 7000 19 | 8000 20 | 9000 21 | 22 | 10000` 23 | 24 | func Test_part1(t *testing.T) { 25 | buf := bytes.NewReader([]byte(input)) 26 | 27 | tests := []struct { 28 | name string 29 | input io.Reader 30 | expected Elf 31 | }{ 32 | { 33 | name: "test input", 34 | input: buf, 35 | expected: Elf{ 36 | Num: 4, 37 | Calories: 24000, 38 | }, 39 | }, 40 | } 41 | 42 | for _, tt := range tests { 43 | t.Run(tt.name, func(t *testing.T) { 44 | n, err := part1(tt.input) 45 | if err != nil { 46 | t.Errorf("Unexpected error. Got: %v", err) 47 | } 48 | if n != tt.expected { 49 | t.Errorf("Expected: %v, got: %v", tt.expected, n) 50 | } 51 | }) 52 | } 53 | } 54 | 55 | func Test_part2(t *testing.T) { 56 | buf := bytes.NewReader([]byte(input)) 57 | 58 | tests := []struct { 59 | name string 60 | input io.Reader 61 | expected []Elf 62 | }{ 63 | { 64 | name: "test input", 65 | input: buf, 66 | expected: []Elf{ 67 | { 68 | Num: 4, 69 | Calories: 24000, 70 | }, 71 | { 72 | Num: 3, 73 | Calories: 11000, 74 | }, 75 | { 76 | Num: 5, 77 | Calories: 10000, 78 | }, 79 | }, 80 | }, 81 | } 82 | for _, tt := range tests { 83 | t.Run(tt.name, func(t *testing.T) { 84 | got, err := part2(tt.input) 85 | if err != nil { 86 | t.Errorf("Unexpected error. Got: %v", err) 87 | } 88 | 89 | if len(got) != len(tt.expected) { 90 | t.Errorf("Number of elements, expected:%d, got: %d", len(tt.expected), len(got)) 91 | } 92 | 93 | for i := 0; i < len(got); i++ { 94 | if got[i] != tt.expected[i] { 95 | t.Errorf("Elements at %d position, expected:%v, got: %v", i, tt.expected[i], got[i]) 96 | } 97 | } 98 | }) 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /2022/day01/8lall0/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | f, err := os.Open("input.txt") 11 | if err != nil { 12 | fmt.Println(err) 13 | os.Exit(1) 14 | } 15 | 16 | fmt.Println("Part 1") 17 | elf, err := part1(f) 18 | if err != nil { 19 | fmt.Println(err) 20 | os.Exit(1) 21 | } 22 | 23 | _, err = f.Seek(0, io.SeekStart) 24 | if err != nil { 25 | fmt.Println(err) 26 | os.Exit(1) 27 | } 28 | elves, err := part2(f) 29 | if err != nil { 30 | fmt.Println(err) 31 | os.Exit(1) 32 | } 33 | 34 | fmt.Println(elf) 35 | fmt.Println("Part 2") 36 | sum := 0 37 | for _, e := range elves { 38 | sum += e.Calories 39 | fmt.Println(e) 40 | } 41 | fmt.Printf("Total calories: %d\n", sum) 42 | } 43 | -------------------------------------------------------------------------------- /2022/day01/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day01/Al-Pragliola/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "sort" 5 | "strconv" 6 | ) 7 | 8 | func PartOne(input []string) string { 9 | maxCal := 0 10 | cal := 0 11 | 12 | for _, v := range input { 13 | if v == "" { 14 | if cal > maxCal { 15 | maxCal = cal 16 | } 17 | 18 | cal = 0 19 | continue 20 | } 21 | 22 | currentCal, err := strconv.Atoi(v) 23 | if err != nil { 24 | panic(err) 25 | } 26 | 27 | cal += currentCal 28 | } 29 | 30 | return strconv.Itoa(maxCal) 31 | } 32 | 33 | func PartTwo(input []string) string { 34 | calories := make([]int, 0) 35 | cal := 0 36 | 37 | for _, v := range input { 38 | if v == "" { 39 | calories = append(calories, cal) 40 | 41 | cal = 0 42 | continue 43 | } 44 | 45 | currentCal, err := strconv.Atoi(v) 46 | if err != nil { 47 | panic(err) 48 | } 49 | 50 | cal += currentCal 51 | } 52 | 53 | sort.Ints(calories) 54 | 55 | return strconv.Itoa(calories[len(calories)-1] + calories[len(calories)-2] + calories[len(calories)-3]) 56 | } 57 | -------------------------------------------------------------------------------- /2022/day01/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestPartOne(t *testing.T) { 8 | tt := []struct { 9 | name string 10 | input []string 11 | expected string 12 | }{ 13 | { 14 | name: "mini input", 15 | input: []string{ 16 | "1000", 17 | "2000", 18 | "3000", 19 | "", 20 | "4000", 21 | "", 22 | "5000", 23 | "6000", 24 | "", 25 | "7000", 26 | "8000", 27 | "9000", 28 | "", 29 | "10000", 30 | "", 31 | }, 32 | expected: "24000", 33 | }, 34 | } 35 | 36 | for _, tc := range tt { 37 | t.Run(tc.name, func(t *testing.T) { 38 | if got := PartOne(tc.input); got != tc.expected { 39 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 40 | } 41 | }) 42 | } 43 | } 44 | 45 | func TestPartTwo(t *testing.T) { 46 | tt := []struct { 47 | name string 48 | input []string 49 | expected string 50 | }{ 51 | { 52 | name: "mini input", 53 | input: []string{ 54 | "1000", 55 | "2000", 56 | "3000", 57 | "", 58 | "4000", 59 | "", 60 | "5000", 61 | "6000", 62 | "", 63 | "7000", 64 | "8000", 65 | "9000", 66 | "", 67 | "10000", 68 | "", 69 | }, 70 | expected: "45000", 71 | }, 72 | } 73 | 74 | for _, tc := range tt { 75 | t.Run(tc.name, func(t *testing.T) { 76 | if got := PartTwo(tc.input); got != tc.expected { 77 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 78 | } 79 | }) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /2022/day01/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day01/alessio-perugini/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | func PartOne(input []string) string { 9 | mostCalories, tmp := 0, 0 10 | 11 | for _, calorie := range input { 12 | if calorie == "" { 13 | if tmp > mostCalories { 14 | mostCalories = tmp 15 | } 16 | tmp = 0 17 | continue 18 | } 19 | v, _ := strconv.Atoi(calorie) 20 | tmp += v 21 | } 22 | 23 | if tmp > 0 && tmp > mostCalories { 24 | mostCalories = tmp 25 | } 26 | 27 | return fmt.Sprintf("%d", mostCalories) 28 | } 29 | 30 | func PartTwo(input []string) string { 31 | sumTopCalories, tmp := 0, 0 32 | top3calories := make([]int, 3) 33 | 34 | for _, calorie := range input { 35 | if calorie == "" { 36 | top3calories = insertOrderedSlice(top3calories, tmp) 37 | tmp = 0 38 | continue 39 | } 40 | v, _ := strconv.Atoi(calorie) 41 | tmp += v 42 | } 43 | top3calories = insertOrderedSlice(top3calories, tmp) 44 | 45 | for _, v := range top3calories { 46 | sumTopCalories += v 47 | } 48 | 49 | return fmt.Sprintf("%d", sumTopCalories) 50 | } 51 | 52 | func insertOrderedSlice(calories []int, calorie int) []int { 53 | for i := 0; i < len(calories); i++ { 54 | if calorie <= calories[i] { 55 | continue 56 | } 57 | 58 | copy(calories[i+1:], calories[i:]) 59 | calories[i] = calorie 60 | break 61 | } 62 | 63 | return calories[:3] 64 | } 65 | -------------------------------------------------------------------------------- /2022/day01/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `1000 10 | 2000 11 | 12 | 4000 13 | 6000 14 | 15 | 2000 16 | 1000` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "10000", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := utils.SplitInput(tc.input) 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "16000", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := utils.SplitInput(tc.input) 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day01/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day01/enrichman/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | "strconv" 7 | "strings" 8 | 9 | "github.com/golangroma/aoc/utils" 10 | ) 11 | 12 | type Elf struct { 13 | ID int 14 | Foods []int 15 | } 16 | 17 | func NewElf(id int) *Elf { 18 | return &Elf{ 19 | ID: id, 20 | Foods: []int{}, 21 | } 22 | } 23 | 24 | func (e *Elf) Calories() int { 25 | calories := 0 26 | for i := range e.Foods { 27 | calories += e.Foods[i] 28 | } 29 | return calories 30 | } 31 | 32 | func (e *Elf) AddFood(calories int) { 33 | e.Foods = append(e.Foods, calories) 34 | } 35 | 36 | func (e *Elf) String() string { 37 | foods := []string{} 38 | for _, food := range e.Foods { 39 | foods = append(foods, strconv.Itoa(food)) 40 | } 41 | return fmt.Sprintf("Elf{ID:%d, Foods:[%s]}", e.ID, strings.Join(foods, ",")) 42 | } 43 | 44 | func PartOne(input []string) string { 45 | elves := elvesFromInput(input) 46 | 47 | sort.Slice(elves, func(i, j int) bool { 48 | return elves[i].Calories() > elves[j].Calories() 49 | }) 50 | 51 | return strconv.Itoa(elves[0].Calories()) 52 | } 53 | 54 | func PartTwo(input []string) string { 55 | elves := elvesFromInput(input) 56 | 57 | sort.Slice(elves, func(i, j int) bool { 58 | return elves[i].Calories() > elves[j].Calories() 59 | }) 60 | 61 | topThreeTotal := elves[0].Calories() + elves[1].Calories() + elves[2].Calories() 62 | 63 | return strconv.Itoa(topThreeTotal) 64 | } 65 | 66 | func elvesFromInput(input []string) []*Elf { 67 | elves := []*Elf{} 68 | 69 | var elf *Elf 70 | for _, caloriesStr := range input { 71 | if elf == nil { 72 | // the ID of the Elf is derived from the order/length of the slice 73 | elf = NewElf(len(elves) + 1) 74 | } 75 | 76 | if caloriesStr == "" { 77 | elves = append(elves, elf) 78 | elf = nil 79 | continue 80 | } 81 | 82 | calories, err := strconv.Atoi(caloriesStr) 83 | utils.CheckErr(err) 84 | elf.AddFood(calories) 85 | } 86 | elves = append(elves, elf) 87 | 88 | return elves 89 | } 90 | -------------------------------------------------------------------------------- /2022/day01/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `1000 10 | 2000 11 | 3000 12 | 13 | 4000 14 | 15 | 5000 16 | 6000 17 | 18 | 7000 19 | 8000 20 | 9000 21 | 22 | 10000` 23 | 24 | func TestPartOne(t *testing.T) { 25 | tt := []struct { 26 | name string 27 | input string 28 | expected string 29 | }{ 30 | { 31 | name: "example", 32 | input: sample, 33 | expected: "24000", 34 | }, 35 | } 36 | 37 | for _, tc := range tt { 38 | t.Run(tc.name, func(t *testing.T) { 39 | input := utils.SplitInput(tc.input) 40 | if got := PartOne(input); got != tc.expected { 41 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 42 | } 43 | }) 44 | } 45 | } 46 | 47 | func TestPartTwo(t *testing.T) { 48 | tt := []struct { 49 | name string 50 | input string 51 | expected string 52 | }{ 53 | { 54 | name: "example", 55 | input: sample, 56 | expected: "45000", 57 | }, 58 | } 59 | 60 | for _, tc := range tt { 61 | t.Run(tc.name, func(t *testing.T) { 62 | input := utils.SplitInput(tc.input) 63 | if got := PartTwo(input); got != tc.expected { 64 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 65 | } 66 | }) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /2022/day01/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: 69289 16 | Part 2: 205615 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day01/giulianopz/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "strconv" 6 | ) 7 | 8 | var maxCaloriesPerElf uint64 9 | var topThree = []uint64{0, 0, 0} 10 | 11 | func PartOne(input []string) string { 12 | 13 | var caloriesPerElf uint64 14 | 15 | for _, line := range input { 16 | if line == "" { 17 | if caloriesPerElf > maxCaloriesPerElf { 18 | maxCaloriesPerElf = caloriesPerElf 19 | } 20 | caloriesPerElf = 0 21 | continue 22 | } 23 | 24 | caloriesPerElf += toUint(line) 25 | } 26 | return strconv.FormatUint(maxCaloriesPerElf, 10) 27 | } 28 | 29 | func PartTwo(input []string) string { 30 | 31 | var caloriesPerElf uint64 32 | 33 | for _, line := range input { 34 | if line == "" { 35 | for idx, top := range topThree { 36 | if caloriesPerElf > top { 37 | 38 | topThree[idx] = caloriesPerElf 39 | 40 | if idx == 0 { 41 | tmp := topThree[idx+1] 42 | topThree[idx+1] = top 43 | topThree[idx+2] = tmp 44 | } 45 | 46 | if idx == 1 { 47 | topThree[idx+1] = top 48 | } 49 | break 50 | } 51 | } 52 | caloriesPerElf = 0 53 | continue 54 | } 55 | 56 | caloriesPerElf += toUint(line) 57 | } 58 | return strconv.FormatUint(topThree[0]+topThree[1]+topThree[2], 10) 59 | } 60 | 61 | func toUint(s string) uint64 { 62 | parsed, err := strconv.ParseUint(s, 10, 64) 63 | exitFatally(err) 64 | return parsed 65 | } 66 | 67 | func exitFatally(err error) { 68 | if err != nil { 69 | log.Fatal(err) 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /2022/day01/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | var sample []string = []string{"1", "2", "3", "", "2", "", "3", "", "11", ""} 8 | 9 | func TestPartOne(t *testing.T) { 10 | tt := []struct { 11 | name string 12 | input []string 13 | expected string 14 | }{ 15 | { 16 | name: "example", 17 | input: sample, 18 | expected: "11", 19 | }, 20 | } 21 | 22 | for _, tc := range tt { 23 | t.Run(tc.name, func(t *testing.T) { 24 | if got := PartOne(tc.input); got != tc.expected { 25 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 26 | } 27 | }) 28 | } 29 | } 30 | 31 | func TestPartTwo(t *testing.T) { 32 | tt := []struct { 33 | name string 34 | input []string 35 | expected string 36 | }{ 37 | { 38 | name: "example", 39 | input: sample, 40 | expected: "20", 41 | }, 42 | } 43 | 44 | for _, tc := range tt { 45 | t.Run(tc.name, func(t *testing.T) { 46 | if got := PartTwo(tc.input); got != tc.expected { 47 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 48 | } 49 | }) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /2022/day01/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/1](https://github.com/lucianoq/adventofcode/tree/master/2022/1) 3 | 4 | -------------------------------------------------------------------------------- /2022/day01/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day01/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | "strconv" 7 | ) 8 | 9 | func PartOne(input []string) string { 10 | current_elf_calories := 0 11 | max_calories := 0 12 | for _, item := range input { 13 | if item == "" { 14 | if current_elf_calories > max_calories { 15 | max_calories = current_elf_calories 16 | } 17 | current_elf_calories = 0 18 | } else { 19 | value, _ := strconv.Atoi(item) 20 | current_elf_calories += value 21 | } 22 | } 23 | if current_elf_calories > max_calories { 24 | max_calories = current_elf_calories 25 | } 26 | return fmt.Sprintf("%d", max_calories) 27 | } 28 | 29 | func PartTwo(input []string) string { 30 | current_elf_calories := 0 31 | elves := make([]int, 0) 32 | for _, item := range input { 33 | if item == "" { 34 | elves = append(elves, current_elf_calories) 35 | current_elf_calories = 0 36 | } else { 37 | value, _ := strconv.Atoi(item) 38 | current_elf_calories += value 39 | } 40 | } 41 | 42 | elves = append(elves, current_elf_calories) 43 | sort.Ints(elves) 44 | 45 | tot := 0 46 | for i := len(elves) - 1 - 2; i < len(elves); i++ { 47 | tot += elves[i] 48 | } 49 | 50 | return fmt.Sprintf("%d", tot) 51 | } 52 | -------------------------------------------------------------------------------- /2022/day01/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day01/misterdelle/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func PartOne(input []string) string { 8 | var elvesMap = make(map[int]int) 9 | var elfId int = 1 10 | 11 | for _, v := range input { 12 | if v != "" { 13 | calories, _ := strconv.Atoi(v) 14 | elvesMap[elfId] = elvesMap[elfId] + calories 15 | } else { 16 | // Blank line means new elf 17 | elfId++ 18 | } 19 | } 20 | 21 | _, maxCalories := getMaxCaloriesXElf(elvesMap) 22 | 23 | return strconv.Itoa(maxCalories) 24 | } 25 | 26 | func PartTwo(input []string) string { 27 | var elvesMap = make(map[int]int) 28 | var elfId int = 1 29 | 30 | for _, v := range input { 31 | if v != "" { 32 | calories, _ := strconv.Atoi(v) 33 | elvesMap[elfId] = elvesMap[elfId] + calories 34 | } else { 35 | // Blank line means new elf 36 | elfId++ 37 | } 38 | } 39 | 40 | elfCarryingMaxCalories1, maxCalories1 := getMaxCaloriesXElf(elvesMap) 41 | elvesMap[elfCarryingMaxCalories1] = 0 42 | elfCarryingMaxCalories2, maxCalories2 := getMaxCaloriesXElf(elvesMap) 43 | elvesMap[elfCarryingMaxCalories2] = 0 44 | elfCarryingMaxCalories3, maxCalories3 := getMaxCaloriesXElf(elvesMap) 45 | elvesMap[elfCarryingMaxCalories3] = 0 46 | 47 | maxCalories := maxCalories1 + maxCalories2 + maxCalories3 48 | 49 | return strconv.Itoa(maxCalories) 50 | } 51 | 52 | func getMaxCaloriesXElf(elvesMap map[int]int) (int, int) { 53 | elfCarryingMaxCalories := 0 54 | maxCalories := 0 55 | 56 | for k, v := range elvesMap { 57 | if v > maxCalories { 58 | maxCalories = v 59 | elfCarryingMaxCalories = k 60 | } 61 | } 62 | 63 | return elfCarryingMaxCalories, maxCalories 64 | } 65 | -------------------------------------------------------------------------------- /2022/day01/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `1000 10 | 2000 11 | 3000 12 | 13 | 4000 14 | 15 | 5000 16 | 6000 17 | 18 | 7000 19 | 8000 20 | 9000 21 | 22 | 10000 23 | ` 24 | 25 | func TestPartOne(t *testing.T) { 26 | tt := []struct { 27 | name string 28 | input string 29 | expected string 30 | }{ 31 | { 32 | name: "example", 33 | input: sample, 34 | expected: "24000", 35 | }, 36 | } 37 | 38 | for _, tc := range tt { 39 | t.Run(tc.name, func(t *testing.T) { 40 | input := utils.SplitInput(tc.input) 41 | if got := PartOne(input); got != tc.expected { 42 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 43 | } 44 | }) 45 | } 46 | } 47 | 48 | func TestPartTwo(t *testing.T) { 49 | tt := []struct { 50 | name string 51 | input string 52 | expected string 53 | }{ 54 | { 55 | name: "example", 56 | input: sample, 57 | expected: "45000", 58 | }, 59 | } 60 | 61 | for _, tc := range tt { 62 | t.Run(tc.name, func(t *testing.T) { 63 | input := utils.SplitInput(tc.input) 64 | if got := PartTwo(input); got != tc.expected { 65 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 66 | } 67 | }) 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /2022/day01/omissis/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/omissis/aoc 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221201151433-77068aa80cfb 6 | -------------------------------------------------------------------------------- /2022/day01/omissis/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221201151433-77068aa80cfb h1:ckYsy8nIsZCF5l0wE8UIVksuvNx6rs/9uIoLyGShERA= 2 | github.com/golangroma/aoc v0.0.0-20221201151433-77068aa80cfb/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day01/omissis/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day01/omissis/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "strconv" 6 | ) 7 | 8 | func PartOne(input []string) string { 9 | max := 0 10 | curr := 0 11 | 12 | for _, line := range input { 13 | if line == "" { 14 | if curr > max { 15 | max = curr 16 | } 17 | curr = 0 18 | 19 | continue 20 | } 21 | 22 | cals, err := strconv.Atoi(line) 23 | if err != nil { 24 | log.Println(err) 25 | 26 | return "" 27 | } 28 | 29 | curr += cals 30 | } 31 | 32 | return strconv.Itoa(max) 33 | } 34 | 35 | func PartTwo(input []string) string { 36 | elves := make([]int, 0) 37 | curr := 0 38 | 39 | for i, line := range input { 40 | if i != 0 && line == "" { 41 | elves = append(elves, curr) 42 | curr = 0 43 | 44 | continue 45 | } 46 | 47 | cals, err := strconv.Atoi(line) 48 | if err != nil { 49 | log.Println(err) 50 | 51 | return "" 52 | } 53 | 54 | curr += cals 55 | } 56 | 57 | res := 0 58 | for i := len(elves) - 3; i < len(elves); i++ { 59 | res += elves[i] 60 | } 61 | 62 | return strconv.Itoa(res) 63 | } 64 | -------------------------------------------------------------------------------- /2022/day01/omissis/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `` 10 | 11 | func TestPartOne(t *testing.T) { 12 | tt := []struct { 13 | name string 14 | input string 15 | expected string 16 | }{ 17 | { 18 | name: "example", 19 | input: sample, 20 | expected: "", 21 | }, 22 | } 23 | 24 | for _, tc := range tt { 25 | t.Run(tc.name, func(t *testing.T) { 26 | input := utils.SplitInput(tc.input) 27 | if got := PartOne(input); got != tc.expected { 28 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 29 | } 30 | }) 31 | } 32 | } 33 | 34 | func TestPartTwo(t *testing.T) { 35 | tt := []struct { 36 | name string 37 | input string 38 | expected string 39 | }{ 40 | { 41 | name: "example", 42 | input: sample, 43 | expected: "", 44 | }, 45 | } 46 | 47 | for _, tc := range tt { 48 | t.Run(tc.name, func(t *testing.T) { 49 | input := utils.SplitInput(tc.input) 50 | if got := PartTwo(input); got != tc.expected { 51 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 52 | } 53 | }) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /2022/day02/8lall0/advent.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | ) 8 | 9 | const ( 10 | WIN int = iota 11 | TIE 12 | LOSE 13 | ) 14 | 15 | const ( 16 | ROCK int = iota 17 | PAPER 18 | SCISSOR 19 | ) 20 | 21 | var opponent = map[string]int{ 22 | "A": ROCK, // Sasso 23 | "B": PAPER, // Carta 24 | "C": SCISSOR, // Forbici 25 | } 26 | 27 | var score = map[int]int{ 28 | WIN: 6, 29 | TIE: 3, 30 | LOSE: 0, 31 | } 32 | 33 | var typeScore = map[int]int{ 34 | ROCK: 1, 35 | PAPER: 2, 36 | SCISSOR: 3, 37 | } 38 | 39 | var results = [][]int{ 40 | // ROCK PAPER SCISSOR 41 | {TIE, LOSE, WIN}, // ROCK 42 | {WIN, TIE, LOSE}, // PAPER 43 | {LOSE, WIN, TIE}, // SCISSOR 44 | } 45 | 46 | func part1(r io.Reader) (int, error) { 47 | mapResults := map[string]int{ 48 | "X": ROCK, // Sasso 49 | "Y": PAPER, // Carta 50 | "Z": SCISSOR, // Forbici 51 | } 52 | 53 | scanner := bufio.NewScanner(r) 54 | 55 | sum := 0 56 | 57 | for scanner.Scan() { 58 | txt := scanner.Text() 59 | var a, b string 60 | _, err := fmt.Sscanf(txt, "%s %s", &a, &b) 61 | if err != nil { 62 | return 0, err 63 | } 64 | 65 | moveA := opponent[a] 66 | moveB := mapResults[b] 67 | 68 | result := results[moveB][moveA] 69 | 70 | sum += score[result] + typeScore[moveB] 71 | } 72 | 73 | return sum, nil 74 | } 75 | 76 | func part2(r io.Reader) (int, error) { 77 | mapResults := map[string]int{ 78 | "X": LOSE, // Sasso 79 | "Y": TIE, // Carta 80 | "Z": WIN, // Forbici 81 | } 82 | 83 | scanner := bufio.NewScanner(r) 84 | 85 | sum := 0 86 | 87 | for scanner.Scan() { 88 | txt := scanner.Text() 89 | var a, b string 90 | _, err := fmt.Sscanf(txt, "%s %s", &a, &b) 91 | if err != nil { 92 | return 0, err 93 | } 94 | 95 | moveA := opponent[a] 96 | result := mapResults[b] 97 | 98 | ndx := 0 99 | for i, row := range results { 100 | if row[moveA] == result { 101 | ndx = i 102 | break 103 | } 104 | } 105 | 106 | sum += score[result] + typeScore[ndx] 107 | } 108 | 109 | return sum, nil 110 | } 111 | -------------------------------------------------------------------------------- /2022/day02/8lall0/advent_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "io" 6 | "testing" 7 | ) 8 | 9 | var input = `A Y 10 | B X 11 | C Z` 12 | 13 | func Test_part1(t *testing.T) { 14 | buf := bytes.NewBuffer([]byte(input)) 15 | 16 | type args struct { 17 | r io.Reader 18 | } 19 | tests := []struct { 20 | name string 21 | args args 22 | want int 23 | wantErr bool 24 | }{ 25 | { 26 | name: "Test part 1", 27 | args: args{r: buf}, 28 | want: 15, 29 | wantErr: false, 30 | }, 31 | } 32 | for _, tt := range tests { 33 | t.Run(tt.name, func(t *testing.T) { 34 | got, err := part1(tt.args.r) 35 | if (err != nil) != tt.wantErr { 36 | t.Errorf("part1() error = %v, wantErr %v", err, tt.wantErr) 37 | return 38 | } 39 | if got != tt.want { 40 | t.Errorf("part1() got = %v, want %v", got, tt.want) 41 | } 42 | }) 43 | } 44 | } 45 | 46 | func Test_part2(t *testing.T) { 47 | buf := bytes.NewBuffer([]byte(input)) 48 | 49 | type args struct { 50 | r io.Reader 51 | } 52 | tests := []struct { 53 | name string 54 | args args 55 | want int 56 | wantErr bool 57 | }{ 58 | { 59 | name: "Test part 2", 60 | args: args{r: buf}, 61 | want: 12, 62 | wantErr: false, 63 | }, 64 | } 65 | for _, tt := range tests { 66 | t.Run(tt.name, func(t *testing.T) { 67 | got, err := part2(tt.args.r) 68 | if (err != nil) != tt.wantErr { 69 | t.Errorf("part2() error = %v, wantErr %v", err, tt.wantErr) 70 | return 71 | } 72 | if got != tt.want { 73 | t.Errorf("part2() got = %v, want %v", got, tt.want) 74 | } 75 | }) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /2022/day02/8lall0/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | f, err := os.Open("input.txt") 11 | if err != nil { 12 | fmt.Println(err) 13 | os.Exit(1) 14 | } 15 | 16 | fmt.Println("Part 1") 17 | n, err := part1(f) 18 | if err != nil { 19 | fmt.Println(err) 20 | os.Exit(1) 21 | } 22 | 23 | fmt.Printf("Score: %d\n", n) 24 | 25 | fmt.Println("Part 2") 26 | _, err = f.Seek(0, io.SeekStart) 27 | if err != nil { 28 | fmt.Println(err) 29 | os.Exit(1) 30 | } 31 | 32 | n, err = part2(f) 33 | if err != nil { 34 | fmt.Println(err) 35 | os.Exit(1) 36 | } 37 | fmt.Printf("Score: %d", n) 38 | } 39 | -------------------------------------------------------------------------------- /2022/day02/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day02/Al-Pragliola/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func PartOne(input []string) string { 9 | score := 0 10 | 11 | for _, line := range input { 12 | moves := strings.Split(line, " ") 13 | moveOne := moves[0] 14 | moveTwo := moves[1] 15 | 16 | score += basicStrat(moveOne, moveTwo) 17 | } 18 | 19 | return strconv.Itoa(score) 20 | } 21 | 22 | func PartTwo(input []string) string { 23 | score := 0 24 | 25 | for _, line := range input { 26 | moves := strings.Split(line, " ") 27 | moveOne := moves[0] 28 | result := moves[1] 29 | 30 | score += complexStrat(moveOne, result) 31 | } 32 | 33 | return strconv.Itoa(score) 34 | } 35 | 36 | func complexStrat(moveOne, result string) int { 37 | resultScores := map[string]int{ 38 | "X": 0, 39 | "Y": 3, 40 | "Z": 6, 41 | } 42 | 43 | mapIndex := map[string]int{ 44 | "X": 0, 45 | "Y": 1, 46 | "Z": 2, 47 | "A": 0, 48 | "B": 1, 49 | "C": 2, 50 | } 51 | 52 | outMat := [][]int{ 53 | {3, 1, 2}, 54 | {1, 2, 3}, 55 | {2, 3, 1}, 56 | } 57 | 58 | return outMat[mapIndex[moveOne]][mapIndex[result]] + resultScores[result] 59 | 60 | } 61 | 62 | func basicStrat(moveOne, moveTwo string) int { 63 | moveScores := map[string]int{ 64 | "X": 1, 65 | "Y": 2, 66 | "Z": 3, 67 | } 68 | 69 | return moveScores[moveTwo] + outcome(moveOne, moveTwo) 70 | 71 | } 72 | 73 | func outcome(moveOne, moveTwo string) int { 74 | win := 6 75 | lose := 0 76 | tie := 3 77 | 78 | mapIndex := map[string]int{ 79 | "X": 0, 80 | "Y": 1, 81 | "Z": 2, 82 | "A": 0, 83 | "B": 1, 84 | "C": 2, 85 | } 86 | 87 | outMat := [][]int{ 88 | {0, 1, -1}, 89 | {-1, 0, 1}, 90 | {1, -1, 0}, 91 | } 92 | 93 | if outMat[mapIndex[moveOne]][mapIndex[moveTwo]] == 0 { 94 | return tie 95 | } 96 | 97 | if outMat[mapIndex[moveOne]][mapIndex[moveTwo]] == 1 { 98 | return win 99 | } 100 | 101 | return lose 102 | } 103 | -------------------------------------------------------------------------------- /2022/day02/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | A Y 11 | B X 12 | C Z` 13 | 14 | func TestPartOne(t *testing.T) { 15 | tt := []struct { 16 | name string 17 | input string 18 | expected string 19 | }{ 20 | { 21 | name: "example", 22 | input: sample, 23 | expected: "15", 24 | }, 25 | } 26 | 27 | for _, tc := range tt { 28 | t.Run(tc.name, func(t *testing.T) { 29 | input := utils.SplitInput(tc.input) 30 | if got := PartOne(input); got != tc.expected { 31 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 32 | } 33 | }) 34 | } 35 | } 36 | 37 | func TestPartTwo(t *testing.T) { 38 | tt := []struct { 39 | name string 40 | input string 41 | expected string 42 | }{ 43 | { 44 | name: "example", 45 | input: sample, 46 | expected: "12", 47 | }, 48 | } 49 | 50 | for _, tc := range tt { 51 | t.Run(tc.name, func(t *testing.T) { 52 | input := utils.SplitInput(tc.input) 53 | if got := PartTwo(input); got != tc.expected { 54 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 55 | } 56 | }) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /2022/day02/alessio-perugini/go.mod: -------------------------------------------------------------------------------- 1 | module day2 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221202074944-5aa0c6d26ab0 6 | -------------------------------------------------------------------------------- /2022/day02/alessio-perugini/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221202074944-5aa0c6d26ab0 h1:6ur0l9yJQC1qGG2pHJEFz2JWxQb5dKDw+eUu2MQ3Gjg= 2 | github.com/golangroma/aoc v0.0.0-20221202074944-5aa0c6d26ab0/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day02/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day02/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `A Y 10 | B X 11 | C Z` 12 | 13 | func TestPartOne(t *testing.T) { 14 | tt := []struct { 15 | name string 16 | input string 17 | expected string 18 | }{ 19 | { 20 | name: "example", 21 | input: sample, 22 | expected: "15", 23 | }, 24 | } 25 | 26 | for _, tc := range tt { 27 | t.Run(tc.name, func(t *testing.T) { 28 | input := utils.SplitInput(tc.input) 29 | if got := PartOne(input); got != tc.expected { 30 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 31 | } 32 | }) 33 | } 34 | } 35 | 36 | func TestPartTwo(t *testing.T) { 37 | tt := []struct { 38 | name string 39 | input string 40 | expected string 41 | }{ 42 | { 43 | name: "example", 44 | input: sample, 45 | expected: "12", 46 | }, 47 | } 48 | 49 | for _, tc := range tt { 50 | t.Run(tc.name, func(t *testing.T) { 51 | input := utils.SplitInput(tc.input) 52 | if got := PartTwo(input); got != tc.expected { 53 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 54 | } 55 | }) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /2022/day02/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day02/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | A Y 11 | B X 12 | C Z 13 | ` 14 | 15 | func TestPartOne(t *testing.T) { 16 | tt := []struct { 17 | name string 18 | input string 19 | expected string 20 | }{ 21 | { 22 | name: "example", 23 | input: sample, 24 | expected: "15", 25 | }, 26 | } 27 | 28 | for _, tc := range tt { 29 | t.Run(tc.name, func(t *testing.T) { 30 | input := utils.SplitInput(tc.input) 31 | if got := PartOne(input); got != tc.expected { 32 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 33 | } 34 | }) 35 | } 36 | } 37 | 38 | func TestPartTwo(t *testing.T) { 39 | tt := []struct { 40 | name string 41 | input string 42 | expected string 43 | }{ 44 | { 45 | name: "example", 46 | input: sample, 47 | expected: "12", 48 | }, 49 | } 50 | 51 | for _, tc := range tt { 52 | t.Run(tc.name, func(t *testing.T) { 53 | input := utils.SplitInput(tc.input) 54 | if got := PartTwo(input); got != tc.expected { 55 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 56 | } 57 | }) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /2022/day02/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: 13809 16 | Part 2: 12316 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day02/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | A Y 11 | B X 12 | C Z 13 | ` 14 | 15 | func TestPartOne(t *testing.T) { 16 | tt := []struct { 17 | name string 18 | input string 19 | expected string 20 | }{ 21 | { 22 | name: "example", 23 | input: sample, 24 | expected: "15", 25 | }, 26 | } 27 | 28 | for _, tc := range tt { 29 | t.Run(tc.name, func(t *testing.T) { 30 | input := utils.SplitInput(tc.input) 31 | if got := PartOne(input); got != tc.expected { 32 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 33 | } 34 | }) 35 | } 36 | } 37 | 38 | func TestPartTwo(t *testing.T) { 39 | tt := []struct { 40 | name string 41 | input string 42 | expected string 43 | }{ 44 | { 45 | name: "example", 46 | input: sample, 47 | expected: "12", 48 | }, 49 | } 50 | 51 | for _, tc := range tt { 52 | t.Run(tc.name, func(t *testing.T) { 53 | input := utils.SplitInput(tc.input) 54 | if got := PartTwo(input); got != tc.expected { 55 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 56 | } 57 | }) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /2022/day02/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/2](https://github.com/lucianoq/adventofcode/tree/master/2022/2) 3 | -------------------------------------------------------------------------------- /2022/day02/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day02/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `A Y 10 | B X 11 | C Z` 12 | 13 | func TestPartOne(t *testing.T) { 14 | tt := []struct { 15 | name string 16 | input string 17 | expected string 18 | }{ 19 | { 20 | name: "example", 21 | input: sample, 22 | expected: "15", 23 | }, 24 | } 25 | 26 | for _, tc := range tt { 27 | t.Run(tc.name, func(t *testing.T) { 28 | input := utils.SplitInput(tc.input) 29 | if got := PartOne(input); got != tc.expected { 30 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 31 | } 32 | }) 33 | } 34 | } 35 | 36 | func TestPartTwo(t *testing.T) { 37 | tt := []struct { 38 | name string 39 | input string 40 | expected string 41 | }{ 42 | { 43 | name: "example", 44 | input: sample, 45 | expected: "12", 46 | }, 47 | } 48 | 49 | for _, tc := range tt { 50 | t.Run(tc.name, func(t *testing.T) { 51 | input := utils.SplitInput(tc.input) 52 | if got := PartTwo(input); got != tc.expected { 53 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 54 | } 55 | }) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /2022/day02/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day02/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | A Y 11 | B X 12 | C Z 13 | ` 14 | 15 | func TestPartOne(t *testing.T) { 16 | tt := []struct { 17 | name string 18 | input string 19 | expected string 20 | }{ 21 | { 22 | name: "example", 23 | input: sample, 24 | expected: "15", 25 | }, 26 | } 27 | 28 | for _, tc := range tt { 29 | t.Run(tc.name, func(t *testing.T) { 30 | input := utils.SplitInput(tc.input) 31 | if got := PartOne(input); got != tc.expected { 32 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 33 | } 34 | }) 35 | } 36 | } 37 | 38 | func TestPartTwo(t *testing.T) { 39 | tt := []struct { 40 | name string 41 | input string 42 | expected string 43 | }{ 44 | { 45 | name: "example", 46 | input: sample, 47 | expected: "12", 48 | }, 49 | } 50 | 51 | for _, tc := range tt { 52 | t.Run(tc.name, func(t *testing.T) { 53 | input := utils.SplitInput(tc.input) 54 | if got := PartTwo(input); got != tc.expected { 55 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 56 | } 57 | }) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /2022/day03/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day03/Al-Pragliola/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "unicode" 6 | ) 7 | 8 | func PartOne(input []string) string { 9 | priorities := buildPriorities() 10 | commonTotal := 0 11 | 12 | for _, line := range input { 13 | common := 0 14 | 15 | rucksack := make(map[rune]int) 16 | errors := make(map[rune]int) 17 | 18 | for r := range line[:len(line)/2] { 19 | rucksack[rune(line[r])]++ 20 | } 21 | 22 | for _, s := range line[len(line)/2:] { 23 | if rucksack[s] > 0 && errors[s] == 0 { 24 | errors[s]++ 25 | common += priorities[s] 26 | } 27 | } 28 | 29 | commonTotal += common 30 | } 31 | 32 | return strconv.Itoa(commonTotal) 33 | } 34 | 35 | func PartTwo(input []string) string { 36 | priorities := buildPriorities() 37 | commonTotal := 0 38 | 39 | for i := 0; i < len(input); i += 3 { 40 | common := 0 41 | 42 | rucksackOne := make(map[rune]int) 43 | rucksackTwo := make(map[rune]int) 44 | 45 | for r := range input[i] { 46 | rucksackOne[rune(input[i][r])]++ 47 | } 48 | 49 | for r := range input[i+1] { 50 | if rucksackOne[rune(input[i+1][r])] > 0 { 51 | rucksackTwo[rune(input[i+1][r])]++ 52 | } 53 | } 54 | 55 | for r := range input[i+2] { 56 | if rucksackTwo[rune(input[i+2][r])] > 0 { 57 | common += priorities[rune(input[i+2][r])] 58 | break 59 | } 60 | } 61 | 62 | commonTotal += common 63 | } 64 | 65 | return strconv.Itoa(commonTotal) 66 | } 67 | 68 | func buildPriorities() map[rune]int { 69 | priorities := make(map[rune]int) 70 | 71 | for i, r := range "abcdefghijklmnopqrstuvwxyz" { 72 | priorities[r] = i + 1 73 | priorities[unicode.ToUpper(r)] = i + 1 + 26 74 | } 75 | 76 | return priorities 77 | } 78 | -------------------------------------------------------------------------------- /2022/day03/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | vJrwpWtwJgWrhcsFMMfFFhFp 11 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 12 | PmmdzqPrVvPwwTWBwg 13 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 14 | ttgJtRGJQctTZtZT 15 | CrZsJsPPZsGzwwsLwLmpwMDw` 16 | 17 | func TestPartOne(t *testing.T) { 18 | tt := []struct { 19 | name string 20 | input string 21 | expected string 22 | }{ 23 | { 24 | name: "example", 25 | input: sample, 26 | expected: "157", 27 | }, 28 | } 29 | 30 | for _, tc := range tt { 31 | t.Run(tc.name, func(t *testing.T) { 32 | input := utils.SplitInput(tc.input) 33 | if got := PartOne(input); got != tc.expected { 34 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func TestPartTwo(t *testing.T) { 41 | tt := []struct { 42 | name string 43 | input string 44 | expected string 45 | }{ 46 | { 47 | name: "example", 48 | input: sample, 49 | expected: "70", 50 | }, 51 | } 52 | 53 | for _, tc := range tt { 54 | t.Run(tc.name, func(t *testing.T) { 55 | input := utils.SplitInput(tc.input) 56 | if got := PartTwo(input); got != tc.expected { 57 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /2022/day03/alessio-perugini/go.mod: -------------------------------------------------------------------------------- 1 | module day3 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221203122218-5dbe9a6770c9 6 | -------------------------------------------------------------------------------- /2022/day03/alessio-perugini/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221203122218-5dbe9a6770c9 h1:mJwI2hbks1uNghFxP6Lb4UgT6Gkn3Ky2AqFQ36o5qc4= 2 | github.com/golangroma/aoc v0.0.0-20221203122218-5dbe9a6770c9/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day03/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day03/alessio-perugini/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func PartOne(input []string) string { 6 | sum := 0 7 | 8 | for _, rucksack := range input { 9 | sum += getPriorityOfCommonItems(rucksack) 10 | } 11 | 12 | return fmt.Sprintf("%d", sum) 13 | } 14 | 15 | func getPriorityOfCommonItems(rucksack string) int { 16 | sum := 0 17 | slot1, itemsInCommon := make(map[rune]int), make(map[rune]bool) 18 | 19 | l := len(rucksack) 20 | for i, item := range rucksack { 21 | if i < l/2 { 22 | slot1[item]++ 23 | continue 24 | } 25 | if _, ok := slot1[item]; ok && !itemsInCommon[item] { 26 | itemsInCommon[item] = true 27 | sum += itemPriority(item) 28 | } 29 | } 30 | 31 | return sum 32 | } 33 | 34 | func itemPriority(item rune) int { 35 | lowerCaseOffset, upperCaseOffset := int('a'), int('A') 36 | 37 | if item >= 'a' && item <= 'z' { 38 | return int(item) - lowerCaseOffset + 1 39 | } 40 | if item >= 'A' && item <= 'Z' { 41 | return int(item) - upperCaseOffset + 27 42 | } 43 | 44 | return 0 45 | } 46 | 47 | func PartTwo(input []string) string { 48 | sum := 0 49 | 50 | for i := 0; i < len(input); i += 3 { 51 | sum += commonItemPriorityBetweenThreeElves(input[i : i+3]) 52 | } 53 | 54 | return fmt.Sprintf("%d", sum) 55 | } 56 | 57 | func commonItemPriorityBetweenThreeElves(rucksacks []string) int { 58 | sum := 0 59 | lastRucksack := len(rucksacks) - 1 60 | common := make(map[rune]int) 61 | 62 | for i, rucksack := range rucksacks { 63 | for _, item := range rucksack { 64 | if common[item] != i { 65 | continue 66 | } 67 | common[item]++ 68 | if lastRucksack == i { 69 | sum += itemPriority(item) 70 | } 71 | } 72 | } 73 | 74 | return sum 75 | } 76 | -------------------------------------------------------------------------------- /2022/day03/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `vJrwpWtwJgWrhcsFMMfFFhFp 10 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 11 | PmmdzqPrVvPwwTWBwg 12 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 13 | ttgJtRGJQctTZtZT 14 | CrZsJsPPZsGzwwsLwLmpwMDw` 15 | 16 | func TestPartOne(t *testing.T) { 17 | tt := []struct { 18 | name string 19 | input string 20 | expected string 21 | }{ 22 | { 23 | name: "example", 24 | input: sample, 25 | expected: "157", 26 | }, 27 | } 28 | 29 | for _, tc := range tt { 30 | t.Run(tc.name, func(t *testing.T) { 31 | input := utils.SplitInput(tc.input) 32 | if got := PartOne(input); got != tc.expected { 33 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 34 | } 35 | }) 36 | } 37 | } 38 | 39 | func TestPartTwo(t *testing.T) { 40 | tt := []struct { 41 | name string 42 | input string 43 | expected string 44 | }{ 45 | { 46 | name: "example", 47 | input: sample, 48 | expected: "70", 49 | }, 50 | } 51 | 52 | for _, tc := range tt { 53 | t.Run(tc.name, func(t *testing.T) { 54 | input := utils.SplitInput(tc.input) 55 | if got := PartTwo(input); got != tc.expected { 56 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 57 | } 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /2022/day03/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day03/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | vJrwpWtwJgWrhcsFMMfFFhFp 11 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 12 | PmmdzqPrVvPwwTWBwg 13 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 14 | ttgJtRGJQctTZtZT 15 | CrZsJsPPZsGzwwsLwLmpwMDw` 16 | 17 | func TestPartOne(t *testing.T) { 18 | tt := []struct { 19 | name string 20 | input string 21 | expected string 22 | }{ 23 | { 24 | name: "example", 25 | input: sample, 26 | expected: "157", 27 | }, 28 | } 29 | 30 | for _, tc := range tt { 31 | t.Run(tc.name, func(t *testing.T) { 32 | input := utils.SplitInput(tc.input) 33 | if got := PartOne(input); got != tc.expected { 34 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func TestPartTwo(t *testing.T) { 41 | tt := []struct { 42 | name string 43 | input string 44 | expected string 45 | }{ 46 | { 47 | name: "example", 48 | input: sample, 49 | expected: "70", 50 | }, 51 | } 52 | 53 | for _, tc := range tt { 54 | t.Run(tc.name, func(t *testing.T) { 55 | input := utils.SplitInput(tc.input) 56 | if got := PartTwo(input); got != tc.expected { 57 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /2022/day03/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: 7872 16 | Part 2: 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day03/giulianopz/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | "strings" 7 | "unicode" 8 | ) 9 | 10 | var lowercase []string = make([]string, 0) 11 | var uppercase []string = make([]string, 0) 12 | 13 | func init() { 14 | for r := 'a'; r <= 'z'; r++ { 15 | lowercase = append(lowercase, fmt.Sprintf("%c", r)) 16 | R := unicode.ToUpper(r) 17 | uppercase = append(uppercase, fmt.Sprintf("%c", R)) 18 | } 19 | } 20 | 21 | func PartOne(input []string) string { 22 | 23 | var totalPriorities int 24 | for _, line := range input { 25 | 26 | separator := len(line) / 2 27 | firstHalf := strings.Split(line[:separator], "") 28 | secondHalf := line[separator:] 29 | 30 | sort.Strings(firstHalf) 31 | for i, c := range firstHalf { 32 | if i == 0 || (firstHalf[i] != firstHalf[i-1]) { 33 | if strings.Contains(secondHalf, c) { 34 | totalPriorities += priority(c) 35 | } 36 | } 37 | } 38 | } 39 | return fmt.Sprintf("%d", totalPriorities) 40 | } 41 | 42 | const offset = 1 43 | 44 | func priority(s string) int { 45 | for i, c := range lowercase { 46 | if c == s { 47 | return i + offset 48 | } 49 | } 50 | for i, c := range uppercase { 51 | if c == s { 52 | return i + len(lowercase) + offset 53 | } 54 | } 55 | return 0 56 | } 57 | 58 | func PartTwo(input []string) string { 59 | 60 | var totalPriorities, i int 61 | for i < len(input) { 62 | 63 | firstRucksack := strings.Split(input[i], "") 64 | secondRucksack := input[i+1] 65 | thirdRucksack := input[i+2] 66 | 67 | sort.Strings(firstRucksack) 68 | for i, c := range firstRucksack { 69 | if i == 0 || (firstRucksack[i] != firstRucksack[i-1]) { 70 | if strings.Contains(secondRucksack, c) && strings.Contains(thirdRucksack, c) { 71 | totalPriorities += priority(c) 72 | } 73 | } 74 | } 75 | i = i + 3 76 | } 77 | return fmt.Sprintf("%d", totalPriorities) 78 | } 79 | -------------------------------------------------------------------------------- /2022/day03/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | vJrwpWtwJgWrhcsFMMfFFhFp 11 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 12 | PmmdzqPrVvPwwTWBwg 13 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 14 | ttgJtRGJQctTZtZT 15 | CrZsJsPPZsGzwwsLwLmpwMDw 16 | ` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "157", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := utils.SplitInput(tc.input) 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "70", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := utils.SplitInput(tc.input) 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day03/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/3](https://github.com/lucianoq/adventofcode/tree/master/2022/3) 3 | -------------------------------------------------------------------------------- /2022/day03/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day03/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func GetCommonChar(line string) rune { 8 | present := make(map[rune]struct{}) 9 | for i, letter := range line { 10 | if i < len(line)/2 { 11 | present[letter] = struct{}{} 12 | } else { 13 | _, ok := present[letter] 14 | if ok { 15 | return letter 16 | } 17 | } 18 | } 19 | return rune(' ') 20 | } 21 | 22 | func GetCommonChars(lines []string, size int) rune { 23 | present := make(map[rune]int) 24 | for _, line := range lines { 25 | currentGroup := make(map[rune]struct{}) 26 | for _, letter := range line { 27 | _, ok := currentGroup[letter] 28 | if !ok { 29 | currentGroup[letter] = struct{}{} 30 | value, ok := present[letter] 31 | if !ok { 32 | present[letter] = 1 33 | } else { 34 | present[letter] = value + 1 35 | } 36 | } 37 | } 38 | } 39 | for k, v := range present { 40 | if v == size { 41 | return k 42 | } 43 | } 44 | return ' ' 45 | } 46 | 47 | func Score(c rune) int { 48 | if c >= 'a' && c <= 'z' { 49 | return int(c) - int('a') + 1 50 | } 51 | if c >= 'A' && c <= 'Z' { 52 | return int(c) - int('A') + 27 53 | } 54 | return 0 55 | } 56 | 57 | func PartOne(input []string) string { 58 | sum := 0 59 | for _, row := range input { 60 | c := GetCommonChar(row) 61 | sum += Score(c) 62 | } 63 | return fmt.Sprintf("%d", sum) 64 | } 65 | 66 | func PartTwo(input []string) string { 67 | size := 3 68 | sum := 0 69 | for i := 0; i < len(input); i += size { 70 | c := GetCommonChars(input[i:i+size], size) 71 | s := Score(c) 72 | sum += s 73 | } 74 | return fmt.Sprintf("%d", sum) 75 | } 76 | -------------------------------------------------------------------------------- /2022/day03/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `vJrwpWtwJgWrhcsFMMfFFhFp 10 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 11 | PmmdzqPrVvPwwTWBwg 12 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 13 | ttgJtRGJQctTZtZT 14 | CrZsJsPPZsGzwwsLwLmpwMDw` 15 | 16 | var sample2 string = `vJrwpWtwJgWrhcsFMMfFFhFp 17 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 18 | PmmdzqPrVvPwwTWBwg 19 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 20 | ttgJtRGJQctTZtZT 21 | CrZsJsPPZsGzwwsLwLmpwMDw 22 | ` 23 | 24 | func TestPartOne(t *testing.T) { 25 | tt := []struct { 26 | name string 27 | input string 28 | expected string 29 | }{ 30 | { 31 | name: "example", 32 | input: sample, 33 | expected: "157", 34 | }, 35 | } 36 | 37 | for _, tc := range tt { 38 | t.Run(tc.name, func(t *testing.T) { 39 | input := utils.SplitInput(tc.input) 40 | if got := PartOne(input); got != tc.expected { 41 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 42 | } 43 | }) 44 | } 45 | } 46 | 47 | func TestPartTwo(t *testing.T) { 48 | tt := []struct { 49 | name string 50 | input string 51 | expected string 52 | }{ 53 | { 54 | name: "example", 55 | input: sample2, 56 | expected: "70", 57 | }, 58 | } 59 | 60 | for _, tc := range tt { 61 | t.Run(tc.name, func(t *testing.T) { 62 | input := utils.SplitInput(tc.input) 63 | if got := PartTwo(input); got != tc.expected { 64 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 65 | } 66 | }) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /2022/day03/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day03/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | vJrwpWtwJgWrhcsFMMfFFhFp 11 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 12 | PmmdzqPrVvPwwTWBwg 13 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 14 | ttgJtRGJQctTZtZT 15 | CrZsJsPPZsGzwwsLwLmpwMDw 16 | ` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "157", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := utils.SplitInput(tc.input) 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "70", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := utils.SplitInput(tc.input) 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day04/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day04/Al-Pragliola/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func PartOne(input []string) string { 9 | totalContained := 0 10 | 11 | for _, line := range input { 12 | ranges := strings.Split(line, ",") 13 | rangeOne := strings.Split(ranges[0], "-") 14 | rangeTwo := strings.Split(ranges[1], "-") 15 | 16 | rangeOneStart, _ := strconv.Atoi(rangeOne[0]) 17 | rangeOneEnd, _ := strconv.Atoi(rangeOne[1]) 18 | rangeTwoStart, _ := strconv.Atoi(rangeTwo[0]) 19 | rangeTwoEnd, _ := strconv.Atoi(rangeTwo[1]) 20 | 21 | if rangeOneStart <= rangeTwoStart && rangeOneEnd >= rangeTwoEnd { 22 | totalContained++ 23 | continue 24 | } 25 | 26 | if rangeTwoStart <= rangeOneStart && rangeTwoEnd >= rangeOneEnd { 27 | totalContained++ 28 | continue 29 | } 30 | } 31 | 32 | return strconv.Itoa(totalContained) 33 | } 34 | 35 | func PartTwo(input []string) string { 36 | totalOverlaps := 0 37 | 38 | for _, line := range input { 39 | ranges := strings.Split(line, ",") 40 | rangeOne := strings.Split(ranges[0], "-") 41 | rangeTwo := strings.Split(ranges[1], "-") 42 | 43 | rangeOneStart, _ := strconv.Atoi(rangeOne[0]) 44 | rangeOneEnd, _ := strconv.Atoi(rangeOne[1]) 45 | rangeTwoStart, _ := strconv.Atoi(rangeTwo[0]) 46 | rangeTwoEnd, _ := strconv.Atoi(rangeTwo[1]) 47 | 48 | if (rangeOneStart < rangeTwoStart && rangeOneEnd < rangeTwoStart) || 49 | (rangeOneStart > rangeTwoEnd && rangeOneEnd > rangeTwoEnd) { 50 | continue 51 | } 52 | 53 | if (rangeTwoStart < rangeOneStart && rangeTwoEnd < rangeOneStart) || 54 | (rangeTwoStart > rangeOneEnd && rangeTwoEnd > rangeOneEnd) { 55 | continue 56 | } 57 | 58 | totalOverlaps++ 59 | } 60 | 61 | return strconv.Itoa(totalOverlaps) 62 | } 63 | -------------------------------------------------------------------------------- /2022/day04/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 2-4,6-8 11 | 2-3,4-5 12 | 5-7,7-9 13 | 2-8,3-7 14 | 6-6,4-6 15 | 2-6,4-8` 16 | 17 | func TestPartOne(t *testing.T) { 18 | tt := []struct { 19 | name string 20 | input string 21 | expected string 22 | }{ 23 | { 24 | name: "example", 25 | input: sample, 26 | expected: "2", 27 | }, 28 | } 29 | 30 | for _, tc := range tt { 31 | t.Run(tc.name, func(t *testing.T) { 32 | input := utils.SplitInput(tc.input) 33 | if got := PartOne(input); got != tc.expected { 34 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func TestPartTwo(t *testing.T) { 41 | tt := []struct { 42 | name string 43 | input string 44 | expected string 45 | }{ 46 | { 47 | name: "example", 48 | input: sample, 49 | expected: "4", 50 | }, 51 | } 52 | 53 | for _, tc := range tt { 54 | t.Run(tc.name, func(t *testing.T) { 55 | input := utils.SplitInput(tc.input) 56 | if got := PartTwo(input); got != tc.expected { 57 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /2022/day04/alessio-perugini/go.mod: -------------------------------------------------------------------------------- 1 | module day4 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221204084937-7169fb3dac38 6 | -------------------------------------------------------------------------------- /2022/day04/alessio-perugini/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221204084937-7169fb3dac38 h1:FgBoeWEE2GcTrV3EmfCDWGDnYKZjqrik9RpuzlXBmug= 2 | github.com/golangroma/aoc v0.0.0-20221204084937-7169fb3dac38/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day04/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day04/alessio-perugini/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | func PartOne(input []string) string { 10 | cnt := 0 11 | 12 | for _, v := range input { 13 | pairs := strings.Split(v, ",") 14 | 15 | min1, max1 := parsePairRange(pairs[0]) 16 | min2, max2 := parsePairRange(pairs[1]) 17 | 18 | if fullyContainedAinB(min1, max1, min2, max2) || fullyContainedAinB(min2, max2, min1, max1) { 19 | cnt++ 20 | } 21 | } 22 | 23 | return fmt.Sprintf("%d", cnt) 24 | } 25 | 26 | func fullyContainedAinB(minA, maxA, minB, maxB int) bool { 27 | return minA <= minB && maxA >= maxB 28 | } 29 | 30 | func parsePairRange(pair string) (int, int) { 31 | interval := strings.Split(pair, "-") 32 | min, _ := strconv.Atoi(interval[0]) 33 | max, _ := strconv.Atoi(interval[1]) 34 | return min, max 35 | } 36 | 37 | func PartTwo(input []string) string { 38 | cnt := 0 39 | 40 | for _, v := range input { 41 | pairs := strings.Split(v, ",") 42 | 43 | min1, max1 := parsePairRange(pairs[0]) 44 | min2, max2 := parsePairRange(pairs[1]) 45 | 46 | if max1 < min2 || max2 < min1 { 47 | continue 48 | } 49 | cnt++ 50 | } 51 | 52 | return fmt.Sprintf("%d", cnt) 53 | } 54 | -------------------------------------------------------------------------------- /2022/day04/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `2-4,6-8 10 | 2-3,4-5 11 | 5-7,7-9 12 | 2-8,3-7 13 | 6-6,4-6 14 | 2-6,4-8 15 | ` 16 | 17 | func TestPartOne(t *testing.T) { 18 | tt := []struct { 19 | name string 20 | input string 21 | expected string 22 | }{ 23 | { 24 | name: "example", 25 | input: sample, 26 | expected: "2", 27 | }, 28 | } 29 | 30 | for _, tc := range tt { 31 | t.Run(tc.name, func(t *testing.T) { 32 | input := utils.SplitInput(tc.input) 33 | if got := PartOne(input); got != tc.expected { 34 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func TestPartTwo(t *testing.T) { 41 | tt := []struct { 42 | name string 43 | input string 44 | expected string 45 | }{ 46 | { 47 | name: "example", 48 | input: sample, 49 | expected: "4", 50 | }, 51 | } 52 | 53 | for _, tc := range tt { 54 | t.Run(tc.name, func(t *testing.T) { 55 | input := utils.SplitInput(tc.input) 56 | if got := PartTwo(input); got != tc.expected { 57 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /2022/day04/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | 15 | /* 16 | Part 1: 532 17 | Part 2: 854 18 | */ 19 | } 20 | -------------------------------------------------------------------------------- /2022/day04/giulianopz/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "strconv" 7 | "strings" 8 | ) 9 | 10 | func PartOne(input []string) string { 11 | 12 | var fullyContainedAssignmentPairs int 13 | for _, line := range input { 14 | pair := strings.Split(line, ",") 15 | firstElf := strings.Split(pair[0], "-") 16 | secondElf := strings.Split(pair[1], "-") 17 | 18 | if (asInt(firstElf[0]) <= asInt(secondElf[0]) && asInt(firstElf[1]) >= asInt(secondElf[1])) || 19 | (asInt(secondElf[0]) <= asInt(firstElf[0]) && asInt(secondElf[1]) >= asInt(firstElf[1])) { 20 | fullyContainedAssignmentPairs += 1 21 | } 22 | } 23 | return fmt.Sprintf("%d", fullyContainedAssignmentPairs) 24 | } 25 | 26 | func PartTwo(input []string) string { 27 | 28 | var overlappingAssignmentPairs int 29 | for _, line := range input { 30 | pair := strings.Split(line, ",") 31 | firstElf := strings.Split(pair[0], "-") 32 | secondElf := strings.Split(pair[1], "-") 33 | 34 | if (asInt(firstElf[0]) <= asInt(secondElf[0]) && asInt(firstElf[1]) >= asInt(secondElf[1])) || 35 | (asInt(secondElf[0]) <= asInt(firstElf[0]) && asInt(secondElf[1]) >= asInt(firstElf[1]) || 36 | asInt(firstElf[0]) <= asInt(secondElf[1]) && asInt(firstElf[1]) >= asInt(secondElf[0])) { 37 | overlappingAssignmentPairs += 1 38 | } 39 | } 40 | return fmt.Sprintf("%d", overlappingAssignmentPairs) 41 | } 42 | 43 | func asInt(s string) int { 44 | i, err := strconv.Atoi(s) 45 | if err != nil { 46 | log.Fatal(err) 47 | } 48 | return i 49 | } 50 | -------------------------------------------------------------------------------- /2022/day04/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 2-4,6-8 11 | 2-3,4-5 12 | 5-7,7-9 13 | 2-8,3-7 14 | 6-6,4-6 15 | 2-6,4-8 16 | ` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "2", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := utils.SplitInput(tc.input) 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "4", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := utils.SplitInput(tc.input) 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day04/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/4](https://github.com/lucianoq/adventofcode/tree/master/2022/4) 3 | -------------------------------------------------------------------------------- /2022/day04/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day04/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | func CheckIfContains(a, b, c, d int) bool { 10 | return a <= c && b >= d || c <= a && d >= b 11 | } 12 | 13 | func CheckIfOverlaps(a, b, c, d int) bool { 14 | if c >= a && c <= b || d >= a && d <= b { 15 | return true 16 | } 17 | if a >= c && a <= d || b >= c && b <= d { 18 | return true 19 | } 20 | return false 21 | } 22 | 23 | func PartOne(input []string) string { 24 | tot := 0 25 | for _, row := range input { 26 | ranges := strings.Split(row, ",") 27 | pair1 := strings.Split(ranges[0], "-") 28 | pair2 := strings.Split(ranges[1], "-") 29 | a, _ := strconv.Atoi(pair1[0]) 30 | b, _ := strconv.Atoi(pair1[1]) 31 | c, _ := strconv.Atoi(pair2[0]) 32 | d, _ := strconv.Atoi(pair2[1]) 33 | contains := CheckIfContains(a, b, c, d) 34 | if contains { 35 | tot += 1 36 | } 37 | } 38 | return fmt.Sprintf("%d", tot) 39 | } 40 | 41 | func PartTwo(input []string) string { 42 | tot := 0 43 | for _, row := range input { 44 | ranges := strings.Split(row, ",") 45 | pair1 := strings.Split(ranges[0], "-") 46 | pair2 := strings.Split(ranges[1], "-") 47 | a, _ := strconv.Atoi(pair1[0]) 48 | b, _ := strconv.Atoi(pair1[1]) 49 | c, _ := strconv.Atoi(pair2[0]) 50 | d, _ := strconv.Atoi(pair2[1]) 51 | contains := CheckIfOverlaps(a, b, c, d) 52 | if contains { 53 | tot += 1 54 | } 55 | } 56 | return fmt.Sprintf("%d", tot) 57 | } 58 | -------------------------------------------------------------------------------- /2022/day04/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `2-4,6-8 10 | 2-3,4-5 11 | 5-7,7-9 12 | 2-8,3-7 13 | 6-6,4-6 14 | 2-6,4-8` 15 | 16 | func TestPartOne(t *testing.T) { 17 | tt := []struct { 18 | name string 19 | input string 20 | expected string 21 | }{ 22 | { 23 | name: "example", 24 | input: sample, 25 | expected: "2", 26 | }, 27 | } 28 | 29 | for _, tc := range tt { 30 | t.Run(tc.name, func(t *testing.T) { 31 | input := utils.SplitInput(tc.input) 32 | if got := PartOne(input); got != tc.expected { 33 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 34 | } 35 | }) 36 | } 37 | } 38 | 39 | func TestPartTwo(t *testing.T) { 40 | tt := []struct { 41 | name string 42 | input string 43 | expected string 44 | }{ 45 | { 46 | name: "example", 47 | input: sample, 48 | expected: "4", 49 | }, 50 | } 51 | 52 | for _, tc := range tt { 53 | t.Run(tc.name, func(t *testing.T) { 54 | input := utils.SplitInput(tc.input) 55 | if got := PartTwo(input); got != tc.expected { 56 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 57 | } 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /2022/day04/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day04/misterdelle/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | func PartOne(input []string) string { 10 | totalRangeFullyContained := 0 11 | count := 0 12 | 13 | for _, v := range input { 14 | pairs := strings.Split(v, ",") 15 | left := pairs[0] 16 | right := pairs[1] 17 | pairsLeft := strings.Split(left, "-") 18 | pairsRight := strings.Split(right, "-") 19 | 20 | startLeft, _ := strconv.Atoi(pairsLeft[0]) 21 | endLeft, _ := strconv.Atoi(pairsLeft[1]) 22 | startRight, _ := strconv.Atoi(pairsRight[0]) 23 | endRight, _ := strconv.Atoi(pairsRight[1]) 24 | 25 | // 26 | // 2-8,3-7 27 | // 2-8 fully contains 3-7 28 | // 29 | // 6-6,4-6 30 | // 6-6 is fully contained by 4-6 31 | // 32 | // 15-57,15-57 33 | // 15-57 is fully contained by 15-57 34 | // 35 | 36 | if startRight == startLeft && endRight == endLeft { 37 | // 38 | // If the ranges are exactly the same I count as one 39 | // 40 | totalRangeFullyContained++ 41 | } else { 42 | if startRight >= startLeft { 43 | if endRight <= endLeft { 44 | // 45 | // Right pair completely contained in Left pair 46 | // 47 | totalRangeFullyContained++ 48 | } 49 | } 50 | 51 | if startLeft >= startRight { 52 | if endLeft <= endRight { 53 | // 54 | // Left pair completely contained in Right pair 55 | // 56 | totalRangeFullyContained++ 57 | } 58 | } 59 | } 60 | 61 | if (startRight-startLeft)*(endLeft-endRight) >= 0 { 62 | count++ 63 | } 64 | } 65 | 66 | fmt.Println(count) 67 | fmt.Println(totalRangeFullyContained) 68 | 69 | return strconv.Itoa(count) 70 | } 71 | 72 | func PartTwo(input []string) string { 73 | var startLeft, endLeft, startRight, endRight int 74 | totalRangeFullyContained := 0 75 | 76 | for _, v := range input { 77 | fmt.Sscanf(v, "%d-%d,%d-%d\n", &startLeft, &endLeft, &startRight, &endRight) 78 | 79 | if (endRight-startLeft)*(endLeft-startRight) >= 0 { 80 | totalRangeFullyContained++ 81 | } 82 | } 83 | 84 | return strconv.Itoa(totalRangeFullyContained) 85 | } 86 | -------------------------------------------------------------------------------- /2022/day04/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 2-4,6-8 11 | 2-3,4-5 12 | 5-7,7-9 13 | 2-8,3-7 14 | 6-6,4-6 15 | 2-6,4-8 16 | ` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "2", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := utils.SplitInput(tc.input) 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "4", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := utils.SplitInput(tc.input) 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day05/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "strings" 7 | 8 | "github.com/golangroma/aoc/utils" 9 | ) 10 | 11 | func main() { 12 | input := ReadFile("input.txt") 13 | 14 | fmt.Printf("Part 1: %v\n", PartOne(input)) 15 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 16 | } 17 | 18 | func ReadFile(filename string) []string { 19 | content, err := os.ReadFile(filename) 20 | utils.CheckErr(err) 21 | 22 | return SplitInput(string(content)) 23 | } 24 | 25 | func SplitInput(content string) []string { 26 | return strings.Split(content, "\n") 27 | } 28 | -------------------------------------------------------------------------------- /2022/day05/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | var sample string = ` [D] 8 | [N] [C] 9 | [Z] [M] [P] 10 | 1 2 3 11 | 12 | move 1 from 2 to 1 13 | move 3 from 1 to 3 14 | move 2 from 2 to 1 15 | move 1 from 1 to 2` 16 | 17 | func TestPartOne(t *testing.T) { 18 | tt := []struct { 19 | name string 20 | input string 21 | expected string 22 | }{ 23 | { 24 | name: "example", 25 | input: sample, 26 | expected: "CMZ", 27 | }, 28 | } 29 | 30 | for _, tc := range tt { 31 | t.Run(tc.name, func(t *testing.T) { 32 | input := SplitInput(tc.input) 33 | if got := PartOne(input); got != tc.expected { 34 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 35 | } 36 | }) 37 | } 38 | } 39 | 40 | func TestPartTwo(t *testing.T) { 41 | tt := []struct { 42 | name string 43 | input string 44 | expected string 45 | }{ 46 | { 47 | name: "example", 48 | input: sample, 49 | expected: "MCD", 50 | }, 51 | } 52 | 53 | for _, tc := range tt { 54 | t.Run(tc.name, func(t *testing.T) { 55 | input := SplitInput(tc.input) 56 | if got := PartTwo(input); got != tc.expected { 57 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 58 | } 59 | }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /2022/day05/alessio-perugini/go.mod: -------------------------------------------------------------------------------- 1 | module day5 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221205071215-269dfb1c5212 6 | -------------------------------------------------------------------------------- /2022/day05/alessio-perugini/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221205071215-269dfb1c5212 h1:ZLCH6tlcB77+Ywm7YuZ8UPiKH8f0mdRXxICldaHZ2B4= 2 | github.com/golangroma/aoc v0.0.0-20221205071215-269dfb1c5212/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day05/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day05/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | var sample string = ` [D] 9 | [N] [C] 10 | [Z] [M] [P] 11 | 1 2 3 12 | 13 | move 1 from 2 to 1 14 | move 3 from 1 to 3 15 | move 2 from 2 to 1 16 | move 1 from 1 to 2` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "CMZ", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := strings.Split(tc.input, "\n") 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "MCD", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := strings.Split(tc.input, "\n") 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day05/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: RNZLFZSJH 16 | Part 2: 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day05/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | [D] 11 | [N] [C] 12 | [Z] [M] [P] 13 | 1 2 3 14 | 15 | move 1 from 2 to 1 16 | move 3 from 1 to 3 17 | move 2 from 2 to 1 18 | move 1 from 1 to 2` 19 | 20 | func TestPartOne(t *testing.T) { 21 | tt := []struct { 22 | name string 23 | input string 24 | expected string 25 | }{ 26 | // { 27 | // name: "example", 28 | // input: sample, 29 | // expected: "CMZ", 30 | // }, 31 | } 32 | 33 | for _, tc := range tt { 34 | t.Run(tc.name, func(t *testing.T) { 35 | input := utils.SplitInput(tc.input) 36 | if got := PartOne(input); got != tc.expected { 37 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 38 | } 39 | }) 40 | } 41 | } 42 | 43 | func TestPartTwo(t *testing.T) { 44 | tt := []struct { 45 | name string 46 | input string 47 | expected string 48 | }{ 49 | // { 50 | // name: "example", 51 | // input: sample, 52 | // expected: "MCD", 53 | // }, 54 | } 55 | 56 | for _, tc := range tt { 57 | t.Run(tc.name, func(t *testing.T) { 58 | input := utils.SplitInput(tc.input) 59 | if got := PartTwo(input); got != tc.expected { 60 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 61 | } 62 | }) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /2022/day05/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/5](https://github.com/lucianoq/adventofcode/tree/master/2022/5) 3 | -------------------------------------------------------------------------------- /2022/day05/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "strings" 7 | 8 | "github.com/golangroma/aoc/utils" 9 | ) 10 | 11 | func ReadFileNoTrim(filename string) []string { 12 | content, err := os.ReadFile(filename) 13 | utils.CheckErr(err) 14 | 15 | return SplitInputNoTrim(string(content)) 16 | } 17 | 18 | func SplitInputNoTrim(content string) []string { 19 | stringContent := string(content) 20 | return strings.Split(stringContent, "\n") 21 | } 22 | 23 | func main() { 24 | input := ReadFileNoTrim("input.txt") 25 | 26 | fmt.Printf("Part 1: %v\n", PartOne(input)) 27 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 28 | } 29 | -------------------------------------------------------------------------------- /2022/day05/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day05/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | var sample string = ` [D] 9 | [N] [C] 10 | [Z] [M] [P] 11 | 1 2 3 12 | 13 | move 1 from 2 to 1 14 | move 3 from 1 to 3 15 | move 2 from 2 to 1 16 | move 1 from 1 to 2` 17 | 18 | func TestPartOne(t *testing.T) { 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example", 26 | input: sample, 27 | expected: "CMZ", 28 | }, 29 | } 30 | 31 | for _, tc := range tt { 32 | t.Run(tc.name, func(t *testing.T) { 33 | input := strings.Split(tc.input, "\n") 34 | if got := PartOne(input); got != tc.expected { 35 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 36 | } 37 | }) 38 | } 39 | } 40 | 41 | func TestPartTwo(t *testing.T) { 42 | tt := []struct { 43 | name string 44 | input string 45 | expected string 46 | }{ 47 | { 48 | name: "example", 49 | input: sample, 50 | expected: "MCD", 51 | }, 52 | } 53 | 54 | for _, tc := range tt { 55 | t.Run(tc.name, func(t *testing.T) { 56 | input := strings.Split(tc.input, "\n") 57 | if got := PartTwo(input); got != tc.expected { 58 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 59 | } 60 | }) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /2022/day06/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day06/Al-Pragliola/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "strconv" 4 | 5 | func PartOne(input []string) string { 6 | msg := input[0] 7 | 8 | return strconv.Itoa(calcMsgIdx(msg, 4)) 9 | } 10 | 11 | func PartTwo(input []string) string { 12 | msg := input[0] 13 | 14 | return strconv.Itoa(calcMsgIdx(msg, 14)) 15 | } 16 | 17 | func calcMsgIdx(msg string, num int) int { 18 | diffCharsNum := 0 19 | 20 | for i := 0; i < len(msg); i++ { 21 | diffChars := make(map[byte]int) 22 | 23 | for j := i; j < i+num; j++ { 24 | if diffChars[msg[j]] == 0 { 25 | diffCharsNum++ 26 | } 27 | 28 | diffChars[msg[j]]++ 29 | } 30 | 31 | if diffCharsNum == num { 32 | return i + num 33 | } 34 | 35 | diffCharsNum = 0 36 | } 37 | 38 | return 0 39 | } 40 | -------------------------------------------------------------------------------- /2022/day06/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var samples = []string{ 10 | "mjqjpqmgbljsphdztnvjfqwrcgsmlb", 11 | "bvwbjplbgvbhsrlpgdmjqwftvncz", 12 | "nppdvjthqldpwncqszvftbrmjlhg", 13 | "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 14 | "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 15 | } 16 | 17 | func TestPartOne(t *testing.T) { 18 | 19 | tt := []struct { 20 | name string 21 | input string 22 | expected string 23 | }{ 24 | { 25 | name: "example 1", 26 | input: samples[0], 27 | expected: "7", 28 | }, { 29 | name: "example 2", 30 | input: samples[1], 31 | expected: "5", 32 | }, { 33 | name: "example 3", 34 | input: samples[2], 35 | expected: "6", 36 | }, { 37 | name: "example 4", 38 | input: samples[3], 39 | expected: "10", 40 | }, { 41 | name: "example 5", 42 | input: samples[4], 43 | expected: "11", 44 | }, 45 | } 46 | 47 | for _, tc := range tt { 48 | t.Run(tc.name, func(t *testing.T) { 49 | input := utils.SplitInput(tc.input) 50 | if got := PartOne(input); got != tc.expected { 51 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 52 | } 53 | }) 54 | } 55 | } 56 | 57 | func TestPartTwo(t *testing.T) { 58 | tt := []struct { 59 | name string 60 | input string 61 | expected string 62 | }{ 63 | { 64 | name: "example 1", 65 | input: samples[0], 66 | expected: "19", 67 | }, { 68 | name: "example 2", 69 | input: samples[1], 70 | expected: "23", 71 | }, { 72 | name: "example 3", 73 | input: samples[2], 74 | expected: "23", 75 | }, { 76 | name: "example 4", 77 | input: samples[3], 78 | expected: "29", 79 | }, { 80 | name: "example 5", 81 | input: samples[4], 82 | expected: "26", 83 | }, 84 | } 85 | 86 | for _, tc := range tt { 87 | t.Run(tc.name, func(t *testing.T) { 88 | input := utils.SplitInput(tc.input) 89 | if got := PartTwo(input); got != tc.expected { 90 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 91 | } 92 | }) 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /2022/day06/alessio-perugini/go.mod: -------------------------------------------------------------------------------- 1 | module day6 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221206074943-62516778f378 6 | -------------------------------------------------------------------------------- /2022/day06/alessio-perugini/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221206074943-62516778f378 h1:lpqCDiUXfwAHnNAIvorPatMW2eEnfZ2qc6StJ5E3oVw= 2 | github.com/golangroma/aoc v0.0.0-20221206074943-62516778f378/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day06/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day06/alessio-perugini/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func PartOne(input []string) string { 6 | return fmt.Sprintf("%d", findFirstUniqueSequenceOfXChars(input[0], 4)) 7 | } 8 | 9 | func PartTwo(input []string) string { 10 | return fmt.Sprintf("%d", findFirstUniqueSequenceOfXChars(input[0], 14)) 11 | } 12 | 13 | func findFirstUniqueSequenceOfXChars(input string, x int) int { 14 | occurrences := make(map[byte]int) 15 | for i := 0; i < x; i++ { 16 | occurrences[input[i]]++ 17 | } 18 | 19 | for i := x; i < len(input); i++ { 20 | if len(occurrences) == x && isUnique(occurrences) { 21 | return i 22 | } 23 | firstElementOfSequence := input[i-x] 24 | if v, ok := occurrences[firstElementOfSequence]; ok { 25 | if v > 1 { 26 | occurrences[firstElementOfSequence]-- 27 | } else { 28 | delete(occurrences, firstElementOfSequence) 29 | } 30 | } 31 | occurrences[input[i]]++ 32 | } 33 | return -1 34 | } 35 | 36 | func isUnique(m map[byte]int) bool { 37 | for _, v := range m { 38 | if v > 1 { 39 | return false 40 | } 41 | } 42 | return true 43 | } 44 | -------------------------------------------------------------------------------- /2022/day06/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func TestPartOne(t *testing.T) { 10 | tt := []struct { 11 | name string 12 | input string 13 | expected string 14 | }{ 15 | { 16 | name: "example", 17 | input: `bvwbjplbgvbhsrlpgdmjqwftvncz`, 18 | expected: "5", 19 | }, 20 | { 21 | name: "2", 22 | input: `nppdvjthqldpwncqszvftbrmjlhg`, 23 | expected: "6", 24 | }, 25 | { 26 | name: "2", 27 | input: `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`, 28 | expected: "10", 29 | }, 30 | } 31 | 32 | for _, tc := range tt { 33 | t.Run(tc.name, func(t *testing.T) { 34 | input := utils.SplitInput(tc.input) 35 | if got := PartOne(input); got != tc.expected { 36 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 37 | } 38 | }) 39 | } 40 | } 41 | 42 | func TestPartTwo(t *testing.T) { 43 | tt := []struct { 44 | name string 45 | input string 46 | expected string 47 | }{ 48 | { 49 | name: "example", 50 | input: `mjqjpqmgbljsphdztnvjfqwrcgsmlb`, 51 | expected: "19", 52 | }, 53 | } 54 | 55 | for _, tc := range tt { 56 | t.Run(tc.name, func(t *testing.T) { 57 | input := utils.SplitInput(tc.input) 58 | if got := PartTwo(input); got != tc.expected { 59 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 60 | } 61 | }) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /2022/day06/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day06/enrichman/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func PartOne(input []string) string { 8 | signal := input[0] 9 | 10 | for i := 0; i < len(signal)-3; i++ { 11 | s := signal[i : i+4] 12 | if areUniqueChars(s) { 13 | return strconv.Itoa(i + 4) 14 | } 15 | } 16 | 17 | return "" 18 | } 19 | 20 | func areUniqueChars(s string) bool { 21 | unique := make(map[rune]struct{}) 22 | for _, r := range s { 23 | unique[r] = struct{}{} 24 | } 25 | return len(unique) == len(s) 26 | } 27 | 28 | func PartTwo(input []string) string { 29 | signal := input[0] 30 | 31 | for i := 0; i < len(signal)-13; i++ { 32 | s := signal[i : i+14] 33 | if areUniqueChars(s) { 34 | return strconv.Itoa(i + 14) 35 | } 36 | } 37 | 38 | return "" 39 | } 40 | -------------------------------------------------------------------------------- /2022/day06/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `` 10 | 11 | func TestPartOne(t *testing.T) { 12 | tt := []struct { 13 | name string 14 | input string 15 | expected string 16 | }{ 17 | { 18 | name: "example 1", 19 | input: "bvwbjplbgvbhsrlpgdmjqwftvncz", 20 | expected: "5", 21 | }, 22 | { 23 | name: "example 2", 24 | input: "nppdvjthqldpwncqszvftbrmjlhg", 25 | expected: "6", 26 | }, 27 | { 28 | name: "example 3", 29 | input: "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 30 | expected: "10", 31 | }, 32 | { 33 | name: "example 4", 34 | input: "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 35 | expected: "11", 36 | }, 37 | } 38 | 39 | for _, tc := range tt { 40 | t.Run(tc.name, func(t *testing.T) { 41 | input := utils.SplitInput(tc.input) 42 | if got := PartOne(input); got != tc.expected { 43 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 44 | } 45 | }) 46 | } 47 | } 48 | 49 | func TestPartTwo(t *testing.T) { 50 | tt := []struct { 51 | name string 52 | input string 53 | expected string 54 | }{ 55 | { 56 | name: "example 1", 57 | input: "mjqjpqmgbljsphdztnvjfqwrcgsmlb", 58 | expected: "19", 59 | }, 60 | { 61 | name: "example 2", 62 | input: "bvwbjplbgvbhsrlpgdmjqwftvncz", 63 | expected: "23", 64 | }, 65 | { 66 | name: "example 3", 67 | input: "nppdvjthqldpwncqszvftbrmjlhg", 68 | expected: "23", 69 | }, 70 | { 71 | name: "example 4", 72 | input: "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 73 | expected: "29", 74 | }, 75 | { 76 | name: "example 5", 77 | input: "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 78 | expected: "26", 79 | }, 80 | } 81 | 82 | for _, tc := range tt { 83 | t.Run(tc.name, func(t *testing.T) { 84 | input := utils.SplitInput(tc.input) 85 | if got := PartTwo(input); got != tc.expected { 86 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 87 | } 88 | }) 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /2022/day06/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: 1625 16 | Part 2: 2250 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day06/giulianopz/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | var occurences = make(map[byte]int, 0) 6 | 7 | func PartOne(input []string) string { 8 | 9 | maxChars := 3 10 | var processedCharsNum int 11 | signals := input[0] 12 | for i := 0; i < len(signals); i++ { 13 | 14 | if i >= maxChars { 15 | 16 | incrByOne(signals[i], signals[i-1], signals[i-2], signals[i-3]) 17 | 18 | if allOnce(signals[i], signals[i-1], signals[i-2], signals[i-3]) { 19 | processedCharsNum = i + 1 20 | break 21 | } 22 | } 23 | } 24 | return fmt.Sprintf("%d", processedCharsNum) 25 | } 26 | 27 | func PartTwo(input []string) string { 28 | 29 | maxChars := 13 30 | var processedCharsNum int 31 | signals := input[0] 32 | for i := 0; i < len(signals); i++ { 33 | 34 | if i >= maxChars { 35 | 36 | lowerBound := i - maxChars 37 | incrByOne([]byte(signals)[lowerBound:i]...) 38 | 39 | if allOnce([]byte(signals)[lowerBound:i]...) { 40 | processedCharsNum = i + 1 41 | break 42 | } 43 | } 44 | } 45 | return fmt.Sprintf("%d", processedCharsNum) 46 | } 47 | 48 | func incrByOne(chars ...byte) { 49 | for _, c := range chars { 50 | occurences[c] += 1 51 | } 52 | } 53 | 54 | func allOnce(chars ...byte) bool { 55 | for _, c := range chars { 56 | if occurences[c] != 1 { 57 | occurences = make(map[byte]int, 0) 58 | return false 59 | } 60 | } 61 | return true 62 | } 63 | -------------------------------------------------------------------------------- /2022/day06/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `bvwbjplbgvbhsrlpgdmjqwftvncz` 10 | var p2sample = `mjqjpqmgbljsphdztnvjfqwrcgsmlb` 11 | 12 | func TestPartOne(t *testing.T) { 13 | tt := []struct { 14 | name string 15 | input string 16 | expected string 17 | }{ 18 | { 19 | name: "example", 20 | input: sample, 21 | expected: "5", 22 | }, 23 | } 24 | 25 | for _, tc := range tt { 26 | t.Run(tc.name, func(t *testing.T) { 27 | input := utils.SplitInput(tc.input) 28 | if got := PartOne(input); got != tc.expected { 29 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 30 | } 31 | }) 32 | } 33 | } 34 | 35 | func TestPartTwo(t *testing.T) { 36 | tt := []struct { 37 | name string 38 | input string 39 | expected string 40 | }{ 41 | { 42 | name: "example", 43 | input: p2sample, 44 | expected: "19", 45 | }, 46 | } 47 | 48 | for _, tc := range tt { 49 | t.Run(tc.name, func(t *testing.T) { 50 | input := utils.SplitInput(tc.input) 51 | if got := PartTwo(input); got != tc.expected { 52 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 53 | } 54 | }) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /2022/day06/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/6](https://github.com/lucianoq/adventofcode/tree/master/2022/6) 3 | 4 | -------------------------------------------------------------------------------- /2022/day06/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day06/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func AllDifferent(slot string) bool { 6 | m := make(map[rune]struct{}) 7 | for _, r := range slot { 8 | _, ok := m[r] 9 | if ok { 10 | return false 11 | } 12 | m[r] = struct{}{} 13 | } 14 | return true 15 | } 16 | 17 | func FindMarker(msg string, length int) int { 18 | for i := 0; i < len(msg)-length; i++ { 19 | if AllDifferent(msg[i : i+length]) { 20 | return i + length 21 | } 22 | } 23 | return 0 24 | } 25 | 26 | func PartOne(input []string) string { 27 | return fmt.Sprintf("%d", FindMarker(input[0], 4)) 28 | } 29 | 30 | func PartTwo(input []string) string { 31 | return fmt.Sprintf("%d", FindMarker(input[0], 14)) 32 | } 33 | -------------------------------------------------------------------------------- /2022/day06/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func TestPartOne(t *testing.T) { 10 | tt := []struct { 11 | name string 12 | input string 13 | expected string 14 | }{ 15 | { 16 | name: "example1", 17 | input: "bvwbjplbgvbhsrlpgdmjqwftvncz", 18 | expected: "5", 19 | }, 20 | { 21 | name: "example2", 22 | input: "nppdvjthqldpwncqszvftbrmjlhg", 23 | expected: "6", 24 | }, 25 | { 26 | name: "example3", 27 | input: "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 28 | expected: "10", 29 | }, 30 | { 31 | name: "example4", 32 | input: "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 33 | expected: "11", 34 | }, 35 | } 36 | 37 | for _, tc := range tt { 38 | t.Run(tc.name, func(t *testing.T) { 39 | input := utils.SplitInput(tc.input) 40 | if got := PartOne(input); got != tc.expected { 41 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 42 | } 43 | }) 44 | } 45 | } 46 | 47 | func TestPartTwo(t *testing.T) { 48 | tt := []struct { 49 | name string 50 | input string 51 | expected string 52 | }{ 53 | { 54 | name: "example1", 55 | input: "mjqjpqmgbljsphdztnvjfqwrcgsmlb", 56 | expected: "19", 57 | }, 58 | { 59 | name: "example2", 60 | input: "bvwbjplbgvbhsrlpgdmjqwftvncz", 61 | expected: "23", 62 | }, 63 | { 64 | name: "example3", 65 | input: "nppdvjthqldpwncqszvftbrmjlhg", 66 | expected: "23", 67 | }, 68 | { 69 | name: "example4", 70 | input: "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 71 | expected: "29", 72 | }, 73 | { 74 | name: "example5", 75 | input: "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 76 | expected: "26", 77 | }, 78 | } 79 | 80 | for _, tc := range tt { 81 | t.Run(tc.name, func(t *testing.T) { 82 | input := utils.SplitInput(tc.input) 83 | if got := PartTwo(input); got != tc.expected { 84 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 85 | } 86 | }) 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /2022/day06/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day06/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func TestPartOne(t *testing.T) { 10 | tt := []struct { 11 | name string 12 | input string 13 | expected string 14 | }{ 15 | { 16 | name: "example1", 17 | input: `mjqjpqmgbljsphdztnvjfqwrcgsmlb`, 18 | expected: "7", 19 | }, 20 | { 21 | name: "example2", 22 | input: `bvwbjplbgvbhsrlpgdmjqwftvncz`, 23 | expected: "5", 24 | }, 25 | { 26 | name: "example3", 27 | input: `nppdvjthqldpwncqszvftbrmjlhg`, 28 | expected: "6", 29 | }, 30 | { 31 | name: "example4", 32 | input: `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`, 33 | expected: "10", 34 | }, 35 | { 36 | name: "example5", 37 | input: `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`, 38 | expected: "11", 39 | }, 40 | } 41 | 42 | for _, tc := range tt { 43 | t.Run(tc.name, func(t *testing.T) { 44 | input := utils.SplitInput(tc.input) 45 | if got := PartOne(input); got != tc.expected { 46 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 47 | } 48 | }) 49 | } 50 | } 51 | 52 | func TestPartTwo(t *testing.T) { 53 | tt := []struct { 54 | name string 55 | input string 56 | expected string 57 | }{ 58 | { 59 | name: "example1", 60 | input: `mjqjpqmgbljsphdztnvjfqwrcgsmlb`, 61 | expected: "19", 62 | }, 63 | { 64 | name: "example2", 65 | input: `bvwbjplbgvbhsrlpgdmjqwftvncz`, 66 | expected: "23", 67 | }, 68 | { 69 | name: "example3", 70 | input: `nppdvjthqldpwncqszvftbrmjlhg`, 71 | expected: "23", 72 | }, 73 | { 74 | name: "example4", 75 | input: `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`, 76 | expected: "29", 77 | }, 78 | { 79 | name: "example5", 80 | input: `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`, 81 | expected: "26", 82 | }, 83 | } 84 | 85 | for _, tc := range tt { 86 | t.Run(tc.name, func(t *testing.T) { 87 | input := utils.SplitInput(tc.input) 88 | if got := PartTwo(input); got != tc.expected { 89 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 90 | } 91 | }) 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /2022/day07/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day07/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | $ cd / 11 | $ ls 12 | dir a 13 | 14848514 b.txt 14 | 8504156 c.dat 15 | dir d 16 | $ cd a 17 | $ ls 18 | dir e 19 | 29116 f 20 | 2557 g 21 | 62596 h.lst 22 | $ cd e 23 | $ ls 24 | 584 i 25 | $ cd .. 26 | $ cd .. 27 | $ cd d 28 | $ ls 29 | 4060174 j 30 | 8033020 d.log 31 | 5626152 d.ext 32 | 7214296 k` 33 | 34 | func TestPartOne(t *testing.T) { 35 | tt := []struct { 36 | name string 37 | input string 38 | expected string 39 | }{ 40 | { 41 | name: "example", 42 | input: sample, 43 | expected: "95437", 44 | }, 45 | } 46 | 47 | for _, tc := range tt { 48 | t.Run(tc.name, func(t *testing.T) { 49 | input := utils.SplitInput(tc.input) 50 | if got := PartOne(input); got != tc.expected { 51 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 52 | } 53 | }) 54 | } 55 | } 56 | 57 | func TestPartTwo(t *testing.T) { 58 | tt := []struct { 59 | name string 60 | input string 61 | expected string 62 | }{ 63 | { 64 | name: "example", 65 | input: sample, 66 | expected: "24933642", 67 | }, 68 | } 69 | 70 | for _, tc := range tt { 71 | t.Run(tc.name, func(t *testing.T) { 72 | input := utils.SplitInput(tc.input) 73 | if got := PartTwo(input); got != tc.expected { 74 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 75 | } 76 | }) 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /2022/day07/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day07/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | $ cd / 11 | $ ls 12 | dir a 13 | 14848514 b.txt 14 | 8504156 c.dat 15 | dir d 16 | $ cd a 17 | $ ls 18 | dir e 19 | 29116 f 20 | 2557 g 21 | 62596 h.lst 22 | $ cd e 23 | $ ls 24 | 584 i 25 | $ cd .. 26 | $ cd .. 27 | $ cd d 28 | $ ls 29 | 4060174 j 30 | 8033020 d.log 31 | 5626152 d.ext 32 | 7214296 k` 33 | 34 | func TestPartOne(t *testing.T) { 35 | tt := []struct { 36 | name string 37 | input string 38 | expected string 39 | }{ 40 | { 41 | name: "example", 42 | input: sample, 43 | expected: "95437", 44 | }, 45 | } 46 | 47 | for _, tc := range tt { 48 | t.Run(tc.name, func(t *testing.T) { 49 | input := utils.SplitInput(tc.input) 50 | if got := PartOne(input); got != tc.expected { 51 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 52 | } 53 | }) 54 | } 55 | } 56 | 57 | func TestPartTwo(t *testing.T) { 58 | tt := []struct { 59 | name string 60 | input string 61 | expected string 62 | }{ 63 | { 64 | name: "example", 65 | input: sample, 66 | expected: "24933642", 67 | }, 68 | } 69 | 70 | for _, tc := range tt { 71 | t.Run(tc.name, func(t *testing.T) { 72 | input := utils.SplitInput(tc.input) 73 | if got := PartTwo(input); got != tc.expected { 74 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 75 | } 76 | }) 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /2022/day07/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/7](https://github.com/lucianoq/adventofcode/tree/master/2022/7) 3 | 4 | -------------------------------------------------------------------------------- /2022/day07/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day07/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `$ cd / 10 | $ ls 11 | dir a 12 | 14848514 b.txt 13 | 8504156 c.dat 14 | dir d 15 | $ cd a 16 | $ ls 17 | dir e 18 | 29116 f 19 | 2557 g 20 | 62596 h.lst 21 | $ cd e 22 | $ ls 23 | 584 i 24 | $ cd .. 25 | $ cd .. 26 | $ cd d 27 | $ ls 28 | 4060174 j 29 | 8033020 d.log 30 | 5626152 d.ext 31 | 7214296 k` 32 | 33 | func TestPartOne(t *testing.T) { 34 | tt := []struct { 35 | name string 36 | input string 37 | expected string 38 | }{ 39 | { 40 | name: "example", 41 | input: sample, 42 | expected: "95437", 43 | }, 44 | } 45 | 46 | for _, tc := range tt { 47 | t.Run(tc.name, func(t *testing.T) { 48 | input := utils.SplitInput(tc.input) 49 | if got := PartOne(input); got != tc.expected { 50 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 51 | } 52 | }) 53 | } 54 | } 55 | 56 | func TestPartTwo(t *testing.T) { 57 | tt := []struct { 58 | name string 59 | input string 60 | expected string 61 | }{ 62 | { 63 | name: "example", 64 | input: sample, 65 | expected: "24933642", 66 | }, 67 | } 68 | 69 | for _, tc := range tt { 70 | t.Run(tc.name, func(t *testing.T) { 71 | input := utils.SplitInput(tc.input) 72 | if got := PartTwo(input); got != tc.expected { 73 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 74 | } 75 | }) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /2022/day07/misterdelle/elf-directory.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | type ElfDirectory struct { 9 | name string 10 | files map[string]*ElfFile 11 | children map[string]*ElfDirectory 12 | parent *ElfDirectory 13 | } 14 | 15 | func newElfDirectory(name string, parent *ElfDirectory) ElfDirectory { 16 | ed := ElfDirectory{ 17 | name: name, 18 | parent: parent, 19 | files: make(map[string]*ElfFile), 20 | } 21 | 22 | if parent != nil { 23 | if parent.children == nil { 24 | parent.children = make(map[string]*ElfDirectory) 25 | } 26 | parent.children[name] = &ed 27 | } 28 | 29 | return ed 30 | } 31 | 32 | func (d *ElfDirectory) isParsable(input string) bool { 33 | return strings.HasPrefix(input, "dir") 34 | } 35 | 36 | func parseElfDirectory(input string, parent *ElfDirectory) *ElfDirectory { 37 | e := ElfDirectory{} 38 | 39 | if !e.isParsable(input) { 40 | return &e 41 | } 42 | 43 | slices := strings.Split(input, " ") 44 | 45 | name := slices[1] 46 | 47 | e = newElfDirectory(name, parent) 48 | 49 | return &e 50 | } 51 | 52 | func (d *ElfDirectory) toString() string { 53 | return fmt.Sprintf("ElfDirectory Name: %s", d.name) 54 | } 55 | 56 | func (d *ElfDirectory) addFile(f ElfFile) { 57 | if d.files == nil { 58 | d.files = make(map[string]*ElfFile) 59 | } 60 | d.files[f.name] = &f 61 | } 62 | 63 | func (d *ElfDirectory) size() int { 64 | size := 0 65 | for v := range d.files { 66 | size += d.files[v].size 67 | } 68 | 69 | for v := range d.children { 70 | child := d.children[v] 71 | size += child.size() 72 | } 73 | 74 | return size 75 | } 76 | 77 | func (d *ElfDirectory) findAllDirectories() []*ElfDirectory { 78 | var allDirs []*ElfDirectory 79 | 80 | for _, v := range d.children { 81 | allDirs = append(allDirs, v) 82 | allSubDirs := v.findAllDirectories() 83 | for i := 0; i < len(allSubDirs); i++ { 84 | allDirs = append(allDirs, allSubDirs[i]) 85 | } 86 | } 87 | 88 | return allDirs 89 | } 90 | -------------------------------------------------------------------------------- /2022/day07/misterdelle/elf-file.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | type ElfFile struct { 10 | name string 11 | size int 12 | } 13 | 14 | func newElfFile(name string, size int) ElfFile { 15 | return ElfFile{name: name, size: size} 16 | } 17 | 18 | func (f *ElfFile) isParsable(input string) bool { 19 | slices := strings.Split(input, " ") 20 | 21 | if len(slices) != 2 { 22 | return false 23 | } 24 | 25 | if slices[0][0] >= '1' && slices[0][0] <= '9' { 26 | return true 27 | } 28 | 29 | return false 30 | } 31 | 32 | func parseElfFile(input string) ElfFile { 33 | e := ElfFile{} 34 | 35 | if !e.isParsable(input) { 36 | return e 37 | } 38 | 39 | slices := strings.Split(input, " ") 40 | 41 | size, _ := strconv.Atoi(slices[0]) 42 | name := slices[1] 43 | 44 | e = newElfFile(name, size) 45 | 46 | return e 47 | } 48 | 49 | func (f *ElfFile) toString() string { 50 | return fmt.Sprintf("ElfFile Name: %s - Size: %d", f.name, f.size) 51 | } 52 | -------------------------------------------------------------------------------- /2022/day07/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day08/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day08/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 30373 11 | 25512 12 | 65332 13 | 33549 14 | 35390` 15 | 16 | func TestPartOne(t *testing.T) { 17 | tt := []struct { 18 | name string 19 | input string 20 | expected string 21 | }{ 22 | { 23 | name: "example", 24 | input: sample, 25 | expected: "21", 26 | }, 27 | } 28 | 29 | for _, tc := range tt { 30 | t.Run(tc.name, func(t *testing.T) { 31 | input := utils.SplitInput(tc.input) 32 | if got := PartOne(input); got != tc.expected { 33 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 34 | } 35 | }) 36 | } 37 | } 38 | 39 | func TestPartTwo(t *testing.T) { 40 | tt := []struct { 41 | name string 42 | input string 43 | expected string 44 | }{ 45 | { 46 | name: "example", 47 | input: sample, 48 | expected: "8", 49 | }, 50 | } 51 | 52 | for _, tc := range tt { 53 | t.Run(tc.name, func(t *testing.T) { 54 | input := utils.SplitInput(tc.input) 55 | if got := PartTwo(input); got != tc.expected { 56 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 57 | } 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /2022/day08/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: 1672 16 | Part 2: 327180 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day08/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 30373 11 | 25512 12 | 65332 13 | 33549 14 | 35390` 15 | 16 | var sample2 = ` 17 | 30373 18 | 25512 19 | 65332 20 | 33549 21 | 35390 22 | ` 23 | 24 | func TestPartOne(t *testing.T) { 25 | tt := []struct { 26 | name string 27 | input string 28 | expected string 29 | }{ 30 | { 31 | name: "example", 32 | input: sample, 33 | expected: "21", 34 | }, 35 | } 36 | 37 | for _, tc := range tt { 38 | t.Run(tc.name, func(t *testing.T) { 39 | input := utils.SplitInput(tc.input) 40 | if got := PartOne(input); got != tc.expected { 41 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 42 | } 43 | }) 44 | } 45 | } 46 | 47 | func TestPartTwo(t *testing.T) { 48 | tt := []struct { 49 | name string 50 | input string 51 | expected string 52 | }{ 53 | { 54 | name: "example", 55 | input: sample2, 56 | expected: "8", 57 | }, 58 | } 59 | 60 | for _, tc := range tt { 61 | t.Run(tc.name, func(t *testing.T) { 62 | input := utils.SplitInput(tc.input) 63 | if got := PartTwo(input); got != tc.expected { 64 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 65 | } 66 | }) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /2022/day08/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/8](https://github.com/lucianoq/adventofcode/tree/master/2022/8) 3 | 4 | -------------------------------------------------------------------------------- /2022/day08/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day08/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `30373 10 | 25512 11 | 65332 12 | 33549 13 | 35390` 14 | 15 | func TestPartOne(t *testing.T) { 16 | tt := []struct { 17 | name string 18 | input string 19 | expected string 20 | }{ 21 | { 22 | name: "example", 23 | input: sample, 24 | expected: "21", 25 | }, 26 | } 27 | 28 | for _, tc := range tt { 29 | t.Run(tc.name, func(t *testing.T) { 30 | input := utils.SplitInput(tc.input) 31 | if got := PartOne(input); got != tc.expected { 32 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 33 | } 34 | }) 35 | } 36 | } 37 | 38 | func TestPartTwo(t *testing.T) { 39 | tt := []struct { 40 | name string 41 | input string 42 | expected string 43 | }{ 44 | { 45 | name: "example", 46 | input: sample, 47 | expected: "8", 48 | }, 49 | } 50 | 51 | for _, tc := range tt { 52 | t.Run(tc.name, func(t *testing.T) { 53 | input := utils.SplitInput(tc.input) 54 | if got := PartTwo(input); got != tc.expected { 55 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 56 | } 57 | }) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /2022/day08/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day09/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | 16 | // part one 6464 17 | // part two 2604 18 | -------------------------------------------------------------------------------- /2022/day09/giulianopz/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | /* 15 | Part 1: 6212 16 | Part 2: 2522 17 | */ 18 | } 19 | -------------------------------------------------------------------------------- /2022/day09/giulianopz/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | R 4 11 | U 4 12 | L 3 13 | D 1 14 | R 4 15 | D 1 16 | L 5 17 | R 2` 18 | 19 | var sample2 string = ` 20 | R 5 21 | U 8 22 | L 8 23 | D 3 24 | R 17 25 | D 10 26 | L 25 27 | U 20 28 | ` 29 | 30 | func TestPartOne(t *testing.T) { 31 | tt := []struct { 32 | name string 33 | input string 34 | expected string 35 | }{ 36 | { 37 | name: "example", 38 | input: sample, 39 | expected: "13", 40 | }, 41 | } 42 | 43 | for _, tc := range tt { 44 | t.Run(tc.name, func(t *testing.T) { 45 | input := utils.SplitInput(tc.input) 46 | if got := PartOne(input); got != tc.expected { 47 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 48 | } 49 | }) 50 | } 51 | } 52 | 53 | func TestPartTwo(t *testing.T) { 54 | tt := []struct { 55 | name string 56 | input string 57 | expected string 58 | }{ 59 | { 60 | name: "example", 61 | input: sample2, 62 | expected: "36", 63 | }, 64 | } 65 | 66 | for _, tc := range tt { 67 | t.Run(tc.name, func(t *testing.T) { 68 | input := utils.SplitInput(tc.input) 69 | if got := PartTwo(input); got != tc.expected { 70 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 71 | } 72 | }) 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /2022/day09/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/9](https://github.com/lucianoq/adventofcode/tree/master/2022/9) 3 | 4 | -------------------------------------------------------------------------------- /2022/day09/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day09/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `R 4 10 | U 4 11 | L 3 12 | D 1 13 | R 4 14 | D 1 15 | L 5 16 | R 2` 17 | 18 | var sample2 string = `R 5 19 | U 8 20 | L 8 21 | D 3 22 | R 17 23 | D 10 24 | L 25 25 | U 20` 26 | 27 | func TestPartOne(t *testing.T) { 28 | tt := []struct { 29 | name string 30 | input string 31 | expected string 32 | }{ 33 | { 34 | name: "example", 35 | input: sample, 36 | expected: "13", 37 | }, 38 | } 39 | 40 | for _, tc := range tt { 41 | t.Run(tc.name, func(t *testing.T) { 42 | input := utils.SplitInput(tc.input) 43 | if got := PartOne(input); got != tc.expected { 44 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 45 | } 46 | }) 47 | } 48 | } 49 | 50 | func TestPartTwo(t *testing.T) { 51 | tt := []struct { 52 | name string 53 | input string 54 | expected string 55 | }{ 56 | { 57 | name: "example", 58 | input: sample2, 59 | expected: "36", 60 | }, 61 | } 62 | 63 | for _, tc := range tt { 64 | t.Run(tc.name, func(t *testing.T) { 65 | input := utils.SplitInput(tc.input) 66 | if got := PartTwo(input); got != tc.expected { 67 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 68 | } 69 | }) 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /2022/day09/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day09/misterdelle/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | "testing" 6 | ) 7 | 8 | // var sample string = ` 9 | // R 4 10 | // U 4 11 | // L 3 12 | // D 1 13 | // R 4 14 | // D 1 15 | // L 5 16 | // R 2 17 | // ` 18 | 19 | func TestPartOne(t *testing.T) { 20 | var sampleOne string = `R 4 21 | U 4 22 | L 3 23 | D 1 24 | R 4 25 | D 1 26 | L 5 27 | R 2` 28 | 29 | tt := []struct { 30 | name string 31 | input string 32 | expected string 33 | }{ 34 | { 35 | name: "example", 36 | input: sampleOne, 37 | expected: "13", 38 | }, 39 | } 40 | 41 | for _, tc := range tt { 42 | t.Run(tc.name, func(t *testing.T) { 43 | //input := utils.SplitInput(tc.input) 44 | input := strings.Split(tc.input, "\n") 45 | if got := PartOne(input); got != tc.expected { 46 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 47 | } 48 | }) 49 | } 50 | } 51 | 52 | func TestPartTwo(t *testing.T) { 53 | var sampleTwo string = `R 5 54 | U 8 55 | L 8 56 | D 3 57 | R 17 58 | D 10 59 | L 25 60 | U 20` 61 | 62 | tt := []struct { 63 | name string 64 | input string 65 | expected string 66 | }{ 67 | { 68 | name: "example", 69 | input: sampleTwo, 70 | expected: "36", 71 | }, 72 | } 73 | 74 | for _, tc := range tt { 75 | t.Run(tc.name, func(t *testing.T) { 76 | input := strings.Split(tc.input, "\n") 77 | if got := PartTwo(input); got != tc.expected { 78 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 79 | } 80 | }) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /2022/day10/Al-Pragliola/input.txt: -------------------------------------------------------------------------------- 1 | addx 1 2 | addx 4 3 | noop 4 | noop 5 | addx 30 6 | addx -24 7 | addx -1 8 | noop 9 | addx 4 10 | addx 1 11 | addx 5 12 | addx -4 13 | addx 5 14 | addx 4 15 | addx 1 16 | noop 17 | addx 5 18 | addx -1 19 | addx 5 20 | addx 3 21 | noop 22 | addx -38 23 | addx 9 24 | addx -4 25 | noop 26 | addx 3 27 | noop 28 | addx 2 29 | addx 3 30 | noop 31 | addx 2 32 | addx 3 33 | noop 34 | addx 2 35 | addx 3 36 | noop 37 | addx 2 38 | addx -17 39 | addx 22 40 | addx -2 41 | addx 5 42 | addx 2 43 | addx 3 44 | addx -2 45 | addx -36 46 | noop 47 | addx 5 48 | noop 49 | addx 3 50 | noop 51 | addx 2 52 | addx -5 53 | noop 54 | addx 10 55 | addx 3 56 | addx -2 57 | addx 3 58 | addx 2 59 | addx 4 60 | noop 61 | noop 62 | noop 63 | noop 64 | addx 3 65 | noop 66 | noop 67 | addx 7 68 | addx 1 69 | noop 70 | noop 71 | addx -38 72 | addx 39 73 | addx -32 74 | noop 75 | noop 76 | noop 77 | addx 5 78 | addx 2 79 | addx -1 80 | addx 4 81 | noop 82 | addx 5 83 | addx -2 84 | addx 5 85 | addx 2 86 | addx -26 87 | addx 31 88 | addx -2 89 | addx 4 90 | addx 3 91 | addx -18 92 | addx 19 93 | addx -38 94 | addx 7 95 | noop 96 | noop 97 | addx 34 98 | addx -39 99 | addx 8 100 | addx 5 101 | addx 2 102 | addx 10 103 | addx -5 104 | addx -2 105 | addx 5 106 | addx 2 107 | addx 11 108 | addx -6 109 | noop 110 | addx 3 111 | noop 112 | addx 2 113 | addx 3 114 | addx -2 115 | addx -38 116 | noop 117 | noop 118 | noop 119 | addx 5 120 | addx 11 121 | noop 122 | addx -3 123 | noop 124 | noop 125 | noop 126 | addx 2 127 | noop 128 | addx -11 129 | addx 16 130 | noop 131 | addx 3 132 | addx 2 133 | addx 8 134 | noop 135 | noop 136 | noop 137 | noop 138 | noop 139 | addx 4 140 | addx 3 141 | noop 142 | addx -20 143 | noop 144 | -------------------------------------------------------------------------------- /2022/day10/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day10/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/10](https://github.com/lucianoq/adventofcode/tree/master/2022/10) 3 | 4 | -------------------------------------------------------------------------------- /2022/day10/mastrogiovanni/input.txt: -------------------------------------------------------------------------------- 1 | noop 2 | noop 3 | noop 4 | addx 4 5 | addx 1 6 | addx 5 7 | addx 1 8 | addx 5 9 | noop 10 | addx -1 11 | addx -6 12 | addx 11 13 | noop 14 | noop 15 | noop 16 | noop 17 | addx 6 18 | addx 5 19 | noop 20 | noop 21 | noop 22 | addx -30 23 | addx 34 24 | addx 2 25 | addx -39 26 | noop 27 | addx 5 28 | addx 2 29 | addx 19 30 | addx -18 31 | addx 2 32 | addx 5 33 | addx 2 34 | addx 3 35 | noop 36 | addx 2 37 | addx 3 38 | noop 39 | addx 2 40 | addx 3 41 | noop 42 | addx 2 43 | addx 3 44 | noop 45 | addx 2 46 | addx -15 47 | addx -22 48 | noop 49 | noop 50 | addx 5 51 | addx 2 52 | noop 53 | noop 54 | addx 14 55 | addx -11 56 | addx 5 57 | addx 2 58 | addx 3 59 | noop 60 | addx 2 61 | addx -16 62 | addx 17 63 | addx 2 64 | addx 5 65 | addx 2 66 | addx -6 67 | addx -25 68 | addx 35 69 | addx 1 70 | addx -36 71 | addx 1 72 | addx 22 73 | addx -19 74 | addx 5 75 | addx 2 76 | noop 77 | noop 78 | addx 5 79 | noop 80 | noop 81 | noop 82 | addx 1 83 | addx 4 84 | noop 85 | noop 86 | noop 87 | addx 5 88 | noop 89 | addx 1 90 | addx 2 91 | addx 3 92 | addx 4 93 | addx -34 94 | addx 21 95 | addx -24 96 | addx 2 97 | addx 5 98 | addx 7 99 | addx -6 100 | addx 2 101 | addx 30 102 | addx -23 103 | addx 10 104 | addx -9 105 | addx 2 106 | addx 2 107 | addx 5 108 | addx -12 109 | addx 13 110 | addx 2 111 | addx 5 112 | addx 2 113 | addx -12 114 | addx -24 115 | addx -1 116 | noop 117 | addx 3 118 | addx 3 119 | addx 1 120 | addx 5 121 | addx 21 122 | addx -16 123 | noop 124 | addx 19 125 | addx -18 126 | addx 2 127 | addx 5 128 | addx 2 129 | addx 3 130 | noop 131 | addx 3 132 | addx -1 133 | addx 1 134 | addx 2 135 | addx -18 136 | addx 1 137 | noop -------------------------------------------------------------------------------- /2022/day10/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day10/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | func PartOne(inputs []string) string { 10 | indexCycle := 0 11 | samplerCycles := []int{20, 60, 100, 140, 180, 220} 12 | currentCycle := 1 13 | cycle := samplerCycles[indexCycle] 14 | tot := 0 15 | X := 1 16 | for _, line := range inputs { 17 | if line == "noop" { 18 | if currentCycle+1 == cycle { 19 | tot += X * (currentCycle + 1) 20 | if indexCycle+1 >= len(samplerCycles) { 21 | break 22 | } 23 | cycle = samplerCycles[indexCycle+1] 24 | indexCycle++ 25 | } 26 | currentCycle++ 27 | } else { 28 | comps := strings.Split(line, " ") 29 | value, _ := strconv.Atoi(comps[1]) 30 | if currentCycle+2 >= cycle { 31 | if currentCycle+2 == cycle { 32 | tot += (X + value) * (currentCycle + 2) 33 | } else { 34 | tot += X * (currentCycle + 1) 35 | } 36 | if indexCycle+1 >= len(samplerCycles) { 37 | break 38 | } 39 | cycle = samplerCycles[indexCycle+1] 40 | indexCycle++ 41 | } 42 | X += value 43 | currentCycle += 2 44 | } 45 | if indexCycle >= len(samplerCycles) { 46 | break 47 | } 48 | } 49 | return fmt.Sprintf("%d", tot) 50 | } 51 | 52 | func PartTwo(inputs []string) string { 53 | X := 1 54 | clock := 0 55 | crt := "" 56 | value := 0 57 | length := 1 58 | for _, line := range inputs { 59 | // Fetch 60 | if line == "noop" { 61 | value = 0 62 | length = 1 63 | } else { 64 | comps := strings.Split(line, " ") 65 | value, _ = strconv.Atoi(comps[1]) 66 | length = 2 67 | } 68 | 69 | // Crt draw 70 | for i := 0; i < length; i++ { 71 | cursor := clock % 40 72 | if cursor >= X-1 && cursor <= X+1 { 73 | crt += "#" 74 | } else { 75 | crt += "." 76 | } 77 | if i == length-1 { 78 | X = X + value 79 | } 80 | clock++ 81 | if clock%40 < (clock-1)%40 { 82 | crt += "\n" 83 | } 84 | } 85 | 86 | } 87 | return crt 88 | } 89 | -------------------------------------------------------------------------------- /2022/day10/misterdelle/input.txt: -------------------------------------------------------------------------------- 1 | noop 2 | addx 10 3 | addx -4 4 | addx -1 5 | noop 6 | noop 7 | addx 5 8 | addx -12 9 | addx 17 10 | noop 11 | addx 1 12 | addx 2 13 | noop 14 | addx 3 15 | addx 2 16 | noop 17 | noop 18 | addx 7 19 | addx 3 20 | noop 21 | addx 2 22 | noop 23 | noop 24 | addx 1 25 | addx -38 26 | addx 5 27 | addx 2 28 | addx 3 29 | addx -2 30 | addx 2 31 | addx 5 32 | addx 2 33 | addx -4 34 | addx 26 35 | addx -19 36 | addx 2 37 | addx 5 38 | addx -2 39 | addx 7 40 | addx -2 41 | addx 5 42 | addx 2 43 | addx 4 44 | addx -17 45 | addx -23 46 | addx 1 47 | addx 5 48 | addx 3 49 | noop 50 | addx 2 51 | addx 24 52 | addx 4 53 | addx -23 54 | noop 55 | addx 5 56 | addx -1 57 | addx 6 58 | noop 59 | addx -2 60 | noop 61 | noop 62 | noop 63 | addx 7 64 | addx 1 65 | addx 4 66 | noop 67 | noop 68 | noop 69 | noop 70 | addx -37 71 | addx 5 72 | addx 2 73 | addx 1 74 | noop 75 | addx 4 76 | addx -2 77 | addx -4 78 | addx 9 79 | addx 7 80 | noop 81 | noop 82 | addx 2 83 | addx 3 84 | addx -2 85 | noop 86 | addx -12 87 | addx 17 88 | noop 89 | addx 3 90 | addx 2 91 | addx -3 92 | addx -30 93 | addx 3 94 | noop 95 | addx 2 96 | addx 3 97 | addx -2 98 | addx 2 99 | addx 5 100 | addx 2 101 | addx 11 102 | addx -6 103 | noop 104 | addx 2 105 | addx -19 106 | addx 20 107 | addx -7 108 | addx 14 109 | addx 8 110 | addx -7 111 | addx 2 112 | addx -26 113 | addx -7 114 | noop 115 | noop 116 | addx 5 117 | addx -2 118 | addx 5 119 | addx 15 120 | addx -13 121 | addx 5 122 | noop 123 | noop 124 | addx 1 125 | addx 4 126 | addx 3 127 | addx -2 128 | addx 4 129 | addx 1 130 | noop 131 | addx 2 132 | noop 133 | addx 3 134 | addx 2 135 | noop 136 | noop 137 | noop 138 | noop 139 | noop 140 | -------------------------------------------------------------------------------- /2022/day10/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day11/Al-Pragliola/input.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 83, 88, 96, 79, 86, 88, 70 3 | Operation: new = old * 5 4 | Test: divisible by 11 5 | If true: throw to monkey 2 6 | If false: throw to monkey 3 7 | 8 | Monkey 1: 9 | Starting items: 59, 63, 98, 85, 68, 72 10 | Operation: new = old * 11 11 | Test: divisible by 5 12 | If true: throw to monkey 4 13 | If false: throw to monkey 0 14 | 15 | Monkey 2: 16 | Starting items: 90, 79, 97, 52, 90, 94, 71, 70 17 | Operation: new = old + 2 18 | Test: divisible by 19 19 | If true: throw to monkey 5 20 | If false: throw to monkey 6 21 | 22 | Monkey 3: 23 | Starting items: 97, 55, 62 24 | Operation: new = old + 5 25 | Test: divisible by 13 26 | If true: throw to monkey 2 27 | If false: throw to monkey 6 28 | 29 | Monkey 4: 30 | Starting items: 74, 54, 94, 76 31 | Operation: new = old * old 32 | Test: divisible by 7 33 | If true: throw to monkey 0 34 | If false: throw to monkey 3 35 | 36 | Monkey 5: 37 | Starting items: 58 38 | Operation: new = old + 4 39 | Test: divisible by 17 40 | If true: throw to monkey 7 41 | If false: throw to monkey 1 42 | 43 | Monkey 6: 44 | Starting items: 66, 63 45 | Operation: new = old + 6 46 | Test: divisible by 2 47 | If true: throw to monkey 7 48 | If false: throw to monkey 5 49 | 50 | Monkey 7: 51 | Starting items: 56, 56, 90, 96, 68 52 | Operation: new = old + 7 53 | Test: divisible by 3 54 | If true: throw to monkey 4 55 | If false: throw to monkey 1 56 | -------------------------------------------------------------------------------- /2022/day11/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day11/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | Monkey 0: 11 | Starting items: 79, 98 12 | Operation: new = old * 19 13 | Test: divisible by 23 14 | If true: throw to monkey 2 15 | If false: throw to monkey 3 16 | 17 | Monkey 1: 18 | Starting items: 54, 65, 75, 74 19 | Operation: new = old + 6 20 | Test: divisible by 19 21 | If true: throw to monkey 2 22 | If false: throw to monkey 0 23 | 24 | Monkey 2: 25 | Starting items: 79, 60, 97 26 | Operation: new = old * old 27 | Test: divisible by 13 28 | If true: throw to monkey 1 29 | If false: throw to monkey 3 30 | 31 | Monkey 3: 32 | Starting items: 74 33 | Operation: new = old + 3 34 | Test: divisible by 17 35 | If true: throw to monkey 0 36 | If false: throw to monkey 1` 37 | 38 | func TestPartOne(t *testing.T) { 39 | tt := []struct { 40 | name string 41 | input string 42 | expected string 43 | }{ 44 | { 45 | name: "example", 46 | input: sample, 47 | expected: "10605", 48 | }, 49 | } 50 | 51 | for _, tc := range tt { 52 | t.Run(tc.name, func(t *testing.T) { 53 | input := utils.SplitInput(tc.input) 54 | if got := PartOne(input); got != tc.expected { 55 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 56 | } 57 | }) 58 | } 59 | } 60 | 61 | func TestPartTwo(t *testing.T) { 62 | tt := []struct { 63 | name string 64 | input string 65 | expected string 66 | }{ 67 | { 68 | name: "example", 69 | input: sample, 70 | expected: "2713310158", 71 | }, 72 | } 73 | 74 | for _, tc := range tt { 75 | t.Run(tc.name, func(t *testing.T) { 76 | input := utils.SplitInput(tc.input) 77 | if got := PartTwo(input); got != tc.expected { 78 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 79 | } 80 | }) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /2022/day11/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/11](https://github.com/lucianoq/adventofcode/tree/master/2022/11) 3 | 4 | -------------------------------------------------------------------------------- /2022/day11/mastrogiovanni/input.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 73, 77 3 | Operation: new = old * 5 4 | Test: divisible by 11 5 | If true: throw to monkey 6 6 | If false: throw to monkey 5 7 | 8 | Monkey 1: 9 | Starting items: 57, 88, 80 10 | Operation: new = old + 5 11 | Test: divisible by 19 12 | If true: throw to monkey 6 13 | If false: throw to monkey 0 14 | 15 | Monkey 2: 16 | Starting items: 61, 81, 84, 69, 77, 88 17 | Operation: new = old * 19 18 | Test: divisible by 5 19 | If true: throw to monkey 3 20 | If false: throw to monkey 1 21 | 22 | Monkey 3: 23 | Starting items: 78, 89, 71, 60, 81, 84, 87, 75 24 | Operation: new = old + 7 25 | Test: divisible by 3 26 | If true: throw to monkey 1 27 | If false: throw to monkey 0 28 | 29 | Monkey 4: 30 | Starting items: 60, 76, 90, 63, 86, 87, 89 31 | Operation: new = old + 2 32 | Test: divisible by 13 33 | If true: throw to monkey 2 34 | If false: throw to monkey 7 35 | 36 | Monkey 5: 37 | Starting items: 88 38 | Operation: new = old + 1 39 | Test: divisible by 17 40 | If true: throw to monkey 4 41 | If false: throw to monkey 7 42 | 43 | Monkey 6: 44 | Starting items: 84, 98, 78, 85 45 | Operation: new = old * old 46 | Test: divisible by 7 47 | If true: throw to monkey 5 48 | If false: throw to monkey 4 49 | 50 | Monkey 7: 51 | Starting items: 98, 89, 78, 73, 71 52 | Operation: new = old + 4 53 | Test: divisible by 2 54 | If true: throw to monkey 3 55 | If false: throw to monkey 2 -------------------------------------------------------------------------------- /2022/day11/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func parse() []*Monkey { 6 | m0 := &Monkey{ 7 | worry_levels: []int{73, 77}, 8 | operation: func(old int) int { return old * 5 }, 9 | divisor: 11, 10 | monkey_if_true: 6, 11 | monkey_if_false: 5, 12 | } 13 | 14 | m1 := &Monkey{ 15 | worry_levels: []int{57, 88, 80}, 16 | operation: func(old int) int { return old + 5 }, 17 | divisor: 19, 18 | monkey_if_true: 6, 19 | monkey_if_false: 0, 20 | } 21 | 22 | m2 := &Monkey{ 23 | worry_levels: []int{61, 81, 84, 69, 77, 88}, 24 | operation: func(old int) int { return old * 19 }, 25 | divisor: 5, 26 | monkey_if_true: 3, 27 | monkey_if_false: 1, 28 | } 29 | 30 | m3 := &Monkey{ 31 | worry_levels: []int{78, 89, 71, 60, 81, 84, 87, 75}, 32 | operation: func(old int) int { return old + 7 }, 33 | divisor: 3, 34 | monkey_if_true: 1, 35 | monkey_if_false: 0, 36 | } 37 | 38 | m4 := &Monkey{ 39 | worry_levels: []int{60, 76, 90, 63, 86, 87, 89}, 40 | operation: func(old int) int { return old + 2 }, 41 | divisor: 13, 42 | monkey_if_true: 2, 43 | monkey_if_false: 7, 44 | } 45 | 46 | m5 := &Monkey{ 47 | worry_levels: []int{88}, 48 | operation: func(old int) int { return old + 1 }, 49 | divisor: 17, 50 | monkey_if_true: 4, 51 | monkey_if_false: 7, 52 | } 53 | 54 | m6 := &Monkey{ 55 | worry_levels: []int{84, 98, 78, 85}, 56 | operation: func(old int) int { return old * old }, 57 | divisor: 7, 58 | monkey_if_true: 5, 59 | monkey_if_false: 4, 60 | } 61 | 62 | m7 := &Monkey{ 63 | worry_levels: []int{98, 89, 78, 73, 71}, 64 | operation: func(old int) int { return old + 4 }, 65 | divisor: 2, 66 | monkey_if_true: 3, 67 | monkey_if_false: 2, 68 | } 69 | 70 | return []*Monkey{m0, m1, m2, m3, m4, m5, m6, m7} 71 | } 72 | 73 | func main() { 74 | monkeys := parse() 75 | fmt.Printf("Part 1: %v\n", PartOne(monkeys)) 76 | 77 | monkeys = parse() 78 | fmt.Printf("Part 1: %v\n", PartTwo(monkeys)) 79 | } 80 | -------------------------------------------------------------------------------- /2022/day11/mastrogiovanni/monkey.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | type MonkeyOperation func(old int) int 9 | 10 | type Monkey struct { 11 | worry_levels []int 12 | operation MonkeyOperation 13 | divisor int 14 | monkey_if_true int 15 | monkey_if_false int 16 | inspection int 17 | } 18 | 19 | func DumpMonkeys(monkeys []*Monkey) { 20 | for i := 0; i < len(monkeys); i++ { 21 | fmt.Printf("Monkey %d {insp. %d}: %+v\n", i, monkeys[i].inspection, monkeys[i].worry_levels) 22 | } 23 | } 24 | 25 | func MonkeyProcessor(monkeys []*Monkey) { 26 | for index := 0; index < len(monkeys); index++ { 27 | current := monkeys[index] 28 | for _, wl := range current.worry_levels { 29 | newWl := current.operation(wl) 30 | newWl = newWl / 3 31 | if newWl%current.divisor == 0 { 32 | monkeys[current.monkey_if_true].worry_levels = append(monkeys[current.monkey_if_true].worry_levels, newWl) 33 | } else { 34 | monkeys[current.monkey_if_false].worry_levels = append(monkeys[current.monkey_if_false].worry_levels, newWl) 35 | } 36 | current.inspection++ 37 | } 38 | current.worry_levels = []int{} 39 | } 40 | } 41 | 42 | func MonkeyProcessor2(big_factor int, monkeys []*Monkey) { 43 | for index := 0; index < len(monkeys); index++ { 44 | current := monkeys[index] 45 | for _, wl := range current.worry_levels { 46 | current.inspection++ 47 | newWl := current.operation(wl) 48 | newWl = newWl % big_factor 49 | if newWl%current.divisor == 0 { 50 | monkeys[current.monkey_if_true].worry_levels = append(monkeys[current.monkey_if_true].worry_levels, newWl) 51 | } else { 52 | monkeys[current.monkey_if_false].worry_levels = append(monkeys[current.monkey_if_false].worry_levels, newWl) 53 | } 54 | } 55 | current.worry_levels = []int{} 56 | } 57 | } 58 | 59 | func GetMonkeyBusiness(monkeys []*Monkey) int { 60 | res := make([]int, len(monkeys)) 61 | for i, m := range monkeys { 62 | res[i] = m.inspection 63 | } 64 | sort.Sort(sort.Reverse(sort.IntSlice(res))) 65 | return res[0] * res[1] 66 | } 67 | -------------------------------------------------------------------------------- /2022/day11/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func PartOne(monkeys []*Monkey) string { 8 | for i := 0; i < 20; i++ { 9 | MonkeyProcessor(monkeys) 10 | } 11 | return fmt.Sprintf("%d", GetMonkeyBusiness(monkeys)) 12 | } 13 | 14 | func PartTwo(monkeys []*Monkey) string { 15 | big_factor := 1 16 | for index := 0; index < len(monkeys); index++ { 17 | big_factor *= monkeys[index].divisor 18 | } 19 | for i := 0; i < 10000; i++ { 20 | MonkeyProcessor2(big_factor, monkeys) 21 | } 22 | return fmt.Sprintf("%d", GetMonkeyBusiness(monkeys)) 23 | } 24 | -------------------------------------------------------------------------------- /2022/day11/misterdelle/input.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 63, 57 3 | Operation: new = old * 11 4 | Test: divisible by 7 5 | If true: throw to monkey 6 6 | If false: throw to monkey 2 7 | 8 | Monkey 1: 9 | Starting items: 82, 66, 87, 78, 77, 92, 83 10 | Operation: new = old + 1 11 | Test: divisible by 11 12 | If true: throw to monkey 5 13 | If false: throw to monkey 0 14 | 15 | Monkey 2: 16 | Starting items: 97, 53, 53, 85, 58, 54 17 | Operation: new = old * 7 18 | Test: divisible by 13 19 | If true: throw to monkey 4 20 | If false: throw to monkey 3 21 | 22 | Monkey 3: 23 | Starting items: 50 24 | Operation: new = old + 3 25 | Test: divisible by 3 26 | If true: throw to monkey 1 27 | If false: throw to monkey 7 28 | 29 | Monkey 4: 30 | Starting items: 64, 69, 52, 65, 73 31 | Operation: new = old + 6 32 | Test: divisible by 17 33 | If true: throw to monkey 3 34 | If false: throw to monkey 7 35 | 36 | Monkey 5: 37 | Starting items: 57, 91, 65 38 | Operation: new = old + 5 39 | Test: divisible by 2 40 | If true: throw to monkey 0 41 | If false: throw to monkey 6 42 | 43 | Monkey 6: 44 | Starting items: 67, 91, 84, 78, 60, 69, 99, 83 45 | Operation: new = old * old 46 | Test: divisible by 5 47 | If true: throw to monkey 2 48 | If false: throw to monkey 4 49 | 50 | Monkey 7: 51 | Starting items: 58, 78, 69, 65 52 | Operation: new = old + 7 53 | Test: divisible by 19 54 | If true: throw to monkey 5 55 | If false: throw to monkey 1 56 | -------------------------------------------------------------------------------- /2022/day11/misterdelle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/p_queue.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type priorityQueue []*node 4 | 5 | func (pq *priorityQueue) Len() int { 6 | return len(*pq) 7 | } 8 | 9 | func (pq *priorityQueue) Less(i, j int) bool { 10 | return (*pq)[i].rank < (*pq)[j].rank 11 | } 12 | 13 | func (pq *priorityQueue) Swap(i, j int) { 14 | (*pq)[i], (*pq)[j] = (*pq)[j], (*pq)[i] 15 | (*pq)[i].index = i 16 | (*pq)[j].index = j 17 | } 18 | 19 | func (pq *priorityQueue) Push(x interface{}) { 20 | n := len(*pq) 21 | no := x.(*node) 22 | no.index = n 23 | *pq = append(*pq, no) 24 | } 25 | 26 | func (pq *priorityQueue) Pop() interface{} { 27 | old := *pq 28 | n := len(old) 29 | no := old[n-1] 30 | no.index = -1 31 | *pq = old[0 : n-1] 32 | return no 33 | } 34 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func PartOne(input []string) string { 8 | w := ParseWorld(input) 9 | 10 | p, _, found := Path(w.From(), w.To()) 11 | 12 | if !found { 13 | return "no path found" 14 | } 15 | 16 | return strconv.Itoa(len(p) - 1) 17 | } 18 | 19 | func PartTwo(input []string) string { 20 | result := 1<<63 - 1 21 | 22 | w := ParseWorld(input) 23 | 24 | for y, row := range input { 25 | for x, raw := range row { 26 | if raw == 'a' || raw == 'S' { 27 | p, _, found := Path(w.Tile(x, y), w.To()) 28 | 29 | if !found { 30 | continue 31 | } 32 | 33 | if len(p)-1 < result { 34 | result = len(p) - 1 35 | } 36 | } 37 | } 38 | } 39 | 40 | return strconv.Itoa(result) 41 | } 42 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | Sabqponm 11 | abcryxxl 12 | accszExk 13 | acctuvwj 14 | abdefghi` 15 | 16 | func TestPartOne(t *testing.T) { 17 | tt := []struct { 18 | name string 19 | input string 20 | expected string 21 | }{ 22 | { 23 | name: "example", 24 | input: sample, 25 | expected: "31", 26 | }, 27 | } 28 | 29 | for _, tc := range tt { 30 | t.Run(tc.name, func(t *testing.T) { 31 | input := utils.SplitInput(tc.input) 32 | if got := PartOne(input); got != tc.expected { 33 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 34 | } 35 | }) 36 | } 37 | } 38 | 39 | func TestPartTwo(t *testing.T) { 40 | tt := []struct { 41 | name string 42 | input string 43 | expected string 44 | }{ 45 | { 46 | name: "example", 47 | input: sample, 48 | expected: "29", 49 | }, 50 | } 51 | 52 | for _, tc := range tt { 53 | t.Run(tc.name, func(t *testing.T) { 54 | input := utils.SplitInput(tc.input) 55 | if got := PartTwo(input); got != tc.expected { 56 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 57 | } 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/short_path.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "container/heap" 4 | 5 | type node struct { 6 | tile *Tile 7 | cost float64 8 | rank float64 9 | parent *node 10 | open bool 11 | closed bool 12 | index int 13 | } 14 | 15 | type nodeMap map[*Tile]*node 16 | 17 | func (nm nodeMap) get(p *Tile) *node { 18 | n, ok := nm[p] 19 | if !ok { 20 | n = &node{ 21 | tile: p, 22 | } 23 | nm[p] = n 24 | } 25 | return n 26 | } 27 | 28 | func Path(from, to *Tile) (path []*Tile, distance float64, found bool) { 29 | nm := nodeMap{} 30 | nq := &priorityQueue{} 31 | heap.Init(nq) 32 | fromNode := nm.get(from) 33 | fromNode.open = true 34 | heap.Push(nq, fromNode) 35 | for { 36 | if nq.Len() == 0 { 37 | return 38 | } 39 | current := heap.Pop(nq).(*node) 40 | current.open = false 41 | current.closed = true 42 | 43 | if current == nm.get(to) { 44 | var p []*Tile 45 | curr := current 46 | for curr != nil { 47 | p = append(p, curr.tile) 48 | curr = curr.parent 49 | } 50 | return p, current.cost, true 51 | } 52 | 53 | for _, neighbor := range current.tile.PathNeighbors() { 54 | cost := current.cost + 1.0 55 | neighborNode := nm.get(neighbor) 56 | if cost < neighborNode.cost { 57 | if neighborNode.open { 58 | heap.Remove(nq, neighborNode.index) 59 | } 60 | neighborNode.open = false 61 | neighborNode.closed = false 62 | } 63 | if !neighborNode.open && !neighborNode.closed { 64 | neighborNode.cost = cost 65 | neighborNode.open = true 66 | neighborNode.rank = cost + neighbor.PathEstimatedCost(to) 67 | neighborNode.parent = current 68 | heap.Push(nq, neighborNode) 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/tile.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type Tile struct { 4 | Kind int 5 | X, Y int 6 | W World 7 | } 8 | 9 | func (t *Tile) PathNeighbors() []*Tile { 10 | var neighbors []*Tile 11 | 12 | for _, offset := range [][]int{ 13 | {-1, 0}, 14 | {1, 0}, 15 | {0, -1}, 16 | {0, 1}, 17 | } { 18 | if n := t.W.Tile(t.X+offset[0], t.Y+offset[1]); n != nil { 19 | current := t.Kind 20 | next := n.Kind 21 | 22 | if current == 'S' { 23 | current = 'a' 24 | } 25 | 26 | if next == 'E' { 27 | next = 'z' 28 | } 29 | 30 | if next-current > 1 { 31 | continue 32 | } 33 | 34 | neighbors = append(neighbors, n) 35 | } 36 | } 37 | 38 | return neighbors 39 | } 40 | 41 | func (t *Tile) PathEstimatedCost(toT *Tile) float64 { 42 | absX := toT.X - t.X 43 | if absX < 0 { 44 | absX = -absX 45 | } 46 | absY := toT.Y - t.Y 47 | if absY < 0 { 48 | absY = -absY 49 | } 50 | return float64(absX + absY) 51 | } 52 | -------------------------------------------------------------------------------- /2022/day12/Al-Pragliola/world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type World map[int]map[int]*Tile 4 | 5 | func (w World) Tile(x, y int) *Tile { 6 | if w[x] == nil { 7 | return nil 8 | } 9 | return w[x][y] 10 | } 11 | 12 | func (w World) SetTile(t *Tile, x, y int) { 13 | if w[x] == nil { 14 | w[x] = map[int]*Tile{} 15 | } 16 | w[x][y] = t 17 | t.X = x 18 | t.Y = y 19 | t.W = w 20 | } 21 | 22 | func (w World) Find(kind int) *Tile { 23 | for _, row := range w { 24 | for _, t := range row { 25 | if t.Kind == kind { 26 | return t 27 | } 28 | } 29 | } 30 | return nil 31 | } 32 | 33 | func (w World) From() *Tile { 34 | return w.Find('S') 35 | } 36 | 37 | func (w World) To() *Tile { 38 | return w.Find('E') 39 | } 40 | 41 | func ParseWorld(input []string) World { 42 | w := World{} 43 | 44 | for y, row := range input { 45 | for x, raw := range row { 46 | w.SetTile(&Tile{ 47 | Kind: int(raw), 48 | }, x, y) 49 | } 50 | } 51 | 52 | return w 53 | } 54 | -------------------------------------------------------------------------------- /2022/day12/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/12](https://github.com/lucianoq/adventofcode/tree/master/2022/12) 3 | 4 | -------------------------------------------------------------------------------- /2022/day12/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day12/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `Sabqponm 10 | abcryxxl 11 | accszExk 12 | acctuvwj 13 | abdefghi` 14 | 15 | func TestPartOne(t *testing.T) { 16 | tt := []struct { 17 | name string 18 | input string 19 | expected string 20 | }{ 21 | { 22 | name: "example", 23 | input: sample, 24 | expected: "31", 25 | }, 26 | } 27 | 28 | for _, tc := range tt { 29 | t.Run(tc.name, func(t *testing.T) { 30 | input := utils.SplitInput(tc.input) 31 | if got := PartOne(input); got != tc.expected { 32 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 33 | } 34 | }) 35 | } 36 | } 37 | 38 | func TestPartTwo(t *testing.T) { 39 | tt := []struct { 40 | name string 41 | input string 42 | expected string 43 | }{ 44 | { 45 | name: "example", 46 | input: sample, 47 | expected: "29", 48 | }, 49 | } 50 | 51 | for _, tc := range tt { 52 | t.Run(tc.name, func(t *testing.T) { 53 | input := utils.SplitInput(tc.input) 54 | if got := PartTwo(input); got != tc.expected { 55 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 56 | } 57 | }) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /2022/day13/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day13/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/13](https://github.com/lucianoq/adventofcode/tree/master/2022/13) 3 | 4 | -------------------------------------------------------------------------------- /2022/day13/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day13/mastrogiovanni/parse_old2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func MakeArray(token []Token) []Token { 6 | return []Token{ 7 | {Type: Open}, 8 | token[0], 9 | {Type: Close}} 10 | } 11 | 12 | func CompareRecursive(left, right [][]Token) int { 13 | fmt.Printf("A: %+v\n", left) 14 | fmt.Printf("B: %+v\n", right) 15 | if len(left) > len(right) { 16 | fmt.Printf("left size %d > right size %d\n", len(left), len(right)) 17 | return +1 18 | } 19 | if len(left) == 0 { 20 | fmt.Println("Compare Y") 21 | if len(left) == len(right) { 22 | return 0 23 | } 24 | return -1 25 | } 26 | for i := 0; i < len(left); i++ { 27 | fmt.Printf("%d/%d\n", i, len(left)) 28 | fmt.Printf("Compare A: %+v (%d)\n", left[i], len(left[i])) 29 | fmt.Printf("Compare B: %+v (%d)\n", right[i], len(right[i])) 30 | if IsArray(left[i]) { 31 | if IsArray(right[i]) { 32 | l := Parse(left[i]) 33 | r := Parse(right[i]) 34 | fmt.Println("Compare 1", l, r) 35 | res := CompareRecursive(l, r) 36 | if res != 0 { 37 | return res 38 | } 39 | } else { 40 | l := Parse(left[i]) 41 | r := Parse(MakeArray(right[i])) 42 | fmt.Println("Compare 2", l, r) 43 | res := CompareRecursive(l, r) 44 | if res != 0 { 45 | return res 46 | } 47 | } 48 | } else { 49 | if IsArray(right[i]) { 50 | l := Parse(MakeArray(left[i])) 51 | r := Parse(right[i]) 52 | fmt.Println("Compare 3", l, r) 53 | res := CompareRecursive(l, r) 54 | if res != 0 { 55 | return res 56 | } 57 | } else { 58 | l := left[i][0].Value 59 | r := right[i][0].Value 60 | fmt.Println("Compare 4", l, r) 61 | if l < r { 62 | return -1 63 | } else if l > r { 64 | return 1 65 | } 66 | } 67 | } 68 | } 69 | 70 | return -1 71 | } 72 | 73 | func Compare2(left, right string) bool { 74 | tokensLeft := Parse(Reverse(GetTokens(left))) 75 | tokensRight := Parse(Reverse(GetTokens(right))) 76 | return CompareRecursive(tokensLeft, tokensRight) < 0 77 | } 78 | -------------------------------------------------------------------------------- /2022/day13/mastrogiovanni/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | func PartOne(inputs []string) string { 9 | tot := 0 10 | index := 1 11 | for i := 0; i < len(inputs); i += 3 { 12 | left := inputs[i] 13 | right := inputs[i+1] 14 | if res := Compare3(left, right); res { 15 | tot += index 16 | } 17 | index++ 18 | } 19 | return fmt.Sprintf("%d", tot) 20 | } 21 | 22 | func PartTwo(inputs []string) string { 23 | list := make([]any, 0) 24 | for i := 0; i < len(inputs); i += 3 { 25 | left := parse(inputs[i]) 26 | right := parse(inputs[i+1]) 27 | list = append(list, left, right) 28 | } 29 | list = append(list, parse("[[2]]"), parse("[[6]]")) 30 | sort.Slice(list, func(i, j int) bool { 31 | return compare(list[i], list[j]) < 0 32 | }) 33 | tot := 1 34 | for i := 0; i < len(list); i++ { 35 | if fmt.Sprintf("%+v", list[i]) == "[[2]]" { 36 | tot *= (i + 1) 37 | } 38 | if fmt.Sprintf("%+v", list[i]) == "[[6]]" { 39 | tot *= (i + 1) 40 | } 41 | } 42 | return fmt.Sprintf("%d", tot) 43 | } 44 | -------------------------------------------------------------------------------- /2022/day13/mastrogiovanni/parts_new.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "encoding/json" 4 | 5 | func parse(line string) []any { 6 | ll := []any{} 7 | _ = json.Unmarshal([]byte(line), &ll) 8 | return ll 9 | } 10 | 11 | func intCompare(left, right int) int { 12 | if left < right { 13 | return -1 14 | } 15 | if left > right { 16 | return 1 17 | } 18 | return 0 19 | } 20 | 21 | func min(left, right int) int { 22 | if left <= right { 23 | return left 24 | } 25 | return right 26 | } 27 | 28 | func compare(left any, right any) int { 29 | lInt, lList, lIsInt := typeOf(left) 30 | rInt, rList, rIsInt := typeOf(right) 31 | 32 | // both integer 33 | if lIsInt && rIsInt { 34 | return intCompare(lInt, rInt) 35 | } 36 | 37 | // both lists 38 | if !lIsInt && !rIsInt { 39 | lLen, rLen := len(lList), len(rList) 40 | for i := 0; i < min(lLen, rLen); i++ { 41 | // return if they're different 42 | if cmp := compare(lList[i], rList[i]); cmp != 0 { 43 | return cmp 44 | } 45 | // continue if they're equal 46 | } 47 | 48 | // when a list is a prefix of another 49 | // the shortest comes first 50 | return intCompare(lLen, rLen) 51 | } 52 | 53 | if lIsInt { 54 | return compare([]any{lInt}, right) 55 | } 56 | 57 | if rIsInt { 58 | return compare(left, []any{rInt}) 59 | } 60 | 61 | panic("wrong types") 62 | } 63 | 64 | func typeOf(x any) (int, []any, bool) { 65 | switch val := x.(type) { 66 | case int: 67 | return val, nil, true 68 | case float64: 69 | return int(val), nil, true 70 | case []any: 71 | return 0, val, false 72 | } 73 | panic("wrong type") 74 | } 75 | 76 | func Compare3(left, right string) bool { 77 | tokensLeft := parse(left) 78 | tokensRight := parse(right) 79 | return compare(tokensLeft, tokensRight) < 0 80 | } 81 | -------------------------------------------------------------------------------- /2022/day13/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `[1,1,3,1,1] 10 | [1,1,5,1,1] 11 | 12 | [[1],[2,3,4]] 13 | [[1],4] 14 | 15 | [9] 16 | [[8,7,6]] 17 | 18 | [[4,4],4,4] 19 | [[4,4],4,4,4] 20 | 21 | [7,7,7,7] 22 | [7,7,7] 23 | 24 | [] 25 | [3] 26 | 27 | [[[]]] 28 | [[]] 29 | 30 | [1,[2,[3,[4,[5,6,7]]]],8,9] 31 | [1,[2,[3,[4,[5,6,0]]]],8,9]` 32 | 33 | func TestPartOne(t *testing.T) { 34 | tt := []struct { 35 | name string 36 | input string 37 | expected string 38 | }{ 39 | { 40 | name: "example", 41 | input: sample, 42 | expected: "13", 43 | }, 44 | } 45 | 46 | for _, tc := range tt { 47 | t.Run(tc.name, func(t *testing.T) { 48 | input := utils.SplitInput(tc.input) 49 | if got := PartOne(input); got != tc.expected { 50 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 51 | } 52 | }) 53 | } 54 | } 55 | 56 | func TestPartTwo(t *testing.T) { 57 | tt := []struct { 58 | name string 59 | input string 60 | expected string 61 | }{ 62 | { 63 | name: "example", 64 | input: sample, 65 | expected: "140", 66 | }, 67 | } 68 | 69 | for _, tc := range tt { 70 | t.Run(tc.name, func(t *testing.T) { 71 | input := utils.SplitInput(tc.input) 72 | if got := PartTwo(input); got != tc.expected { 73 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 74 | } 75 | }) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /2022/day14/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day14/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | 498,4 -> 498,6 -> 496,6 11 | 503,4 -> 502,4 -> 502,9 -> 494,9` 12 | 13 | func TestPartOne(t *testing.T) { 14 | tt := []struct { 15 | name string 16 | input string 17 | expected string 18 | }{ 19 | { 20 | name: "example", 21 | input: sample, 22 | expected: "24", 23 | }, 24 | } 25 | 26 | for _, tc := range tt { 27 | t.Run(tc.name, func(t *testing.T) { 28 | input := utils.SplitInput(tc.input) 29 | if got := PartOne(input); got != tc.expected { 30 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 31 | } 32 | }) 33 | } 34 | } 35 | 36 | func TestPartTwo(t *testing.T) { 37 | tt := []struct { 38 | name string 39 | input string 40 | expected string 41 | }{ 42 | { 43 | name: "example", 44 | input: sample, 45 | expected: "93", 46 | }, 47 | } 48 | 49 | for _, tc := range tt { 50 | t.Run(tc.name, func(t *testing.T) { 51 | input := utils.SplitInput(tc.input) 52 | if got := PartTwo(input); got != tc.expected { 53 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 54 | } 55 | }) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /2022/day14/alessio-perugini/go.mod: -------------------------------------------------------------------------------- 1 | module day14 2 | 3 | go 1.19 4 | 5 | require github.com/golangroma/aoc v0.0.0-20221214003614-9240e51c1554 6 | -------------------------------------------------------------------------------- /2022/day14/alessio-perugini/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golangroma/aoc v0.0.0-20221214003614-9240e51c1554 h1:K5l4cIgZ8WVEPyxVeG5zMWera9BbKdpXe3+T+LSnsvg= 2 | github.com/golangroma/aoc v0.0.0-20221214003614-9240e51c1554/go.mod h1:w+hmxaKu/BTGBydQ9A0DfdM/iYMFbYLkMtV79VCxVFM= 3 | -------------------------------------------------------------------------------- /2022/day14/alessio-perugini/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day14/alessio-perugini/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `498,4 -> 498,6 -> 496,6 10 | 503,4 -> 502,4 -> 502,9 -> 494,9` 11 | 12 | func TestPartOne(t *testing.T) { 13 | tt := []struct { 14 | name string 15 | input string 16 | expected string 17 | }{ 18 | { 19 | name: "example", 20 | input: sample, 21 | expected: "24", 22 | }, 23 | } 24 | 25 | for _, tc := range tt { 26 | t.Run(tc.name, func(t *testing.T) { 27 | input := utils.SplitInput(tc.input) 28 | if got := PartOne(input); got != tc.expected { 29 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 30 | } 31 | }) 32 | } 33 | } 34 | 35 | func TestPartTwo(t *testing.T) { 36 | tt := []struct { 37 | name string 38 | input string 39 | expected string 40 | }{ 41 | { 42 | name: "example", 43 | input: sample, 44 | expected: "93", 45 | }, 46 | } 47 | 48 | for _, tc := range tt { 49 | t.Run(tc.name, func(t *testing.T) { 50 | input := utils.SplitInput(tc.input) 51 | if got := PartTwo(input); got != tc.expected { 52 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 53 | } 54 | }) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /2022/day14/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/14](https://github.com/lucianoq/adventofcode/tree/master/2022/14) 3 | 4 | -------------------------------------------------------------------------------- /2022/day14/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day14/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `498,4 -> 498,6 -> 496,6 10 | 503,4 -> 502,4 -> 502,9 -> 494,9` 11 | 12 | func TestPartOne(t *testing.T) { 13 | tt := []struct { 14 | name string 15 | input string 16 | expected string 17 | }{ 18 | { 19 | name: "example", 20 | input: sample, 21 | expected: "24", 22 | }, 23 | } 24 | 25 | for _, tc := range tt { 26 | t.Run(tc.name, func(t *testing.T) { 27 | input := utils.SplitInput(tc.input) 28 | if got := PartOne(input); got != tc.expected { 29 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 30 | } 31 | }) 32 | } 33 | } 34 | 35 | func TestPartTwo(t *testing.T) { 36 | tt := []struct { 37 | name string 38 | input string 39 | expected string 40 | }{ 41 | { 42 | name: "example", 43 | input: sample, 44 | expected: "93", 45 | }, 46 | } 47 | 48 | for _, tc := range tt { 49 | t.Run(tc.name, func(t *testing.T) { 50 | input := utils.SplitInput(tc.input) 51 | if got := PartTwo(input); got != tc.expected { 52 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 53 | } 54 | }) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /2022/day15/Al-Pragliola/input.txt: -------------------------------------------------------------------------------- 1 | Sensor at x=2208586, y=2744871: closest beacon is at x=2094814, y=3380585 2 | Sensor at x=3937279, y=2452476: closest beacon is at x=3597034, y=2313095 3 | Sensor at x=3535638, y=3151860: closest beacon is at x=4098735, y=3373876 4 | Sensor at x=1867584, y=2125870: closest beacon is at x=2685670, y=2236965 5 | Sensor at x=2290971, y=1583182: closest beacon is at x=2685670, y=2236965 6 | Sensor at x=3137806, y=2216828: closest beacon is at x=3233556, y=2000000 7 | Sensor at x=3393352, y=331000: closest beacon is at x=3233556, y=2000000 8 | Sensor at x=1444302, y=821689: closest beacon is at x=1683873, y=-199301 9 | Sensor at x=1084667, y=3412239: closest beacon is at x=2094814, y=3380585 10 | Sensor at x=439341, y=3916996: closest beacon is at x=-290982, y=4102300 11 | Sensor at x=295460, y=2114590: closest beacon is at x=362644, y=370187 12 | Sensor at x=2212046, y=3819484: closest beacon is at x=2094814, y=3380585 13 | Sensor at x=3413280, y=3862465: closest beacon is at x=4098735, y=3373876 14 | Sensor at x=3744934, y=1572303: closest beacon is at x=3597034, y=2313095 15 | Sensor at x=3349047, y=2522469: closest beacon is at x=3597034, y=2313095 16 | Sensor at x=171415, y=591241: closest beacon is at x=362644, y=370187 17 | Sensor at x=3237499, y=2150414: closest beacon is at x=3233556, y=2000000 18 | Sensor at x=559077, y=454593: closest beacon is at x=362644, y=370187 19 | Sensor at x=3030733, y=2047512: closest beacon is at x=3233556, y=2000000 20 | Sensor at x=1667358, y=3956837: closest beacon is at x=2094814, y=3380585 21 | Sensor at x=1850337, y=98963: closest beacon is at x=1683873, y=-199301 22 | Sensor at x=2699546, y=3157824: closest beacon is at x=2094814, y=3380585 23 | Sensor at x=1113195, y=98130: closest beacon is at x=1683873, y=-199301 24 | Sensor at x=59337, y=246804: closest beacon is at x=362644, y=370187 25 | Sensor at x=566043, y=29068: closest beacon is at x=362644, y=370187 26 | Sensor at x=2831421, y=2581088: closest beacon is at x=2685670, y=2236965 27 | Sensor at x=597818, y=749461: closest beacon is at x=362644, y=370187 28 | -------------------------------------------------------------------------------- /2022/day15/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day15/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/15](https://github.com/lucianoq/adventofcode/tree/master/2022/15) 3 | 4 | -------------------------------------------------------------------------------- /2022/day15/mastrogiovanni/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input, 2000000)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input, 4000000)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day15/mastrogiovanni/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `Sensor at x=2, y=18: closest beacon is at x=-2, y=15 10 | Sensor at x=9, y=16: closest beacon is at x=10, y=16 11 | Sensor at x=13, y=2: closest beacon is at x=15, y=3 12 | Sensor at x=12, y=14: closest beacon is at x=10, y=16 13 | Sensor at x=10, y=20: closest beacon is at x=10, y=16 14 | Sensor at x=14, y=17: closest beacon is at x=10, y=16 15 | Sensor at x=8, y=7: closest beacon is at x=2, y=10 16 | Sensor at x=2, y=0: closest beacon is at x=2, y=10 17 | Sensor at x=0, y=11: closest beacon is at x=2, y=10 18 | Sensor at x=20, y=14: closest beacon is at x=25, y=17 19 | Sensor at x=17, y=20: closest beacon is at x=21, y=22 20 | Sensor at x=16, y=7: closest beacon is at x=15, y=3 21 | Sensor at x=14, y=3: closest beacon is at x=15, y=3 22 | Sensor at x=20, y=1: closest beacon is at x=15, y=3` 23 | 24 | func TestPartOne(t *testing.T) { 25 | tt := []struct { 26 | name string 27 | input string 28 | expected string 29 | }{ 30 | { 31 | name: "example", 32 | input: sample, 33 | expected: "26", 34 | }, 35 | } 36 | 37 | for _, tc := range tt { 38 | t.Run(tc.name, func(t *testing.T) { 39 | input := utils.SplitInput(tc.input) 40 | if got := PartOne(input, 10); got != tc.expected { 41 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 42 | } 43 | }) 44 | } 45 | } 46 | 47 | func TestPartTwo(t *testing.T) { 48 | tt := []struct { 49 | name string 50 | input string 51 | expected string 52 | }{ 53 | { 54 | name: "example", 55 | input: sample, 56 | expected: "56000011", 57 | }, 58 | } 59 | 60 | for _, tc := range tt { 61 | t.Run(tc.name, func(t *testing.T) { 62 | input := utils.SplitInput(tc.input) 63 | if got := PartTwo(input, 20); got != tc.expected { 64 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 65 | } 66 | }) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /2022/day16/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day16/Al-Pragliola/p_queue.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type priorityQueue []*node 4 | 5 | func (pq *priorityQueue) Len() int { 6 | return len(*pq) 7 | } 8 | 9 | func (pq *priorityQueue) Less(i, j int) bool { 10 | return (*pq)[i].rank < (*pq)[j].rank 11 | } 12 | 13 | func (pq *priorityQueue) Swap(i, j int) { 14 | (*pq)[i], (*pq)[j] = (*pq)[j], (*pq)[i] 15 | (*pq)[i].index = i 16 | (*pq)[j].index = j 17 | } 18 | 19 | func (pq *priorityQueue) Push(x interface{}) { 20 | n := len(*pq) 21 | no := x.(*node) 22 | no.index = n 23 | *pq = append(*pq, no) 24 | } 25 | 26 | func (pq *priorityQueue) Pop() interface{} { 27 | old := *pq 28 | n := len(old) 29 | no := old[n-1] 30 | no.index = -1 31 | *pq = old[0 : n-1] 32 | return no 33 | } 34 | -------------------------------------------------------------------------------- /2022/day16/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = ` 10 | Valve AA has flow rate=0; tunnels lead to valves DD, II, BB 11 | Valve BB has flow rate=13; tunnels lead to valves CC, AA 12 | Valve CC has flow rate=2; tunnels lead to valves DD, BB 13 | Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE 14 | Valve EE has flow rate=3; tunnels lead to valves FF, DD 15 | Valve FF has flow rate=0; tunnels lead to valves EE, GG 16 | Valve GG has flow rate=0; tunnels lead to valves FF, HH 17 | Valve HH has flow rate=22; tunnel leads to valve GG 18 | Valve II has flow rate=0; tunnels lead to valves AA, JJ 19 | Valve JJ has flow rate=21; tunnel leads to valve II` 20 | 21 | func TestPartOne(t *testing.T) { 22 | tt := []struct { 23 | name string 24 | input string 25 | expected string 26 | }{ 27 | { 28 | name: "example", 29 | input: sample, 30 | expected: "1651", 31 | }, 32 | } 33 | 34 | for _, tc := range tt { 35 | t.Run(tc.name, func(t *testing.T) { 36 | input := utils.SplitInput(tc.input) 37 | if got := PartOne(input); got != tc.expected { 38 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 39 | } 40 | }) 41 | } 42 | } 43 | 44 | func TestPartTwo(t *testing.T) { 45 | tt := []struct { 46 | name string 47 | input string 48 | expected string 49 | }{ 50 | { 51 | name: "example", 52 | input: sample, 53 | expected: "1707", 54 | }, 55 | } 56 | 57 | for _, tc := range tt { 58 | t.Run(tc.name, func(t *testing.T) { 59 | input := utils.SplitInput(tc.input) 60 | if got := PartTwo(input); got != tc.expected { 61 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 62 | } 63 | }) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /2022/day16/Al-Pragliola/short_path.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "container/heap" 4 | 5 | type Tile struct { 6 | Name string 7 | } 8 | 9 | func (t *Tile) PathNeighbors(w map[string][]*Tile) []*Tile { 10 | return w[t.Name] 11 | } 12 | 13 | func (t *Tile) PathEstimatedCost(to *Tile) float64 { 14 | return 1.0 15 | } 16 | 17 | type node struct { 18 | tile *Tile 19 | cost float64 20 | rank float64 21 | parent *node 22 | open bool 23 | closed bool 24 | index int 25 | } 26 | 27 | type nodeMap map[string]*node 28 | 29 | func (nm nodeMap) get(p *Tile) *node { 30 | n, ok := nm[p.Name] 31 | if !ok { 32 | n = &node{ 33 | tile: p, 34 | } 35 | nm[p.Name] = n 36 | } 37 | return n 38 | } 39 | 40 | func Path(from, to *Tile, world map[string][]*Tile) (path []*Tile, distance float64, found bool) { 41 | nm := nodeMap{} 42 | nq := &priorityQueue{} 43 | heap.Init(nq) 44 | fromNode := nm.get(from) 45 | fromNode.open = true 46 | heap.Push(nq, fromNode) 47 | for { 48 | if nq.Len() == 0 { 49 | return 50 | } 51 | current := heap.Pop(nq).(*node) 52 | current.open = false 53 | current.closed = true 54 | 55 | if current == nm.get(to) { 56 | var p []*Tile 57 | curr := current 58 | for curr != nil { 59 | p = append(p, curr.tile) 60 | curr = curr.parent 61 | } 62 | return p, current.cost, true 63 | } 64 | 65 | for _, neighbor := range current.tile.PathNeighbors(world) { 66 | cost := current.cost + 1.0 67 | neighborNode := nm.get(neighbor) 68 | if cost < neighborNode.cost { 69 | if neighborNode.open { 70 | heap.Remove(nq, neighborNode.index) 71 | } 72 | neighborNode.open = false 73 | neighborNode.closed = false 74 | } 75 | if !neighborNode.open && !neighborNode.closed { 76 | neighborNode.cost = cost 77 | neighborNode.open = true 78 | neighborNode.rank = cost + neighbor.PathEstimatedCost(to) 79 | neighborNode.parent = current 80 | heap.Push(nq, neighborNode) 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /2022/day16/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/16](https://github.com/lucianoq/adventofcode/tree/master/2022/16) 3 | 4 | -------------------------------------------------------------------------------- /2022/day17/Al-Pragliola/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2022/day17/Al-Pragliola/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>` 10 | 11 | func TestPartOne(t *testing.T) { 12 | tt := []struct { 13 | name string 14 | input string 15 | expected string 16 | }{ 17 | { 18 | name: "example", 19 | input: sample, 20 | expected: "3068", 21 | }, 22 | } 23 | 24 | for _, tc := range tt { 25 | t.Run(tc.name, func(t *testing.T) { 26 | input := utils.SplitInput(tc.input) 27 | if got := PartOne(input); got != tc.expected { 28 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 29 | } 30 | }) 31 | } 32 | } 33 | 34 | func TestPartTwo(t *testing.T) { 35 | tt := []struct { 36 | name string 37 | input string 38 | expected string 39 | }{ 40 | { 41 | name: "example", 42 | input: sample, 43 | expected: "1514285714288", 44 | }, 45 | } 46 | 47 | for _, tc := range tt { 48 | t.Run(tc.name, func(t *testing.T) { 49 | input := utils.SplitInput(tc.input) 50 | if got := PartTwo(input); got != tc.expected { 51 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 52 | } 53 | }) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /2022/day17/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/17](https://github.com/lucianoq/adventofcode/tree/master/2022/17) 3 | 4 | -------------------------------------------------------------------------------- /2022/day18/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/18](https://github.com/lucianoq/adventofcode/tree/master/2022/18) 3 | 4 | -------------------------------------------------------------------------------- /2022/day19/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/19](https://github.com/lucianoq/adventofcode/tree/master/2022/19) 3 | 4 | -------------------------------------------------------------------------------- /2022/day20/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/20](https://github.com/lucianoq/adventofcode/tree/master/2022/20) 3 | 4 | -------------------------------------------------------------------------------- /2022/day21/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/21](https://github.com/lucianoq/adventofcode/tree/master/2022/21) 3 | 4 | -------------------------------------------------------------------------------- /2022/day22/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/22](https://github.com/lucianoq/adventofcode/tree/master/2022/22) 3 | 4 | -------------------------------------------------------------------------------- /2022/day23/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/23](https://github.com/lucianoq/adventofcode/tree/master/2022/23) 3 | 4 | -------------------------------------------------------------------------------- /2022/day24/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/24](https://github.com/lucianoq/adventofcode/tree/master/2022/24) 3 | 4 | -------------------------------------------------------------------------------- /2022/day25/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2022/25](https://github.com/lucianoq/adventofcode/tree/master/2022/25) 3 | 4 | -------------------------------------------------------------------------------- /2023/day01/enrichman/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /2023/day01/enrichman/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | "unicode" 7 | ) 8 | 9 | func PartOne(input []string) string { 10 | var tot int 11 | 12 | for _, line := range input { 13 | var result string 14 | 15 | runes := []rune(line) 16 | for _, r := range runes { 17 | if unicode.IsDigit(r) { 18 | result = string(r) 19 | break 20 | } 21 | } 22 | 23 | for i := len(runes) - 1; i >= 0; i-- { 24 | if unicode.IsDigit(runes[i]) { 25 | result += string(runes[i]) 26 | break 27 | } 28 | } 29 | 30 | res, err := strconv.Atoi(result) 31 | if err != nil { 32 | panic(err) 33 | } 34 | tot += res 35 | } 36 | 37 | return strconv.Itoa(tot) 38 | } 39 | 40 | func PartTwo(input []string) string { 41 | tot := 0 42 | 43 | for _, line := range input { 44 | digits := []string{} 45 | 46 | for i, r := range line { 47 | if unicode.IsDigit(r) { 48 | digits = append(digits, string(r)) 49 | } else { 50 | if digitStr, found := hasDigitPrefix(line[i:]); found { 51 | digits = append(digits, digitStr) 52 | } 53 | } 54 | } 55 | 56 | concat := digits[0] + digits[len(digits)-1] 57 | 58 | result, err := strconv.Atoi(concat) 59 | if err != nil { 60 | panic(err) 61 | } 62 | tot += result 63 | } 64 | 65 | return strconv.Itoa(tot) 66 | } 67 | 68 | func hasDigitPrefix(s string) (string, bool) { 69 | digits := []string{ 70 | "zero", "one", "two", "three", "four", 71 | "five", "six", "seven", "eight", "nine", 72 | } 73 | 74 | for i, digitString := range digits { 75 | if strings.HasPrefix(s, digitString) { 76 | return strconv.Itoa(i), true 77 | } 78 | } 79 | return "", false 80 | } 81 | -------------------------------------------------------------------------------- /2023/day01/enrichman/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `1abc2 10 | pqr3stu8vwx 11 | a1b2c3d4e5f 12 | treb7uchet` 13 | 14 | func TestPartOne(t *testing.T) { 15 | tt := []struct { 16 | name string 17 | input string 18 | expected string 19 | }{ 20 | { 21 | name: "example", 22 | input: sample, 23 | expected: "142", 24 | }, 25 | } 26 | 27 | for _, tc := range tt { 28 | t.Run(tc.name, func(t *testing.T) { 29 | input := utils.SplitInput(tc.input) 30 | if got := PartOne(input); got != tc.expected { 31 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 32 | } 33 | }) 34 | } 35 | } 36 | 37 | var sample2 string = `two1nine 38 | eightwothree 39 | abcone2threexyz 40 | xtwone3four 41 | 4nineeightseven2 42 | zoneight234 43 | 7pqrstsixteen` 44 | 45 | func TestPartTwo(t *testing.T) { 46 | tt := []struct { 47 | name string 48 | input string 49 | expected string 50 | }{ 51 | { 52 | name: "example", 53 | input: sample2, 54 | expected: "281", 55 | }, 56 | } 57 | 58 | for _, tc := range tt { 59 | t.Run(tc.name, func(t *testing.T) { 60 | input := utils.SplitInput(tc.input) 61 | if got := PartTwo(input); got != tc.expected { 62 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 63 | } 64 | }) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /2023/day01/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/1](https://github.com/lucianoq/adventofcode/tree/master/2023/1) 3 | 4 | -------------------------------------------------------------------------------- /2023/day02/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/2](https://github.com/lucianoq/adventofcode/tree/master/2023/2) 3 | 4 | -------------------------------------------------------------------------------- /2023/day03/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/3](https://github.com/lucianoq/adventofcode/tree/master/2023/3) 3 | 4 | -------------------------------------------------------------------------------- /2023/day04/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/4](https://github.com/lucianoq/adventofcode/tree/master/2023/4) 3 | 4 | -------------------------------------------------------------------------------- /2023/day05/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/5](https://github.com/lucianoq/adventofcode/tree/master/2023/5) 3 | 4 | -------------------------------------------------------------------------------- /2023/day06/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/6](https://github.com/lucianoq/adventofcode/tree/master/2023/6) 3 | 4 | -------------------------------------------------------------------------------- /2023/day07/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/7](https://github.com/lucianoq/adventofcode/tree/master/2023/7) 3 | 4 | -------------------------------------------------------------------------------- /2023/day08/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/8](https://github.com/lucianoq/adventofcode/tree/master/2023/8) 3 | 4 | -------------------------------------------------------------------------------- /2023/day09/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/9](https://github.com/lucianoq/adventofcode/tree/master/2023/9) 3 | 4 | -------------------------------------------------------------------------------- /2023/day10/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/10](https://github.com/lucianoq/adventofcode/tree/master/2023/10) 3 | 4 | -------------------------------------------------------------------------------- /2023/day11/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/11](https://github.com/lucianoq/adventofcode/tree/master/2023/11) 3 | 4 | -------------------------------------------------------------------------------- /2023/day12/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/12](https://github.com/lucianoq/adventofcode/tree/master/2023/12) 3 | 4 | -------------------------------------------------------------------------------- /2023/day13/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/13](https://github.com/lucianoq/adventofcode/tree/master/2023/13) 3 | 4 | -------------------------------------------------------------------------------- /2023/day14/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/14](https://github.com/lucianoq/adventofcode/tree/master/2023/14) 3 | 4 | -------------------------------------------------------------------------------- /2023/day15/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/15](https://github.com/lucianoq/adventofcode/tree/master/2023/15) 3 | 4 | -------------------------------------------------------------------------------- /2023/day16/lucianoq/README.md: -------------------------------------------------------------------------------- 1 | 2 | [lucianoq/adventofcode/2023/16](https://github.com/lucianoq/adventofcode/tree/master/2023/16) 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Golang Roma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | go build -o aoc ./cmd/aoc/main.go 3 | 4 | test: 5 | go test -v $(shell go list ./... | grep -v template) -------------------------------------------------------------------------------- /cmd/aoc/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/golangroma/aoc/internal/challenge" 8 | ) 9 | 10 | func main() { 11 | session := os.Getenv("SESSION") 12 | if err := challenge.Execute(session); err != nil { 13 | log.Fatal(err) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/golangroma/aoc 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/pkg/errors v0.9.1 7 | gopkg.in/yaml.v2 v2.4.0 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 2 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 3 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 4 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 5 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 6 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 7 | -------------------------------------------------------------------------------- /internal/challenge/teams.go: -------------------------------------------------------------------------------- 1 | package challenge 2 | 3 | import ( 4 | "os" 5 | 6 | "gopkg.in/yaml.v2" 7 | ) 8 | 9 | type Participant struct { 10 | ID int `yaml:"aoc_id"` 11 | Name string `yaml:"name"` 12 | } 13 | 14 | type Teams struct { 15 | Teams []*Team `yaml:"team"` 16 | Participants []Participant `yaml:"participants"` 17 | } 18 | 19 | type Team struct { 20 | ID string `yaml:"id"` 21 | Members []string `yaml:"members"` 22 | Stats map[string]Stats 23 | } 24 | 25 | func LoadTeams() (*Teams, error) { 26 | f, err := os.ReadFile("teams.yaml") 27 | if err != nil { 28 | return nil, err 29 | } 30 | 31 | teams := &Teams{} 32 | 33 | err = yaml.Unmarshal(f, teams) 34 | if err != nil { 35 | return nil, err 36 | } 37 | 38 | return teams, nil 39 | } 40 | -------------------------------------------------------------------------------- /internal/challenge/users.go: -------------------------------------------------------------------------------- 1 | package challenge 2 | 3 | import "fmt" 4 | 5 | type User struct { 6 | AocID int 7 | Name string 8 | Username string 9 | AvatarURL string 10 | Team *Team 11 | ProfileURL string 12 | Stats map[string]Stats 13 | } 14 | 15 | type Stats struct { 16 | Submissions int 17 | Score int 18 | Stars [25]Star 19 | } 20 | 21 | type Star int 22 | 23 | const ( 24 | NoStar Star = iota 25 | SilverStar 26 | GoldStar 27 | ) 28 | 29 | func GetUsersFromTeams(teams *Teams) []*User { 30 | participantsMap := make(map[string]*User) 31 | 32 | // load users from the participants 33 | for _, p := range teams.Participants { 34 | participantsMap[p.Name] = &User{ 35 | AocID: p.ID, 36 | Username: p.Name, 37 | AvatarURL: fmt.Sprintf("https://github.com/%s.png?size=60", p.Name), 38 | ProfileURL: fmt.Sprintf("https://github.com/%s", p.Name), 39 | } 40 | } 41 | 42 | // check if a user is part of a team 43 | for _, t := range teams.Teams { 44 | for _, member := range t.Members { 45 | if m, found := participantsMap[member]; found { 46 | m.Team = t 47 | participantsMap[member] = m 48 | } 49 | } 50 | } 51 | 52 | users := []*User{} 53 | for _, v := range participantsMap { 54 | users = append(users, v) 55 | } 56 | 57 | return users 58 | } 59 | -------------------------------------------------------------------------------- /scripts/new-puzzle.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | source .env 6 | 7 | YEAR=$1 8 | DAY=$(printf %02d $2) 9 | 10 | mkdir -p ./$YEAR/day$DAY/$USERNAME 11 | cp ./template/* ./$YEAR/day$DAY/$USERNAME 12 | cd ./$YEAR/day$DAY/$USERNAME 13 | 14 | if [[ -z $session ]]; then 15 | echo "Missing 'Advent of Code' session. Cannot download input." 16 | else 17 | curl -s --cookie "session=$session" https://adventofcode.com/$1/day/$(printf %d $2)/input > input.txt 18 | fi 19 | 20 | echo "You can now run 'cd ./$YEAR/day$DAY/$USERNAME' and work on your solution!" 21 | -------------------------------------------------------------------------------- /teams.yaml: -------------------------------------------------------------------------------- 1 | 2 | participants: 3 | 4 | - aoc_id: 504742 5 | name: enrichman 6 | - name: luigibarbato 7 | - aoc_id: 2284430 8 | name: giulianopz 9 | - aoc_id: 2432594 10 | name: omissis 11 | - aoc_id: 731816 12 | name: mastrogiovanni 13 | - aoc_id: 1599019 14 | name: Al-Pragliola 15 | - aoc_id: 373470 16 | name: lucianoq 17 | - aoc_id: 2292598 18 | name: alessio-perugini 19 | - aoc_id: 2322348 20 | name: fdonzello 21 | - aoc_id: 1247572 22 | name: mbiagetti 23 | - aoc_id: 2405432 24 | name: 8lall0 25 | - aoc_id: 2527273 26 | name: misterdelle 27 | - aoc_id: 3525219 28 | name: saiello 29 | 30 | # 31 | # Join a team by adding your name to it! 32 | # 33 | team: 34 | - id: golangroma 35 | link: https://golangroma.it 36 | members: 37 | - enrichman 38 | - giulianopz 39 | - mastrogiovanni 40 | - lucianoq 41 | - alessio-perugini 42 | - mbiagetti 43 | - misterdelle 44 | - saiello 45 | 46 | - id: golangnapoli 47 | link: https://golangnapoli.it 48 | members: 49 | - luigibarbato 50 | - Al-Pragliola 51 | 52 | - id: golangtorino 53 | link: https://www.meetup.com/golang-torino/ 54 | members: 55 | - omissis 56 | - 8lall0 57 | 58 | - id: golangmilano 59 | link: https://www.meetup.com/golang-milano/ 60 | members: [] 61 | -------------------------------------------------------------------------------- /template/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | func main() { 10 | input := utils.ReadFile("input.txt") 11 | 12 | fmt.Printf("Part 1: %v\n", PartOne(input)) 13 | fmt.Printf("Part 2: %v\n", PartTwo(input)) 14 | } 15 | -------------------------------------------------------------------------------- /template/parts.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func PartOne(input []string) string { 4 | return "" 5 | } 6 | 7 | func PartTwo(input []string) string { 8 | return "" 9 | } 10 | -------------------------------------------------------------------------------- /template/parts_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golangroma/aoc/utils" 7 | ) 8 | 9 | var sample string = `` 10 | 11 | func TestPartOne(t *testing.T) { 12 | tt := []struct { 13 | name string 14 | input string 15 | expected string 16 | }{ 17 | { 18 | name: "example", 19 | input: sample, 20 | expected: "not-yet-implemented", 21 | }, 22 | } 23 | 24 | for _, tc := range tt { 25 | t.Run(tc.name, func(t *testing.T) { 26 | input := utils.SplitInput(tc.input) 27 | if got := PartOne(input); got != tc.expected { 28 | t.Errorf("PartOne() = %v, want %v", got, tc.expected) 29 | } 30 | }) 31 | } 32 | } 33 | 34 | func TestPartTwo(t *testing.T) { 35 | tt := []struct { 36 | name string 37 | input string 38 | expected string 39 | }{ 40 | { 41 | name: "example", 42 | input: sample, 43 | expected: "not-yet-implemented", 44 | }, 45 | } 46 | 47 | for _, tc := range tt { 48 | t.Run(tc.name, func(t *testing.T) { 49 | input := utils.SplitInput(tc.input) 50 | if got := PartTwo(input); got != tc.expected { 51 | t.Errorf("PartTwo() = %v, want %v", got, tc.expected) 52 | } 53 | }) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /utils/utils.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "log" 5 | "os" 6 | "strconv" 7 | "strings" 8 | ) 9 | 10 | func ReadFile(filename string) []string { 11 | content, err := os.ReadFile(filename) 12 | CheckErr(err) 13 | 14 | return SplitInput(string(content)) 15 | } 16 | 17 | func SplitInput(content string) []string { 18 | stringContent := strings.TrimSpace(string(content)) 19 | return strings.Split(stringContent, "\n") 20 | } 21 | 22 | func CheckErr(err error) { 23 | if err != nil { 24 | log.Fatal(err) 25 | } 26 | } 27 | 28 | type Converter[T, V any] func(T) (V, error) 29 | 30 | func Convert[T, V any](value T, converter Converter[T, V]) (V, error) { 31 | return converter(value) 32 | } 33 | 34 | func StringSliceToIntSliceConverter(arr []string) ([]int, error) { 35 | converted := []int{} 36 | 37 | for _, v := range arr { 38 | res, err := strconv.Atoi(v) 39 | if err != nil { 40 | return nil, err 41 | } 42 | converted = append(converted, res) 43 | } 44 | 45 | return converted, nil 46 | } 47 | --------------------------------------------------------------------------------