add new solutions

This commit is contained in:
kanna5 2026-01-27 18:38:43 +09:00
parent 67cad91898
commit 81cc2d3ba6
No known key found for this signature in database
19 changed files with 693 additions and 14 deletions

View file

@ -0,0 +1,35 @@
package q1114
type Foo struct {
one, two chan struct{}
}
func NewFoo() *Foo {
return &Foo{
one: make(chan struct{}),
two: make(chan struct{}),
}
}
func (f *Foo) First(printFirst func()) {
// Do not change this line
printFirst()
close(f.one)
}
func (f *Foo) Second(printSecond func()) {
<-f.one
/// Do not change this line
printSecond()
close(f.two)
}
func (f *Foo) Third(printThird func()) {
<-f.two
// Do not change this line
printThird()
}

View file

@ -0,0 +1,36 @@
package q1115
type FooBar struct {
n int
foo, bar chan struct{}
}
func NewFooBar(n int) *FooBar {
ret := &FooBar{
n: n,
foo: make(chan struct{}, 1),
bar: make(chan struct{}, 1),
}
ret.bar <- struct{}{}
return ret
}
func (fb *FooBar) Foo(printFoo func()) {
for i := 0; i < fb.n; i++ {
<-fb.bar
// printFoo() outputs "foo". Do not change or remove this line.
printFoo()
fb.foo <- struct{}{}
}
}
func (fb *FooBar) Bar(printBar func()) {
for i := 0; i < fb.n; i++ {
<-fb.foo
// printBar() outputs "bar". Do not change or remove this line.
printBar()
fb.bar <- struct{}{}
}
}

View file

@ -0,0 +1,53 @@
package q1116
type void struct{}
type ZeroEvenOdd struct {
n int
c0, c1, c2 chan void
}
func NewZeroEvenOdd(n int) *ZeroEvenOdd {
zeo := &ZeroEvenOdd{
n: n,
c0: make(chan void, 1),
c1: make(chan void, 1),
c2: make(chan void, 1),
}
zeo.c0 <- void{}
return zeo
}
func (z *ZeroEvenOdd) Zero(printNumber func(int)) {
for i := range z.n {
<-z.c0
printNumber(0)
if (i+1)%2 == 0 {
z.c2 <- void{}
} else {
z.c1 <- void{}
}
}
}
func (z *ZeroEvenOdd) Even(printNumber func(int)) {
for i := range z.n {
if (i+1)%2 != 0 {
continue
}
<-z.c2
printNumber(i + 1)
z.c0 <- void{}
}
}
func (z *ZeroEvenOdd) Odd(printNumber func(int)) {
for i := range z.n {
if (i+1)%2 == 0 {
continue
}
<-z.c1
printNumber(i + 1)
z.c0 <- void{}
}
}

View file

@ -0,0 +1,38 @@
package q1117
type void struct{}
type H2O struct {
h, o, hRel chan void
}
func NewH2O() *H2O {
h := &H2O{
h: make(chan void, 2),
o: make(chan void, 2),
hRel: make(chan void, 2),
}
h.o <- void{}
return h
}
func (h *H2O) Hydrogen(releaseHydrogen func()) {
<-h.h
// releaseHydrogen() outputs "H". Do not change or remove this line.
releaseHydrogen()
h.hRel <- void{} // confirmation
}
func (h *H2O) Oxygen(releaseOxygen func()) {
<-h.o
// releaseOxygen() outputs "H". Do not change or remove this line.
releaseOxygen()
h.h <- void{}
h.h <- void{}
<-h.hRel
<-h.hRel
h.o <- void{}
}