2025 Day 2 Complete!
This commit is contained in:
2
.envrc
2
.envrc
@@ -1,4 +1,4 @@
|
||||
PATH_add sbin
|
||||
PATH_add bin
|
||||
|
||||
export AOCROOT=`pwd`
|
||||
export CURRDAY=`date +%d`
|
||||
|
||||
1
2025/day02/input
Normal file
1
2025/day02/input
Normal file
@@ -0,0 +1 @@
|
||||
9595822750-9596086139,1957-2424,88663-137581,48152-65638,12354817-12385558,435647-489419,518494-609540,2459-3699,646671-688518,195-245,295420-352048,346-514,8686839668-8686892985,51798991-51835611,8766267-8977105,2-17,967351-995831,6184891-6331321,6161577722-6161678622,912862710-913019953,6550936-6625232,4767634976-4767662856,2122995-2257010,1194-1754,779-1160,22-38,4961-6948,39-53,102-120,169741-245433,92902394-92956787,531-721,64-101,15596-20965,774184-943987,8395-11781,30178-47948,94338815-94398813
|
||||
67
2025/day02/main.go
Normal file
67
2025/day02/main.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToString()
|
||||
part1(inp)
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
func part1(inp string) {
|
||||
var res int
|
||||
ranges := getRanges(inp)
|
||||
for _, rng := range ranges {
|
||||
for wrk := rng.beg; wrk <= rng.end; wrk++ {
|
||||
sWrk := h.Itoa(wrk)
|
||||
hlf := len(sWrk) / 2
|
||||
if sWrk[:hlf] == sWrk[hlf:] {
|
||||
res += wrk
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func part2(inp string) {
|
||||
var res int
|
||||
ranges := getRanges(inp)
|
||||
for _, rng := range ranges {
|
||||
for wrk := rng.beg; wrk <= rng.end; wrk++ {
|
||||
sWrk := h.Itoa(wrk)
|
||||
cnt := len(sWrk)
|
||||
for i := 0; i < cnt/2; i++ {
|
||||
ch := i + 1
|
||||
if cnt%ch != 0 { // Must be divisible
|
||||
continue
|
||||
}
|
||||
if strings.Repeat(sWrk[:ch], cnt/ch) == sWrk {
|
||||
res += wrk
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
type Range struct {
|
||||
beg, end int
|
||||
}
|
||||
|
||||
func getRanges(inp string) []Range {
|
||||
ret := []Range{}
|
||||
segs := strings.Split(inp, ",")
|
||||
for i := range segs {
|
||||
pts := strings.Split(segs[i], "-")
|
||||
ret = append(ret, Range{beg: h.Atoi(pts[0]), end: h.Atoi(pts[1])})
|
||||
}
|
||||
return ret
|
||||
}
|
||||
119
2025/day02/problem
Normal file
119
2025/day02/problem
Normal file
@@ -0,0 +1,119 @@
|
||||
Advent of Code
|
||||
|
||||
br0xen [7](AoC++) 4*
|
||||
|
||||
--- Day 2: Gift Shop ---
|
||||
|
||||
You get inside and take the elevator to its only other stop: the gift
|
||||
shop. "Thank you for visiting the North Pole!" gleefully exclaims a nearby
|
||||
sign. You aren't sure who is even allowed to visit the North Pole, but you
|
||||
know you can access the lobby through here, and from there you can access
|
||||
the rest of the North Pole base.
|
||||
|
||||
As you make your way through the surprisingly extensive selection, one of
|
||||
the clerks recognizes you and asks for your help.
|
||||
|
||||
As it turns out, one of the younger Elves was playing on a gift shop
|
||||
computer and managed to add a whole bunch of invalid product IDs to their
|
||||
gift shop database! Surely, it would be no trouble for you to identify the
|
||||
invalid product IDs for them, right?
|
||||
|
||||
They've even checked most of the product ID ranges already; they only have
|
||||
a few product ID ranges (your puzzle input) that you'll need to check. For
|
||||
example:
|
||||
|
||||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
|
||||
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
|
||||
824824821-824824827,2121212118-2121212124
|
||||
|
||||
(The ID ranges are wrapped here for legibility; in your input, they appear
|
||||
on a single long line.)
|
||||
|
||||
The ranges are separated by commas (,); each range gives its first ID and
|
||||
last ID separated by a dash (-).
|
||||
|
||||
Since the young Elf was just doing silly patterns, you can find the
|
||||
invalid IDs by looking for any ID which is made only of some sequence of
|
||||
digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123
|
||||
twice) would all be invalid IDs.
|
||||
|
||||
None of the numbers have leading zeroes; 0101 isn't an ID at all. (101 is
|
||||
a valid ID that you would ignore.)
|
||||
|
||||
Your job is to find all of the invalid IDs that appear in the given
|
||||
ranges. In the above example:
|
||||
|
||||
• 11-22 has two invalid IDs, 11 and 22.
|
||||
• 95-115 has one invalid ID, 99.
|
||||
• 998-1012 has one invalid ID, 1010.
|
||||
• 1188511880-1188511890 has one invalid ID, 1188511885.
|
||||
• 222220-222224 has one invalid ID, 222222.
|
||||
• 1698522-1698528 contains no invalid IDs.
|
||||
• 446443-446449 has one invalid ID, 446446.
|
||||
• 38593856-38593862 has one invalid ID, 38593859.
|
||||
• The rest of the ranges contain no invalid IDs.
|
||||
|
||||
Adding up all the invalid IDs in this example produces 1227775554.
|
||||
|
||||
What do you get if you add up all of the invalid IDs?
|
||||
|
||||
Your puzzle answer was 40398804950.
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
The clerk quickly discovers that there are still invalid IDs in the ranges
|
||||
in your list. Maybe the young Elf was doing other silly patterns as well?
|
||||
|
||||
Now, an ID is invalid if it is made only of some sequence of digits
|
||||
repeated at least twice. So, 12341234 (1234 two times), 123123123 (123
|
||||
three times), 1212121212 (12 five times), and 1111111 (1 seven times) are
|
||||
all invalid IDs.
|
||||
|
||||
From the same example as before:
|
||||
|
||||
• 11-22 still has two invalid IDs, 11 and 22.
|
||||
• 95-115 now has two invalid IDs, 99 and 111.
|
||||
• 998-1012 now has two invalid IDs, 999 and 1010.
|
||||
• 1188511880-1188511890 still has one invalid ID, 1188511885.
|
||||
• 222220-222224 still has one invalid ID, 222222.
|
||||
• 1698522-1698528 still contains no invalid IDs.
|
||||
• 446443-446449 still has one invalid ID, 446446.
|
||||
• 38593856-38593862 still has one invalid ID, 38593859.
|
||||
• 565653-565659 now has one invalid ID, 565656.
|
||||
• 824824821-824824827 now has one invalid ID, 824824824.
|
||||
• 2121212118-2121212124 now has one invalid ID, 2121212121.
|
||||
|
||||
Adding up all the invalid IDs in this example produces 4174379265.
|
||||
|
||||
What do you get if you add up all of the invalid IDs using these new
|
||||
rules?
|
||||
|
||||
Your puzzle answer was 65794984339.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should [16]return to your Advent calendar and try
|
||||
another puzzle.
|
||||
|
||||
If you still want to see it, you can [17]get your puzzle input.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
1. https://adventofcode.com/
|
||||
2. https://adventofcode.com/2025/about
|
||||
3. https://adventofcode.com/2025/events
|
||||
5. https://adventofcode.com/2025/settings
|
||||
6. https://adventofcode.com/2025/auth/logout
|
||||
7. Advent of Code Supporter
|
||||
https://adventofcode.com/2025/support
|
||||
8. https://adventofcode.com/2025
|
||||
9. https://adventofcode.com/2025
|
||||
10. https://adventofcode.com/2025/support
|
||||
11. https://adventofcode.com/2025/sponsors
|
||||
12. https://adventofcode.com/2025/leaderboard/private
|
||||
13. https://adventofcode.com/2025/stats
|
||||
14. https://adventofcode.com/2025/sponsors
|
||||
15. https://adventofcode.com/2025/sponsors/redirect?url=https%3A%2F%2Fwww%2Ecitadelsecurities%2Ecom%2F
|
||||
16. https://adventofcode.com/2025
|
||||
17. https://adventofcode.com/2025/day/2/input
|
||||
1
2025/day02/testinput
Normal file
1
2025/day02/testinput
Normal file
@@ -0,0 +1 @@
|
||||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||
Reference in New Issue
Block a user