#Day 06 - Solutions

87 messages · Page 1 of 1 (latest)

fading plaza
#

beep boop

fringe maple
ornate swift
#
package main

import (
    _ "embed"
    "fmt"
)

//go:embed input
var input string

func main() {
    // Part 1
    fmt.Println(markerIndex(4))

    // Part 2
    fmt.Println(markerIndex(14))
}

func markerIndex(markerLen int) int {
    for i := 0; i < len(input); i++ {
        if isMarker(input[i : i+markerLen]) {
            return i + markerLen
        }
    }
    return -1
}

func isMarker(s string) bool {
    for i := 0; i < len(s); i++ {
        for j := i + 1; j < len(s); j++ {
            if s[i] == s[j] {
                return false
            }
        }
    }
    return true
}
rare pebble
#

map ez

ornate swift
#

boring af

rare pebble
#

haha

ornate swift
#

this doesnt get harder at all

#

isnt it supposed to get harder every day?

rare pebble
#

eventually I think. its still first week

frosty moss
#

There wasn't a lot to work with today... ||```rb

Day 6: We had to fix a handheld communicator

$stdin = open 'day6.txt'

"#
or
:"
S=
-> *
x,n{(n..).
find{L[_1-
n,n].chars
.uniq.size
.==n;}};L=
gets;p([S[
4],S[14]])

ornate swift
frosty moss
#

i could not figure out what to make

#

like... checksums?

#

then someone suggested a handheld radio

#

i spent forever trying to figure out how to get the second knob to be a *

ornate swift
#

lool

violet gyro
#
package main

import (
    "fmt"
    "os"
    "strings"
)

func main() {
    f, _ := os.ReadFile("./input.txt")
    fmt.Println(getFirstMarker(string(f), 4))
    fmt.Println(getFirstMarker(string(f), 14))
}

func getFirstMarker(s string, size int) int {
    for i := size; i < len(s); i++ {
        if isUnique(s[i-size:i]) {
            return i
        }
    }
    return 0
}

func isUnique(s string) bool {
    ss := strings.Split(s, "")
    set := make(map[string]struct{})
    for _, v := range ss {
        set[v] = struct{}{}
    }
    return len(set) == len(ss)
}
#

why is it so easy today 🤣

hallow falcon
#

very easy day lol

quartz bane
signal geode
hallow falcon
#

so you don't gotta do f, err := os.ReadFile("input") intput := string(f)

#

it basically does that for you with a comment

signal geode
#

holy shit

#

that is awesome

hallow falcon
#

yea I think it came out in like 1.15 or 1.16

#

something like that can't remember

signal geode
#

i need some kind of sheet where every useful feature is listed :/

tall pond
edgy dune
#
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    f, _ := os.Open("input.txt")
    defer f.Close()

    buf := bufio.NewScanner(f)

    var result int

    for buf.Scan() {
        line := buf.Text()
        var i int
 
        for i = 0; i < len(line)-3; i++ {
            fourChars := line[i : i+4]
            uniq := make(map[rune]bool)
            for _, letter := range fourChars {
                uniq[letter] = true
            }

            if len(uniq) == 4 {
                result = i + 4
                fmt.Println("Part 1:", result)
                break
            }
        }
    }
}```
grave cave
#

https://github.com/mohanavel15/advent-of-code/blob/main/2022/day06.go

func Solution(input string) {
    input_len := len(input)
    for i := range input {
        if input_len-i < 3 {
            break
        }

        last_index := i + 4 // i + 4 for question 1 and i + 14 for question 2

        marker := input[i:last_index]
        has_repeat := false

        for mi, r := range marker {
            if strings.IndexRune(marker[mi+1:], r) != -1 {
                has_repeat = true
                break
            }
        }

        if !has_repeat {
            fmt.Println("Answer:", last_index)
            break
        }
    }
}
GitHub

My solutions for Advent of code. Contribute to mohanavel15/advent-of-code development by creating an account on GitHub.

agile ruin
tall pond
#

Oh i see

#

no i dont

agile ruin
tall pond
charred hearth
#

Today was very easy

d6p1 :: String -> Int -> Int
d6p1 a b = if (length . fromList . take 4) a == 4 then b + 4 else d6p1 (tail a) (b + 1)

d6p2 :: String -> Int -> Int
d6p2 a b = if (length . fromList . take 14) a == 14 then b + 14 else d6p2 (tail a) (b + 1)
charred hearth
# tall pond what lang is this even

Haskell.
You can write the entire solution for today as:

main :: IO ()
main = do
  input <- readFile "06.txt"
  print $ solve input 0 4
  print $ solve input 0 14
  where
    solve a b c = if (length . fromList . take c) a == c then b + c else solve (tail a) (b + 1) c
halcyon dew
#
import sys

data = sys.stdin.read().strip()

packet, message = -1, -1

for i in range(len(data)):
    if len({*data[i:i+4]}) == 4 and packet == -1:
        packet = i + 4

    if len({*data[i:i+14]}) == 14 and message == -1:
        message = i + 14

    if packet != -1 and message != -1:
        break

print(packet, message)
```not bad
#

21st place on our leaderboard lol

#

hopefully i can score good tonight

charred hearth
halcyon dew
#

lol epic

#

throw a lambda and a __import__ in there to avoid the declaration of data and ur good lol

#

or just input().strip() i guess

#

it's only one line input

charred hearth
#

You wanna get really dirty 😏

halcyon dew
#

lmao

charred hearth
#
(lambda data: print(next(filter(lambda i: len({*data[i:i+4]}) == 4, range(len(data)))) + 4, next(filter(lambda i: len({*data[i:i+14]}) == 14, range(len(data)))) + 14))(input())
``` here
halcyon dew
#

nice

fading plaza
#

Not a bad day for go

vale kernel
#

so much easier than yesterday

fading plaza
#
:- use_module(library(dcg/basics)).
:- use_module(library(clpfd)).

numbers([R]) --> nonblank(R).
numbers([R|Rs]) --> nonblank(R), numbers(Rs).

marker(Index, Length, [H|Tail], Result):-
    length(Sub, Length),
    append(Sub, _, [H|Tail]),
    all_distinct(Sub),
    Result is Index + Length.

marker(Index, Length, [_|Tail], Result):-
    NIndex is Index + 1,
    marker(NIndex, Length, Tail, Result).

:-
    phrase_from_file(numbers(A), 'input.txt'),
    marker(0, 4, A, P1),
    marker(0, 14, A, P2),
    format("~p\n", P1),
    format("~p\n", P2),
    halt.

#

Some more prolog

worn forge
edgy dune
#

Out of curiosity. How come you use embedfs instead of just calling os.Open?

fading plaza
#

When using embed it might ne easier to define the type as []byte

#

That way you don’t have to open and readall

edgy dune
#

You can os.ReadFile too

charred hearth
#

Embeding the file directly as string or bytes is simple and a struggle free feature because it just works.

fading plaza
#

They aren’t doing that though 😉

worn forge
#

Simplified to this: ```go
func solve(data []byte, n int) {
outer:
for i := 0; i < len(data); i++ {
var bits uint64
for j := 0; j < n; j++ {
if bits&(1<<(data[i+j]-'a')) != 0 {
continue outer
}

        bits |= 1 << (data[i+j] - 'a')
    }

    // if we got here, all chars were different
    fmt.Println(i + n)
    return
}

}```

fading plaza
#

Simplified??

worn forge
#

By simplified I meant I reduced LOC
But yeah this would be much simpler with a map hehe

ornate swift
worn forge
#

Wait, wtf
~~Benchmarking that yields ~~
BenchmarkSolve-16 34561 33238 ns/op 0 B/op 0 allocs/op
~~Change it to ~~```go
func solve(data []byte, n int) {
outer:
for i := 0; i < len(data); i++ {
var bits uint64
for j := 0; j < n; j++ {
v := data[i+j] - 'a'
if bits&(1<<v) != 0 {
continue outer
}

        bits |= 1 << (v)
    }

    // if we got here, all chars were different
    _ = i + n
    return
}

}```And now the benchmark is
BenchmarkSolve-16 56258526 21.87 ns/op 0 B/op 0 allocs/op
???

#

It is not the println btw
I removed that for both benchs

fading plaza
#

They use a bit mask to keep track of what numbers have been seen instead of a map

#

When the bit wise and yields something other than 0 there is a duplicate so they skip that entry

tall pond
#

How do you benchmark that? Ive tried doing pprof with cpuprofile on my code but no results

fading plaza
#

Have the func return data. Compiler might be doing something too smart

worn forge
#

Fair will do

worn forge
tall pond
#

Saving this for tomorrow, Thanks a ton!

worn forge
rich sail
#
    var marker_appears int
    var marker string
    for i := 0; i < len(input); i++ {
        if len(marker) < 4 {
            if !strings.Contains(marker, string(input[i])) {
                marker = fmt.Sprintf("%v%v", marker, string(input[i]))
            } else {
                marker = ""
            }
        } else {
            marker_appears = i + 1 //because it's after
            break
        }
    }
    fmt.Printf("the input is %v in length\n", len(input))
    fmt.Printf("the marker is %v\n", marker)
    fmt.Printf("marker appears at %v\n", marker_appears)

not sure what im doing wrong here..

#

oop, nvm i see now

quartz bane
#

looks good, except it should be i and not i+1, and you shouldn't completly delete the marker